package com.lion.lionwebsite.Service;

import com.lion.lionwebsite.Dao.normal.GalleryMapper;
import com.lion.lionwebsite.Dao.normal.TagMapper;
import com.lion.lionwebsite.Domain.Gallery;
import com.lion.lionwebsite.Domain.Tag;
import com.lion.lionwebsite.Util.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class TagService {
    TagMapper tagMapper;

    GalleryMapper galleryMapper;

    public TagService(TagMapper tagMapper, GalleryMapper galleryMapper) {
        this.tagMapper = tagMapper;
        this.galleryMapper = galleryMapper;
    }

    public String createTag(String tagStr){
        Response response = Response.generateResponse();
        //查看tag是否存在
        if(tagMapper.selectTagExistByTag(tagStr) != 0){
            response.failure("该tag:" + tagStr + "已存在");
        }else{
            Tag tag = new Tag(0, tagStr, 0);
            tagMapper.insertTag(tag);
            response.success("创建tag:" + tagStr + "成功");
            response.set("tid", String.valueOf(tag.getId()));
        }

        return response.toJSONString();
    }

    public String selectAllTag(){
        Response response = Response.generateResponse();
        ArrayList<Tag> tags = tagMapper.selectAllTag();
        HashMap<Integer, Tag> tagHashMap = new HashMap<>();
        for (Tag tag : tags) {
            tagHashMap.put(tag.getId(), tag);
        }

        if(tags.isEmpty())
            response.failure("当前还没有标签");
        else
            response.success(new ObjectMapper().valueToTree(tagHashMap).toString());

        return response.toJSONString();
    }

    public String deleteTag(int tagId){
        Response response = Response.generateResponse();

        if(tagMapper.selectTagUsage(tagId) == 0){
            tagMapper.deleteTagById(tagId);
            response.success("标签删除成功");
        }else{
            response.failure("标签删除失败，还有本子引用该标签(如果还显示可以删除，请刷新)");
        }

        return response.toJSONString();
    }

    public String markTag(int gid, int tid){
        Response response = Response.generateResponse();

        Gallery gallery = galleryMapper.selectGalleryByGid(gid);
        if(gallery == null)//查看本子是否存在
            response.failure("本子不存在，无法标记标签");
        else if (tagMapper.selectTagExistById(tid) == 0)            //查看标签是否存在
            response.failure("标签不存在，无法标记标签");
        else
            if(tagMapper.selectIsMark(gid, tid) != 0)           //查看是否已有该条记录
                response.failure("当前本子已有该标签");
            else {
                if (tagMapper.markTag(gid, tid) == 1) {
                    response.success("标记本子成功");
                    tagMapper.incrTagUsage(tid);
                }
                else
                    response.failure("未知错误");
            }

        return response.toJSONString();
    }

    public String createTagAndMark(int gid, String tagString){
        Response response = Response.generateResponse();
        Gallery gallery = galleryMapper.selectGalleryByGid(gid);
        //查看tag是否存在
        if(tagMapper.selectTagExistByTag(tagString) != 0){
            response.failure("该tag:" + tagString + "已存在，请刷新再尝试");
        }else if(gallery == null) {//查看本子是否存在
            response.failure("本子不存在，取消创建标签，请刷新再尝试");
        }else{
            Tag tag = new Tag(0, tagString, 0);
            tagMapper.insertTag(tag);
            if (tagMapper.markTag(gid, tag.getId()) == 1) {
                response.success("创建标签并标记成功");
                response.set("tid", String.valueOf(tag.getId()));
                tagMapper.incrTagUsage(tag.getId());
            }
        }

        return response.toJSONString();
    }

    public String disMarkTag(int gid, int tid){
        Response response = Response.generateResponse();
        if(tagMapper.disMarkTag(gid, tid) == 0)
            response.failure("取消标记失败，可能并没有标记");
        else {
            response.success("取消标记成功");
            tagMapper.decrTagUsage(tid);
        }

        return response.toJSONString();
    }

}
