package com.tax.token.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
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 Map<String, String> cleanTypeMap = Maps.newHashMap();
    static {
        //对外的 cleanType 1.清app 2.清电子税局 3.清app+电子税局
        //对内的 cleanType 13.清app 2.清电子税局 14.清app+电子税局
        cleanTypeMap.put("1", "13");
        cleanTypeMap.put("2", "2");
        cleanTypeMap.put("3", "14");
    }

    public static String transfarCleanType(String oldCleanType) {
        if(StringUtil.isEmpty(oldCleanType) || !cleanTypeMap.containsKey(oldCleanType)) {
            oldCleanType = "2";
        }
        return cleanTypeMap.get(oldCleanType);
    }

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

        if(cleanType.equals("10")) {
            json.put("dqdm", cookieReq.getZoneCode());
            json.put("taxno", cookieReq.getTaxno());
            if(StringUtil.isNotEmpty(cookieReq.getSite())) {
                json.put("site", cookieReq.getSite());
            }
        } else {
            if(StringUtil.isNotEmpty(cookieReq.getUserInfo())) {
                if (cleanType.equals("13") || cleanType.equals("14")) {
                    JSONObject loginInfoJson = new JSONObject();
                    loginInfoJson.put("taxno", cookieReq.getTaxno());
                    loginInfoJson.put("dqdm", cookieReq.getZoneCode());
                    loginInfoJson.put("username", cookieReq.getLoginName());
                    loginInfoJson.put("session", cookieReq.getUserInfo());
                    json.put("loginInfoJson", loginInfoJson);
                } else {
                    JSONArray jsonArray = new JSONArray();
                    if (cleanType.equals("3")) {
                        jsonArray.add(cookieReq.getZoneCode() + ":" + cookieReq.getUserInfo());
                    } else {
                        jsonArray.add(cookieReq.getZoneCode() + ":" + taxno + ":" + cookieReq.getUserInfo());
                    }
                    json.put("keyList", jsonArray);
                }
            } else {
                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.getZoneCode() + ":" + 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;
    }
}
