package com.yutou.nas.Controllers;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.nas.mybatis.dao.MusicDataDao;
import com.yutou.nas.mybatis.dao.MusicFavoritesDao;
import com.yutou.nas.mybatis.dao.MusicFavoritesDirDao;
import com.yutou.nas.mybatis.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 收藏夹相关
 */
@Controller
@RequestMapping("/nas/music/favorite/")
public class MusicFavoritesController {
    @Resource
    MusicFavoritesDao favoritesDao;
    @Resource
    MusicFavoritesDirDao favoritesDirDao;
    @Resource
    MusicDataDao musicDataDao;

    @RequestMapping("dir/add.do")
    @ResponseBody
    public String addFavoriteDir(@RequestBody JSONObject body) {
        String favorite = body.getString("favorite");
        JSONObject json = new JSONObject();
        MusicFavoritesDirExample example = new MusicFavoritesDirExample();
        example.createCriteria().andTitleEqualTo(favorite);
        if (!favoritesDirDao.selectByExample(example).isEmpty()) {
            json.put("code", -1);
            json.put("msg", "已有该收藏夹");
        } else {
            MusicFavoritesDir favoritesDir = new MusicFavoritesDir();
            favoritesDir.setTitle(favorite);
            int ret = favoritesDirDao.insert(favoritesDir);
            json.put("code", ret == 0 ? -1 : 0);
            json.put("msg", ret == 0 ? "添加失败" : "添加成功");
        }
        return json.toJSONString();
    }

    @RequestMapping("dir/list.do")
    @ResponseBody
    public String getAllFavoriteDir() {
        JSONObject json = new JSONObject();
        json.put("code", 0);
        List<MusicFavoritesDir> list = favoritesDirDao.selectByExample(new MusicFavoritesDirExample());
        JSONArray array = new JSONArray();
        for (MusicFavoritesDir favoritesDir : list) {
            JSONObject data = new JSONObject();
            data.put("id", favoritesDir.getId());
            data.put("title", favoritesDir.getTitle());
            MusicFavoritesExample example = new MusicFavoritesExample();
            example.createCriteria().andFavoriteidEqualTo(favoritesDir.getId());
            data.put("count", favoritesDao.countByExample(example));
            array.add(data);
        }
        json.put("data", array);
        return json.toJSONString();
    }

    @RequestMapping("dir/rename.do")
    @ResponseBody
    public String renameFavoriteDir(@RequestBody JSONObject body) {
        int id = body.getInteger("id");
        String title = body.getString("title");
        JSONObject json = new JSONObject();
        MusicFavoritesDir favoritesDir = favoritesDirDao.selectByPrimaryKey(id);
        if (favoritesDir == null) {
            json.put("code", -1);
            json.put("msg", "没有该收藏夹");
        } else {
            favoritesDir.setTitle(title);
            favoritesDirDao.updateByPrimaryKey(favoritesDir);
            json.put("code", 0);
            json.put("msg", "更新成功");
        }
        return json.toJSONString();
    }

    @RequestMapping("add.do")
    @ResponseBody
    public String addFavorite(@RequestBody JSONObject body) {
        JSONObject json = new JSONObject();
        JSONArray array = body.getJSONArray("md5s");
        int fid = body.getInteger("fid");
        if (array == null || fid == -1) {
            json.put("code", -1);
            json.put("msg", "参数异常:  " + body);
            return json.toJSONString();
        }
        for (Object md5 : array) {
            MusicFavoritesExample example = new MusicFavoritesExample();
            example.createCriteria().andMusisMd5EqualTo((String) md5);
            if (favoritesDao.countByExample(example) == 0) {
                MusicFavorites favorites = new MusicFavorites();
                favorites.setMusisMd5((String) md5);
                favorites.setFavoriteid(fid);
                favorites.setSubTime(new Date());
                favoritesDao.insert(favorites);
            }
        }
        json.put("code", 0);
        json.put("msg", "添加成功");
        return json.toJSONString();
    }

    @RequestMapping("remove.do")
    @ResponseBody
    public String removeFavorite(@RequestBody JSONObject body) {
        JSONArray md5s = body.getJSONArray("data");
        JSONObject json = new JSONObject();
        for (Object md5 : md5s) {
            LinkedHashMap<String,Object> js= (LinkedHashMap<String, Object>) md5;
            MusicFavoritesExample example = new MusicFavoritesExample();
            example.createCriteria().andMusisMd5EqualTo((String) js.get("md5"))
                    .andFavoriteidEqualTo((Integer) js.get("fid"));
            favoritesDao.deleteByExample(example);
        }
        json.put("code", 0);
        json.put("msg", "已从收藏夹中移除");
        return json.toJSONString();
    }

    @RequestMapping("get.do")
    @ResponseBody
    public String getFavorite(@RequestBody JSONObject body) {
        int fid = body.getInteger("fid");
        JSONObject json = new JSONObject();
        try {
            MusicFavoritesExample example = new MusicFavoritesExample();
            example.createCriteria().andFavoriteidEqualTo(fid);
            List<MusicFavorites> list = favoritesDao.selectByExample(example);
            List<MusicData> dataList = new ArrayList<>();
            for (MusicFavorites favorites : list) {
                MusicDataExample dataExample = new MusicDataExample();
                dataExample.createCriteria().andMd5EqualTo(favorites.getMusisMd5());
                try {
                    dataList.add(musicDataDao.selectByExample(dataExample).get(0));
                } catch (Exception ignored) {
                }
            }
            list.clear();
            json.put("code", 0);
            json.put("data", JSONArray.toJSON(dataList));
        } catch (Exception e) {
            e.printStackTrace();
            json.put("code", -1);
            json.put("msg", e.getLocalizedMessage());
            json.put("data", new ArrayList<>());
        }
        return json.toJSONString();
    }
}
