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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.GlobalVar;
import com.gic.commons.util.HttpClient;
import com.gic.haoban.manage.service.pojo.QwOpenResultBO;
import com.gic.haoban.manage.service.pojo.QwOpenResultInitBO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * https://www.showdoc.com.cn/1889765213630885/8704551724545128    密码:147258
 */
public class OpenUtils {

    private static Logger logger = LogManager.getLogger(OpenUtils.class);

    private static final String  gicCallbackHost = GlobalVar.ctxPropertiesMap.get("haoban_service_host")+"/haoban-manage3-operation-web/qywx-msg-notice" ;

    private static final String openHost= "http://47.94.7.218:8089/wxwork/%s" ;

    public static ServiceResponse<QwOpenResultInitBO> init(Long qwUserId) {
        JSONObject initJson = new JSONObject() ;
        if(null != qwUserId) {
            initJson.put("vid",qwUserId) ;
        }
        String initStr = HttpClient.sendPostJSON(String.format(openHost,"init") ,initJson.toJSONString(),"utf-8") ;
        logger.info("init={}",initStr);
        QwOpenResultBO initResult = result(initStr) ;
        if(!initResult.isOk()) {
            return ServiceResponse.failure("9999",initResult.getErrmsg()) ;
        }
        QwOpenResultInitBO initBO = JSONObject.parseObject(initResult.getData().toString(), QwOpenResultInitBO.class) ;
        String uuid = initBO.getUuid() ;
        // 设置回调
        setCallback(uuid);
        // 获取二维码
        ServiceResponse<QwOpenResultInitBO> response = getQrcode(uuid,initBO) ;
        if(!response.isSuccess()) {
            return response ;
        }
        return ServiceResponse.success(initBO) ;
    }

    public static void logout(String uuid) {
        JSONObject json = new JSONObject() ;
        json.put("uuid",uuid) ;
        String callbackJson = HttpClient.sendPostJSON(String.format(openHost,"LoginOut"),json.toJSONString(),"utf-8") ;
        logger.info("退出={},uuid={}",callbackJson,uuid);
    }

    private static void setCallback(String uuid) {
        JSONObject json = new JSONObject() ;
        json.put("uuid",uuid) ;
        json.put("url",gicCallbackHost) ;
        String callbackJson = HttpClient.sendPostJSON(String.format(openHost,"SetCallbackUrl"),json.toJSONString(),"utf-8") ;
        logger.info("设置回调={},uuid={}",callbackJson,uuid);
    }

    private static ServiceResponse<QwOpenResultInitBO> getQrcode(String uuid , QwOpenResultInitBO initBO) {
        JSONObject json = new JSONObject() ;
        json.put("uuid",uuid) ;
        String backJson = HttpClient.sendPostJSON(String.format(openHost,"getQrCode"),json.toJSONString(),"utf-8") ;
        logger.info("获取qrcode={},uuid={}",backJson,uuid);
        QwOpenResultBO initResult = result(backJson) ;
        if(!initResult.isOk()) {
            return ServiceResponse.failure("9999",initResult.getErrmsg()) ;
        }
        QwOpenResultInitBO backBO = JSONObject.parseObject(initResult.getData().toString(), QwOpenResultInitBO.class) ;
        initBO.setQrcode(backBO.getQrcode());
        initBO.setKey(backBO.getKey());
        return ServiceResponse.success() ;
    }

    private static QwOpenResultBO result(String json) {
        QwOpenResultBO bo = JSONObject.parseObject(json, QwOpenResultBO.class);
        return bo;
    }
}
