package com.lion.lionwebsite.Service;

import com.lion.lionwebsite.Dao.normal.CollectMapper;
import com.lion.lionwebsite.Dao.normal.GalleryMapper;
import com.lion.lionwebsite.Dao.normal.UserMapper;
import com.lion.lionwebsite.Domain.User;
import com.lion.lionwebsite.Interceptor.TaskHandlerInterceptor;
import com.lion.lionwebsite.Util.CustomUtil;
import com.lion.lionwebsite.Util.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserServiceImpl{

    @Resource
    UserMapper userMapper;

    @Resource
    GalleryMapper galleryMapper;

    @Resource
    CollectMapper collectMapper;

    @Resource
    TaskHandlerInterceptor taskHandlerInterceptor;

    public UserServiceImpl(UserMapper userMapper, GalleryMapper galleryMapper, CollectMapper collectMapper, TaskHandlerInterceptor taskHandlerInterceptor) {
        this.userMapper = userMapper;
        this.galleryMapper = galleryMapper;
        this.collectMapper = collectMapper;
        this.taskHandlerInterceptor = taskHandlerInterceptor;
    }

    public String addAuthCode(String targetAuthCode, String people) {
        Response response = Response.generateResponse();

        try {
            userMapper.insertUser(new User(-1, targetAuthCode, people, CustomUtil.now(), true));
            taskHandlerInterceptor.updateAuthCodes();
            response.success("插入成功");
        }catch (Exception e){
            e.printStackTrace();
            response.failure("插入失败");
        }

        return response.toJSONString();
    }

    public String alterAuthCode(String targetAuthCode, String newAuthCode) {
        Response response = Response.generateResponse();

        try{
            if(userMapper.isExist(targetAuthCode) != 0) {
                userMapper.updateAuthCode(targetAuthCode, newAuthCode);
                taskHandlerInterceptor.updateAuthCodes();
                response.success("修改成功");
            } else {
                response.failure("授权码不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            response.failure("修改失败");
        }

        return response.toJSONString();
    }

    public String alterUsername(String targetAuthCode, String newUsername){
        Response response = Response.generateResponse();

        try{
            if(userMapper.isExist(targetAuthCode) != 0){
                userMapper.updateUsername(targetAuthCode, newUsername);
                response.success("修改成功");
            }else {
                response.failure("授权码不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            response.failure("修改失败");
        }

        return response.toJSONString();
    }

    public String deleteAuthCode(String targetAuthCode) {
        Response response = Response.generateResponse();

        if(userMapper.isExist(targetAuthCode) != 0)
            try{
                User user = userMapper.selectUserByAuthCode(targetAuthCode);

                //修改下载人以及取消收藏
                ArrayList<Integer> galleries = collectMapper.selectGidByCollector(user.getId());
                for(Integer gallery: galleries)
                    collectMapper.disCollect(gallery, user.getId());
                galleryMapper.updateGalleryDownloader(3, user.getId());

                userMapper.deleteUserByAuthCode(targetAuthCode);
                response.success("删除成功");
                taskHandlerInterceptor.updateAuthCodes();
            }catch (Exception e){
                e.printStackTrace();
                response.failure("删除失败");
            }

        else
            response.failure("授权码不存在");

        return response.toJSONString();
    }

    public String alterStatus(String AuthCode, boolean isEnable){
        Response response = Response.generateResponse();

        if(userMapper.isExist(AuthCode) == 1){
            int id = userMapper.selectUserByAuthCode(AuthCode).getId();
            userMapper.updateIsEnableById(id, isEnable);
            galleryMapper.updateGalleryDownloader(3, id);
            response.success("修改用户状态成功");
            taskHandlerInterceptor.updateAuthCodes();
        }
        else{
            response.failure("该用户不存在");
        }

        return response.toJSONString();
    }

    public String getAllUser() {
        Response response = Response.generateResponse();
        User[] users = userMapper.selectAllUser();

        response.success(new ObjectMapper().valueToTree(users).toString());

        return response.toJSONString();
    }

    public int getUserId(String AuthCode){
        return userMapper.selectUserByAuthCode(AuthCode).getId();
    }
}
