package com.tax.token.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tax.token.constants.TokenConstants;
import com.tax.token.model.CleanCookieVo;
import com.tax.token.model.CookieReq;
import com.yxcheng.token.utils.*;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CleanCookieUtil {

    private static final Logger logger = LoggerFactory.getLogger(CleanCookieUtil.class);

    public static CleanCookieVo cleanCookie(String uuIdFlag, CookieReq cookieReq, String type)  throws IOException, HttpException {
        CleanCookieVo cleanCookieVo = new CleanCookieVo();
        String taxno = cookieReq.getTaxno();
        JSONObject json = new JSONObject();
        json.put("userCode", cookieReq.getUserCode());
        json.put("type", type);
        json.put("requestId", uuIdFlag);

        if(type.equals("10")) {
            json.put("dqdm", cookieReq.getZoneCode());
            json.put("taxno", cookieReq.getTaxno());
            if(StringUtil.isNotEmpty(cookieReq.getSite())) {
                json.put("site", cookieReq.getSite());
            }
        } else {
            JSONArray jsonArray = new JSONArray();
            if (type.equals("3")) {
                jsonArray.add(cookieReq.getZoneCode() + ":" + cookieReq.getUserInfo());
            } else {
                jsonArray.add(cookieReq.getZoneCode() + ":" + taxno + ":" + cookieReq.getUserInfo());
            }
            json.put("keyList", jsonArray);
        }
        if(null!=cookieReq.getUserInfoJson()) {
            JSONObject loginInfoJson = JSON.parseObject(JSON.toJSONString(cookieReq.getUserInfoJson()));
            loginInfoJson.put("taxno", cookieReq.getTaxno());
            loginInfoJson.put("dqdm", cookieReq.getZoneCode());
            json.put("loginInfoJson", loginInfoJson);
        }
        String url = TokenConstants.CLEAN_COOKIE_URL;
        logger.info(uuIdFlag + " cleanCookie taxno is " + taxno + " url is " + url + ",param is " + json.toString());
        cleanCookieVo.setRequestContext(json.toString());
        
        ResponseContent ret = HttpHelper.postJsonEntity(url,json.toString());
        String resultStr = ret.getContent();

        cleanCookieVo.setResponseContext(resultStr);
        logger.info(uuIdFlag + " cleanCookie taxno is " + taxno + ",result is " + resultStr);
        if(StringUtils.isNotEmpty(resultStr)) {
            Map<String, Object> resultStrMap = JSONHelper.parserToMap(resultStr);
            cleanCookieVo.setCode(ObjectUtil.fixNull(resultStrMap.get("code")));
            cleanCookieVo.setMessage(ObjectUtil.fixNull(resultStrMap.get("message")));
        }
        return cleanCookieVo;
    }

    public static CleanCookieVo cleanCookiebyMd5(String uuIdFlag, CookieReq cookieReq, String type)  throws IOException, HttpException {
        CleanCookieVo cleanCookieVo = new CleanCookieVo();
        String taxno = cookieReq.getTaxno();
        JSONObject json = new JSONObject();
        json.put("userCode", cookieReq.getUserCode());
        json.put("type", type);
        json.put("requestId", uuIdFlag);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(cookieReq.getUserCode() + ":" + taxno + ":" + cookieReq.getUserInfo());
        json.put("keyList", jsonArray);

        String url = TokenConstants.CLEAN_COOKIE_URL;
        logger.info(uuIdFlag + " cleanCookiebyMd5 taxno is " + taxno + " url is " + url + ",param is " + json.toString());
        cleanCookieVo.setRequestContext(json.toString());

        ResponseContent ret = HttpHelper.postJsonEntity(url,json.toString());
        String resultStr = ret.getContent();

        cleanCookieVo.setResponseContext(resultStr);
        logger.info(uuIdFlag + " cleanCookiebyMd5 taxno is " + taxno + ",result is " + resultStr);
        if(StringUtils.isNotEmpty(resultStr)) {
            Map<String, Object> resultStrMap = JSONHelper.parserToMap(resultStr);
            cleanCookieVo.setCode(ObjectUtil.fixNull(resultStrMap.get("code")));
            cleanCookieVo.setMessage(ObjectUtil.fixNull(resultStrMap.get("message")));
        }
        return cleanCookieVo;
    }
}
