package com.tax.token.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tax.token.constants.TokenConstants;
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, Object> cleanCookie(String uuIdFlag, CookieReq cookieReq, String type)  throws IOException, HttpException {
        Map<String,Object> resultMap = new HashMap<>();
        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());
        } 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);
        }

        String url = TokenConstants.CLEAN_COOKIE_URL;
        logger.info(uuIdFlag + " taxno is " + taxno + " url is " + url + ",param is " + json.toString());
        ResponseContent ret = HttpHelper.postJsonEntity(url,json.toString());
        String resultStr = ret.getContent();
        logger.info(uuIdFlag + " taxno is " + taxno + ",result is " + resultStr);
        if(StringUtils.isNotEmpty(resultStr)) {
            Map<String, Object> resultStrMap = JSONHelper.parserToMap(resultStr);
            resultMap.put("code", ObjectUtil.fixNull(resultStrMap.get("code")));
            resultMap.put("message", ObjectUtil.fixNull(resultStrMap.get("message")));
        }
        return resultMap;
    }

}
