package com.mortals.xhx.busiz.handler.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.xhx.busiz.handler.BaseReqHandler;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.daemon.applicationservice.model.HcpReq;
import dorkbox.notify.Notify;
import dorkbox.notify.Pos;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * p
 */
@Service
@Slf4j
public class BusizAssessmentReqHandler extends BaseReqHandler<String, Object> {


    @Value("${config.hcpurl:http://127.0.0.1:8090/inter/hcpapi/hcpGrabEvaluate}")
    private String hcpUrl;


    @Value("${configPath:/tmp/mid.prop}")
    private String confPath;

    @Override
    protected ApiResp<Object> handle(String request) throws AppException {
        //todo
        Map<String, String> params = JSONObject.parseObject(request, new TypeReference<Map<String, String>>() {
        });
        StringBuilder url = new StringBuilder();

        for (Map.Entry<String, String> entry : params.entrySet()) {
            String key = entry.getKey();
            if (key.contains("http")) {
                String replaceStr = StrUtil.replaceFirst(entry.getKey(), "http", "https");
                url.append(replaceStr);
                url.append("=");
                url.append(entry.getValue());
            }
        }
        if (params.containsKey("affairCode") && !ObjectUtils.isEmpty(params.get("affairCode"))) {
            url.append("&");
            url.append("affairCode");
            url.append("=");
            url.append(params.get("affairCode"));
        }

        if (params.containsKey("shardKey") && !ObjectUtils.isEmpty(params.get("shardKey"))) {
            url.append("&");
            url.append("shardKey");
            url.append("=");
            url.append(params.get("shardKey"));
        }
        log.info("url:{}", url.toString());
        sendHttpMessage(hcpUrl, url.toString());

        return ApiResp.ok();
    }

    @Override
    protected void validData(String request) throws IllegalArgumentException {

    }


    private void sendHttpMessage(String url, String hcpUrl) {
        if (url == null) {
            log.info("发送地址为空！");
            return;
        }
        //读取窗口id
        boolean exist = FileUtil.exist(confPath);
        if (!exist) {
            log.info("配置文件不存在,返回!");
            return;
        }
        String json = FileUtil.readString(confPath, Charset.forName("UTF-8"));
        boolean jsonBoolean = JSONValidator.from(json).validate();
        if (!jsonBoolean) {
            log.info("配置文件格式错误,返回!:{}", json);
            return;
        }
        HcpReq hcpReq = JSON.parseObject(json, HcpReq.class);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("url", hcpUrl);
        paramMap.put("windowid", hcpReq.getWindowId());
        log.info("req url:{},body:{}", url, JSON.toJSONString(paramMap));
        String response = HttpUtil.post(url, paramMap);
        log.info("response :" + response);
        Rest<Void> rest = JSON.parseObject(response, new TypeReference<Rest<Void>>() {
        });
    }
}
