package com.tzld.piaoquan.api.service.wecom.thirdparty.impl;

import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.api.common.Constant;
import com.tzld.piaoquan.api.common.enums.wecom.WeComThirdPartyCallBackTypeEnum;
import com.tzld.piaoquan.api.component.WeComThirdPartyApiClient;
import com.tzld.piaoquan.api.model.param.wecom.thirdpart.UuidRequest;
import com.tzld.piaoquan.api.model.param.wecom.thirdpart.WeComThirdPartyCallBackParam;
import com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComStaff;
import com.tzld.piaoquan.api.service.wecom.thirdparty.WeComThirdPartyCallBackService;
import com.tzld.piaoquan.api.service.wecom.thirdparty.WeComThirdPartyService;
import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
import com.tzld.piaoquan.growth.common.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class WeComThirdPartyCallBackServiceImpl implements WeComThirdPartyCallBackService {

    @Autowired
    private WeComThirdPartyApiClient apiClient;
    @Autowired
    private WeComThirdPartyService weComThirdPartyService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void handleCallback(WeComThirdPartyCallBackParam param) {
        log.info("handleCallback param: {}", JSONObject.toJSONString(param));
        WeComThirdPartyCallBackTypeEnum typeEnum = WeComThirdPartyCallBackTypeEnum.from(param.getType());
        if (typeEnum == WeComThirdPartyCallBackTypeEnum.other) {
            log.info("三方平台企微回调类型未处理：type={}, json={}", param.getType(), param.getJson());
            return;
        }
        handle(param, typeEnum);
    }

    private void handle(WeComThirdPartyCallBackParam param, WeComThirdPartyCallBackTypeEnum typeEnum) {
        if (typeEnum == WeComThirdPartyCallBackTypeEnum.LOGIN_SUCCESS) {
            weComThirdPartyService.getRunClientByUuid(new UuidRequest(param.getUuid()));
            String loginStatusKey = Constant.loginStatusKey.replace("{uuid}", param.getUuid());
            redisUtils.set(loginStatusKey, String.valueOf(typeEnum.getVal()), 3600);
            return;
        } else if (typeEnum == WeComThirdPartyCallBackTypeEnum.SCAN_CODE
                || typeEnum == WeComThirdPartyCallBackTypeEnum.CONFIRM_SCAN_CODE
                || typeEnum == WeComThirdPartyCallBackTypeEnum.CANCEL_SCAN_CODE
                || typeEnum == WeComThirdPartyCallBackTypeEnum.NEED_VERIFY_CODE) {
            String loginStatusKey = Constant.loginStatusKey.replace("{uuid}", param.getUuid());
            redisUtils.set(loginStatusKey, String.valueOf(typeEnum.getVal()), 300);
            return;
        }
        JSONObject json = JSONObject.parseObject(param.getJson());
        List<Integer> disconnectCallBackTypes = getDisconnectCallBackTypes();
        if (disconnectCallBackTypes.contains(typeEnum.getVal())) {
            ThirdPartWeComStaff staff = weComThirdPartyService.getStaffByUuid(param.getUuid());
            if (staff == null) {
                return;
            }
            LarkRobotUtil.sendWeComThirdPartMessage(
                    "【账号掉线回调通知】\n" +
                            "账号名称：" + staff.getName() + "\n" +
                            "账号VID：" + staff.getThirdStaffId() + "\n" +
                            "账号UUID：" + staff.getThirdUuid() + "\n" +
                            "回调信息：" + json.toJSONString());
            return;
        }
    }

    private List<Integer> getDisconnectCallBackTypes() {
        List<Integer> result = new ArrayList<>();
        result.add(WeComThirdPartyCallBackTypeEnum.SECOND_VERIFY_NOTIFY.getVal());
        result.add(WeComThirdPartyCallBackTypeEnum.LOGIN_END.getVal());
        result.add(WeComThirdPartyCallBackTypeEnum.OTHER_DEVICE_LOGIN.getVal());
        result.add(WeComThirdPartyCallBackTypeEnum.QR_CODE_SCAN_TIMEOUT.getVal());
        result.add(WeComThirdPartyCallBackTypeEnum.EXCEPTION_DISCONNECT.getVal());
        return result;
    }
}
