package com.yutou.nas.bangumi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.nas.mybatis.dao.BangumiItemDao;
import com.yutou.nas.mybatis.dao.BangumiListDao;
import com.yutou.nas.mybatis.model.BangumiItem;
import com.yutou.nas.mybatis.model.BangumiItemExample;
import com.yutou.nas.mybatis.model.BangumiList;
import com.yutou.nas.mybatis.model.BangumiListExample;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("anim/")
public class AnimationController {
    @Resource
    BangumiListDao listDao;
    @Resource
    BangumiItemDao itemDao;

    @ResponseBody
    @RequestMapping(value = "rss/data.do", method = RequestMethod.GET)
    public String getAnimList(String key,String type,String team, int page) {
        boolean isNull = key.length() == 0;
        if(!StringUtils.isEmpty(type)&&type.length()>2)
        type=type.substring(2);
        if(!StringUtils.isEmpty(team)&&team.length()>2)
            team=team.substring(2);
        String[] keys = key.split(" ");
        JSONArray items = null;
        if (!isNull)
            items = new AnimationData().bangumiList(page,type,team, keys);
        JSONObject json = new JSONObject();
        if (items != null) {
            json.put("data", items);
        } else {
            json.put("data", new JSONArray());
        }
        json.put("count", 999);
        json.put("code", 0);
        json.put("msg", "ok");
        return json.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "type/add.do", method = RequestMethod.POST)
    public String addRssType(String title) {
        BangumiList bangumiList = new BangumiList();
        bangumiList.setTitle(title);
        bangumiList.setStatus(1);
        int i = listDao.insert(bangumiList);
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", i == 0 ? "添加失败" : "添加成功");
        return json.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "type/list.do", method = RequestMethod.GET)
    public String getRssTypeList() {
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", "ok");
        json.put("data", JSONArray.toJSON(listDao.selectByExample(new BangumiListExample())));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "type/del.do", method = RequestMethod.POST)
    public String delType(int id){
        BangumiItemExample example=new BangumiItemExample();
        example.createCriteria().andBidEqualTo(id+"");
        itemDao.deleteByExample(example);
        int i=listDao.deleteByPrimaryKey(id);
        JSONObject json=new JSONObject();
        json.put("code", 0);
        json.put("msg", i == 0 ? "删除失败" : "删除成功");
        return json.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "rss/add.do", method = RequestMethod.POST)
    public String addAnimationRss(String title, String author, String categories, String titleKey, String bid) {
        BangumiItem item = new BangumiItem();
        item.setTitle(title);
        item.setAuthor(author);
        item.setCategories(categories);
        item.setTitlekey(titleKey);
        item.setBid(bid);
        int i = itemDao.insert(item);
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", i == 0 ? "添加失败" : "添加成功");
        return json.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "rss/list.do", method = RequestMethod.GET)
    public String getAnimationRssList(String type) {
        JSONObject json = new JSONObject();
        BangumiItemExample example=new BangumiItemExample();
        example.createCriteria().andBidEqualTo(type);
        json.put("code", 0);
        json.put("msg", "ok");
        json.put("data", JSONArray.toJSON(itemDao.selectByExample(example)));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "rss/del.do", method = RequestMethod.POST)
    public String delAnimation(int id){
        int i=itemDao.deleteByPrimaryKey(id);
        JSONObject json=new JSONObject();
        json.put("code", 0);
        json.put("msg", i == 0 ? "删除失败" : "删除成功");
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "client/get.do", method = RequestMethod.GET)
    public String clientApi(){
        AnimationData animationData=new AnimationData();
        List<BangumiItem> list=itemDao.selectByExample(new BangumiItemExample());
        for (BangumiItem item : list) {
            item.setCategories(animationData.nameToValue(item.getCategories(),false)+"");
            item.setAuthor(animationData.nameToValue(item.getAuthor(),true)+"");
        }
        JSONArray array= (JSONArray) JSONArray.toJSON(list);
        return array.toJSONString();
    }
}
