package com.yutou.tools.bangumi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.mybatis.dao.BangumiItemDao;
import com.yutou.tools.mybatis.dao.BangumiListDao;
import com.yutou.tools.mybatis.model.BangumiItem;
import com.yutou.tools.mybatis.model.BangumiItemExample;
import com.yutou.tools.mybatis.model.BangumiList;
import com.yutou.tools.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
public class AnimationController {
    @Resource
    BangumiListDao listDao;
    @Resource
    BangumiItemDao itemDao;

    @ResponseBody
    @RequestMapping(value = "/anim/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 = "/anim/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 = "/anim/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 = "/anim/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 = "/anim/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);
        item.setEnable(1);
        int i = itemDao.insert(item);
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", i == 0 ? "添加失败" : "添加成功");
        return json.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/anim/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 = "/anim/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 = "/anim/rss/edit.do",method = RequestMethod.POST)
    public JSONObject editAnimation(int id,String title, String author, String categories, String titleKey,String enable){
        JSONObject json=new JSONObject();
        BangumiItem item =itemDao.selectByPrimaryKey(id);
        if(!StringUtils.isEmpty(title)){
            item.setTitle(title);
        }
        if(!StringUtils.isEmpty(author)){
            item.setAuthor(author);
        }
        if(!StringUtils.isEmpty(categories)){
            item.setCategories(categories);
        }
        if(!StringUtils.isEmpty(titleKey)){
            item.setTitlekey(titleKey);
        }
        if(!StringUtils.isEmpty(enable)){
            item.setEnable(Integer.parseInt(enable));
        }
        int i=itemDao.updateByPrimaryKey(item);
        if(i>0){
            json.put("msg","操作成功");
        }else{
            json.put("msg","操作失败");
        }
        json.put("code",1);
        return json;
    }
    @ResponseBody
    @RequestMapping(value = "/anim/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();
    }
}
