package thyyxxk.webserver.service.socketmessage;

import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.socketmessage.SocketMessageDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.socketmessage.MessageIndex;
import thyyxxk.webserver.entity.socketmessage.SocketMessage;
import thyyxxk.webserver.entity.socketmessage.SystemMessage;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SocketMessageService {
    private final SocketMessageDao dao;
    private final RedisLikeService redis;

    @Autowired
    public SocketMessageService(SocketMessageDao dao, RedisLikeService redis) {
        this.dao = dao;
        this.redis = redis;
    }

    private SocketMessageService getThis() {
        return SpringUtil.getBean(this.getClass());
    }

    public ResultVo<List<SystemMessage>> selectSystemMessages(String date) {
        date = date.equals("latest") ? dao.selectLatestDate() : dao.selectLastDate(date);
        List<SystemMessage> list = dao.selectMessagesByDate(date);
        if (null == list || list.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "已经到顶了，无法找到更多的内容。");
        }
        return ResultVoUtil.success(list);
    }

    public ResultVo<Map<String, Object>> onPageRefresh(List<String> exclude) {
        if (exclude.isEmpty()) {
            exclude.add("0");
        }
        List<SystemMessage> systemMessages = dao.selectImportantSystemMessages(exclude);
        int unreadCount = dao.selectUnreadCount(TokenUtil.getInstance().getTokenUserId());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("systemMessage", systemMessages);
        resultMap.put("unreadCount", unreadCount);
        return ResultVoUtil.success(resultMap);
    }

    public ResultVo<List<MessageIndex>> fetchAllSenders() {
        List<MessageIndex> resultList = new ArrayList<>();
        if (dao.selectSystemMessageCount() > 0) {
            MessageIndex systm = new MessageIndex();
            systm.setSender("00000");
            systm.setSenderName("系统通知");
            systm.setUnreadCount(0);
            resultList.add(systm);
        }
        List<MessageIndex> temp = dao.selectMessageIndex(TokenUtil.getInstance().getTokenUserId());
        temp.forEach(itm -> itm.setSenderName(redis.getDeptAndEmployeeName(itm.getSender())));
        resultList.addAll(temp);
        return ResultVoUtil.success(resultList);
    }

    public ResultVo<List<SocketMessage>> fetchAllMessages(String sender) {
        if (sender.equals("00000")) {
            return ResultVoUtil.success(dao.selectAllSystemMessages());
        }
        String receiver = TokenUtil.getInstance().getTokenUserId();
        List<SocketMessage> list = dao.selectNeededMessages(sender, receiver);
        getThis().updateReadStatus(sender, receiver);
        return ResultVoUtil.success(list);
    }

    @Async
    public void updateReadStatus(String sender, String receiver) {
        dao.updateReadStatus(sender, receiver);
    }
}
