package com.gic.haoban.manage.service.service.message;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.GICMQClientUtil;
import com.gic.haoban.manage.api.enums.NoticeMessageTypeEnum;
import com.gic.haoban.manage.api.util.notify.NoticeMessageUtil;
import com.gic.message.center.api.subscribe.model.NoticeMessageForm;
import com.gic.mq.sdk.GicMQClient;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * @description: 企业微信通知消息发送处理类
 * @author: 巴弗
 * @createDate: 2023/4/21 14:03
 */
@Service
public class NoticeMessageHandler {
    private static final Logger logger = getLogger(NoticeMessageHandler.class);

    /**
     * 发送小程序消息
     */
    public void sendMessage(NoticeMessageForm noticeMessageForm){
        sendMessage(noticeMessageForm, false);
    }

    public void sendMessage(NoticeMessageForm noticeMessageForm,boolean delay){
        logger.info("消息中心发送消息实体:{}", JSONObject.toJSONString(noticeMessageForm));

        if (ObjectUtil.isEmpty(noticeMessageForm.getMessageCode()) || ObjectUtil.isEmpty(noticeMessageForm.getMqRouterCode())) {
            logger.info("消息中心发送消息实体 messageCode 或 mqRouterCode 不存在");
        }
        GicMQClient client = GICMQClientUtil.getClientInstance();
        try {
            client.sendMessage(noticeMessageForm.getMqRouterCode(), JSONObject.toJSONString(noticeMessageForm), sendDelayValue(delay));
        } catch (Exception e) {
            logger.error("消息中心发送消息实体发送异常 ", e);
        }
    }

    public void sendMessage(NoticeMessageForm noticeMessageForm,int delay){
        logger.info("消息中心发送消息实体:{}", JSONObject.toJSONString(noticeMessageForm));

        if (ObjectUtil.isEmpty(noticeMessageForm.getMessageCode()) || ObjectUtil.isEmpty(noticeMessageForm.getMqRouterCode())) {
            logger.info("消息中心发送消息实体 messageCode 或 mqRouterCode 不存在");
        }
        GicMQClient client = GICMQClientUtil.getClientInstance();
        try {
            client.sendMessage(noticeMessageForm.getMqRouterCode(), JSONObject.toJSONString(noticeMessageForm), delay);
        } catch (Exception e) {
            logger.error("消息中心发送消息实体发送异常 ", e);
        }
    }

    private int sendDelayValue(boolean delay) {
        if (!delay) {
            return 0;
        }
        LocalDateTime currentDateTime = LocalDateTime.now();

        // 设置当天的8点
        LocalDateTime dateTime8AM = LocalDateTime.of(currentDateTime.toLocalDate(), LocalTime.of(8, 0, 0));

        // 设置当天的22点
        LocalDateTime dateTime10PM = LocalDateTime.of(currentDateTime.toLocalDate(), LocalTime.of(22, 0, 0));

        // 设置明天的8点
        LocalDateTime nextDay8AM = dateTime8AM.plusDays(1);

        long secondsDiff;
        if (currentDateTime.isAfter(dateTime8AM) && currentDateTime.isBefore(dateTime10PM)) {
            secondsDiff = 0;
        } else if (currentDateTime.isAfter(dateTime10PM) && currentDateTime.isBefore(nextDay8AM)) {
            secondsDiff = dateTime10PM.until(nextDay8AM, ChronoUnit.SECONDS);
        } else {
            secondsDiff = currentDateTime.until(dateTime8AM, ChronoUnit.SECONDS);
        }

        return (int) secondsDiff;
    }
}
