package com.cslg.ppa.service.commom;


import com.cslg.ppa.common.core.auth.WxResultStatus;
import com.cslg.ppa.common.exception.WxApiException;
import com.cslg.ppa.common.okhttp.MyCookieStore;
import com.cslg.ppa.common.utils.HttpUtils;
import com.cslg.ppa.common.utils.JsonUtils;
import com.cslg.ppa.entity.commom.Article;
import com.cslg.ppa.entity.commom.BizData;
import com.cslg.ppa.entity.commom.WxResultBody;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xiexiang
 * @Date 2024/7/10
 */
public class WeiXinApi {

    /**接口地址**/
    public static final Map<String, String> URL_MAP = new HashMap<>();

    static {
        //初始化登录cookie
        URL_MAP.put("startLogin", "https://mp.weixin.qq.com/cgi-bin/bizlogin?action=startlogin");

        //二维码图片接口
        URL_MAP.put("getqrcode", "https://mp.weixin.qq.com/cgi-bin/scanloginqrcode");

        //二维码扫描状态接口
        URL_MAP.put("qrcodeAsk", "https://mp.weixin.qq.com/cgi-bin/scanloginqrcode");

        //扫码确认后，登录接口
        URL_MAP.put("bizlogin", "https://mp.weixin.qq.com/cgi-bin/bizlogin?action=login");

        //扫码确认后，登录接口
        URL_MAP.put("searchbiz", "https://mp.weixin.qq.com/cgi-bin/searchbiz");

        //扫码确认后，登录接口
        URL_MAP.put("findListEx", "https://mp.weixin.qq.com/cgi-bin/appmsg");
    }

    /**
     * POST请求开始登录接口，初始化cookie
     * @param sessionid 时间戳，加两位随机数
     * @return
     */
    public static WxResultBody startLogin(String sessionid) {
        if (StringUtils.isBlank(sessionid)) {
            sessionid = "" + System.currentTimeMillis() + (int)(Math.random()*100);
        }

        Map<String, String> params = new HashMap<>(8);
        params.put("sessionid", sessionid);
        params.put("userlang", "zh_CN");
        params.put("redirect_url", "");
        params.put("login_type", "3");
        params.put("token", "");
        params.put("lang", "zh_CN");
        params.put("f", "json");
        params.put("ajax", "1");

        WxResultBody wxResultBody = parseWxResultBody(HttpUtils.doPost(URL_MAP.get("startLogin"), params));

        return wxResultBody;
    }

    /**
     * 获取二维码图片 字节输出流
     * @return
     */
    public static InputStream getQRCode() {
        Map<String, String> params = new HashMap<>(2);
        params.put("action", "getqrcode");
        params.put("random", System.currentTimeMillis() + "");
        InputStream inputStream = HttpUtils.doGetStream(URL_MAP.get("getqrcode"), params);

        return inputStream;
    }


    /**
     * GET 轮询二维码状态接口
     * @return
     */
    public static WxResultBody askQRCode() {
        Map<String, String> params = new HashMap<>(5);
        params.put("action", "ask");
        params.put("token", "");
        params.put("lang", "zh_CN");
        params.put("f", "json");
        params.put("ajax", "1");

        WxResultBody wxResultBody = parseWxResultBody(HttpUtils.doGet(URL_MAP.get("qrcodeAsk"), params));

        return wxResultBody;
    }

    /**
     * 扫码确认后，登录接口
     * @return
     */
    public static WxResultBody bizlogin() {
        Map<String, String> params = new HashMap<>(10);
        params.put("userlang", "zh_CN");
        params.put("redirect_url", "");
        params.put("cookie_forbidden", "0");
        params.put("cookie_cleaned", "0");
        params.put("plugin_used", "0");
        params.put("login_type", "3");
        params.put("token", "");
        params.put("lang", "zh_CN");
        params.put("f", "json");
        params.put("ajax", "1");

        WxResultBody wxResultBody = parseWxResultBody(HttpUtils.doPost(URL_MAP.get("bizlogin"), params));

        return wxResultBody;
    }


    /**
     * 搜索公众号
     * @return
     */
    public static WxResultBody<List<BizData>> searchBiz(String keyword){
        Map<String, String> params = new HashMap<>(10);
        params.put("action", "search_biz");
        params.put("begin", "0");
        params.put("count", "10");
        params.put("query", keyword);
        params.put("token", MyCookieStore.getToken());
        params.put("lang", "zh_CN");
        params.put("f", "json");
        params.put("ajax", "1");

        WxResultBody<List<BizData>> wxResultBody = parseWxResultBody(HttpUtils.doGet(URL_MAP.get("searchbiz"), params),
                new TypeReference<WxResultBody<List<BizData>>>() {}
        );

        return wxResultBody;
    }

    /**
     * 搜索公众号的文章
     * @return
     */
    public static WxResultBody<List<Article>> findExList(String fakeid){
        Map<String, String> params = new HashMap<>(10);
        params.put("action", "list_ex");
        params.put("begin", "0");
        params.put("count", "5");
        params.put("fakeid", fakeid);
        params.put("token", MyCookieStore.getToken());
//        params.put("token", "538670236");
        params.put("type", "9");
        params.put("query", "");
        params.put("lang", "zh_CN");
        params.put("f", "json");
        params.put("ajax", "1");
//        System.out.println(HttpUtils.doGet(URL_MAP.get("findListEx"), params));
        WxResultBody<List<Article>> wxResultBody = parseWxResultBody(HttpUtils.doGet(URL_MAP.get("findListEx"), params),
                new TypeReference<WxResultBody<List<Article>>>() {}
        );
        return wxResultBody;
    }

    /**
     * 搜索公众号的文章
     *
     * @return
     */
    public static WxResultBody<List<Article>> findExList(String fakeid, String token, String cookie) {
        Map<String, String> params = new HashMap<>(10);
        params.put("action", "list_ex");
        params.put("begin", "0");
        params.put("count", "5");
        params.put("fakeid", fakeid);
        params.put("token", token);
        params.put("type", "9");
//        long startTime = 1753977600L;
//        long currentTime = 1754755200L;
//        String query = String.format("createtime_start:%d&createtime_end:%d", startTime, currentTime);
//        params.put("query", query);
        params.put("query", "");
        params.put("lang", "zh_CN");
        params.put("f", "json");
        params.put("ajax", "1");
//        System.out.println(HttpUtils.doGet(URL_MAP.get("findListEx"), params));
        WxResultBody<List<Article>> wxResultBody = parseWxResultBody(HttpUtils.doGet(URL_MAP.get("findListEx"), params,cookie),
                new TypeReference<WxResultBody<List<Article>>>() {
                }
        );
        return wxResultBody;
    }


    /**
     * 转成java bean
     * @param jsonRes json结果字符串
     * @return
     */
    public static WxResultBody parseWxResultBody(String jsonRes) {
        WxResultBody wxResultBody = JsonUtils.jsonToObj(jsonRes, WxResultBody.class);

        checkIsSuccess(wxResultBody);

        return wxResultBody;
    }

    /**
     * 转成java bean
     * @param jsonRes json结果字符串
     * @return
     */
    public static <T> T parseWxResultBody(String jsonRes, TypeReference<T> typeReference) {
        T wxResultBody = JsonUtils.jsonToObj(jsonRes, typeReference);
        checkIsSuccess((WxResultBody) wxResultBody);
        return wxResultBody;
    }

    public static void checkIsSuccess(WxResultBody wxResultBody) {
        // 判断是否请求成功
        if (wxResultBody == null) {
            throw new WxApiException(WxResultStatus.FAIL_NULL_RES);
        }

        if (wxResultBody.getBase_resp().getRet() == null
                || wxResultBody.getBase_resp().getRet() != 0) {
            throw new WxApiException(WxResultStatus.FAIL_STATUS);
        }
    }
}
