package thyyxxk.webserver.service.socketmessage;

import cn.hutool.extra.spring.SpringUtil;
import lombok.Data;
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.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;

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

    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.success(new ArrayList<>());
        }
        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);
        }
        String code = TokenUtil.getInstance().getTokenUserId();
        List<MessageIndex> temp = dao.selectMessageIndex(code);
        resultList.addAll(temp);
        return ResultVoUtil.success(resultList);
    }

    @Data
    public static class FetchMessage {
        private String sender;
        private Long currentPage;
        private Boolean updateReadStatus = true;
    }

    public ResultVo<List<SocketMessage>> fetchAllMessages(FetchMessage data) {
        if (data.getSender().equals("00000")) {
            return ResultVoUtil.success(dao.selectAllSystemMessages());
        }
        String receiver = TokenUtil.getInstance().getTokenUserId();
        if (data.getUpdateReadStatus()) {
            getThis().updateReadStatus(data.getSender(), receiver);
        }
        return ResultVoUtil.success(dao.selectNeededMessagesPage(data.getSender(), receiver, data.getCurrentPage(), 30));
    }

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