package com.tzld.piaoquan.growth.common.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.growth.common.common.base.CommonResponse;
import com.tzld.piaoquan.growth.common.common.constant.MessageConstant;
import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
import com.tzld.piaoquan.growth.common.component.ProxyHttpPoolClient;
import com.tzld.piaoquan.growth.common.dao.mapper.PreSpecialSendMessageMapper;
import com.tzld.piaoquan.growth.common.model.bo.MsgResult;
import com.tzld.piaoquan.growth.common.model.po.PreSpecialSendMessage;
import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
import com.tzld.piaoquan.growth.common.service.MessageService;
import com.tzld.piaoquan.growth.common.service.WeComSendService;
import com.tzld.piaoquan.growth.common.utils.DateUtil;
import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

import static com.tzld.piaoquan.growth.common.common.constant.MessageConstant.specialStaffIdList;
import static com.tzld.piaoquan.growth.common.common.constant.WeComConstant.*;


@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private HttpPoolClient httpPoolClient;

    @Autowired
    private ProxyHttpPoolClient proxyHttpPoolClient;

    @Autowired
    private WeComAccessTokenService weComAccessTokenService;

    @Autowired
    private PreSpecialSendMessageMapper preSpecialSendMessageMapper;

    @Autowired
    private WeComSendService weComSendService;


    @Override
    public boolean pushWeComMessage(JSONObject jsonObject, Long corpId) {
        try {
            String accessToken = weComAccessTokenService.getWeComAccessToken(corpId);
            String url = POST_WE_COM_ADD_MSG_TEMPLATE
                    + "?access_token=" + accessToken;
            String s;
            if (corpId == 1L) {
                s = httpPoolClient.post(url, jsonObject.toJSONString());
            } else {
                s = proxyHttpPoolClient.post(url, jsonObject.toJSONString());
            }
            JSONObject res = JSONObject.parseObject(s);
            log.info("pushWeComMessage res={}", res);
            Integer code = res.getInteger("errcode");
            if (code != 0) {
                LarkRobotUtil.sendMessage("pushWeComMessage error" + "res:" + res.toJSONString());
            }
            return code == 0;
        } catch (IOException e) {
            LarkRobotUtil.sendMessage("pushWeComMessage error" + e.getMessage());
            log.error("pushWeComMessage error", e);
        }
        return false;
    }


    @Override
    public String getMessageText(Long staffId) {
        if (specialStaffIdList.contains(staffId)) {
            return MessageConstant.specialText;
        }
        Integer hour = DateUtil.getHourOfDay();
        if (hour < 12) {
            return MessageConstant.morningText;
        } else if(hour > 12 && hour < 18) {
            return MessageConstant.afternoonText;
        } else {
            return MessageConstant.guaranteedText;
        }
    }

    @Override
    public CommonResponse<Void> createPreSpecialAssembleSendMessage(List<PreSpecialSendMessage> preSpecialSendMessages) {
        for (PreSpecialSendMessage preSpecialSendMessage : preSpecialSendMessages) {
            preSpecialSendMessageMapper.insertSelective(preSpecialSendMessage);
        }
        return CommonResponse.success();
    }

    @Override
    public MsgResult sendAutoReplyMessage(JSONObject jsonObject, Long corpId) {
        MsgResult msgResult = new MsgResult();
        try {
            String accessToken = weComAccessTokenService.getWeComAccessToken(corpId);
            String url = POST_WE_COM_SEND_WELCOME_MSG
                    + "?access_token=" + accessToken;
            String s;
            if (corpId == 1L) {
                s = httpPoolClient.post(url, jsonObject.toJSONString());
            } else {
                s = proxyHttpPoolClient.post(url, jsonObject.toJSONString());
            }
            JSONObject res = JSONObject.parseObject(s);
            log.info("sendAutoReplyMessage res={}", res);
            Integer code = res.getInteger("errcode");
            msgResult.setErrcode(code);
            if (code != 0 && code != 41051) {
                msgResult.setErrmsg(res.getString("errmsg"));
                LarkRobotUtil.sendMessage("sendAutoReplyMessage error" + "res:" + res.toJSONString());
            }
        } catch (IOException e) {
            msgResult.setErrcode(-1);
            msgResult.setErrmsg(e.getMessage());
            LarkRobotUtil.sendMessage("sendAutoReplyMessage error" + e.getMessage());
            log.error("sendAutoReplyMessage error", e);
        }
        return msgResult;
    }

    @Override
    public boolean pushMomentMessage(JSONObject jsonObject, Long corpId) {
        try {
            String accessToken = weComAccessTokenService.getWeComAccessToken(corpId);
            String url = POST_WE_COM_ADD_MOMENT_TASK
                    + "?access_token=" + accessToken;
            String s;
            if (corpId == 1L) {
                s = httpPoolClient.post(url, jsonObject.toJSONString());
            } else {
                s = proxyHttpPoolClient.post(url, jsonObject.toJSONString());
            }
            JSONObject res = JSONObject.parseObject(s);
            log.info("pushMomentMessage res={}", res);
            Integer code = res.getInteger("errcode");
            if (code != 0) {
                LarkRobotUtil.sendMessage("pushMomentMessage error" + "res:" + res.toJSONString());
            }
            return code == 0;
        } catch (IOException e) {
            LarkRobotUtil.sendMessage("pushMomentMessage error" + e.getMessage());
            log.error("pushMomentMessage error", e);
        }
        return false;
    }

}

