package org.enfi.common.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.jfinal.kit.Kv;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;
import com.jfinal.wxaapp.api.WxaAccessTokenApi;
import com.jfinal.wxaapp.api.WxaQrcodeApi;
import com.xkcoding.http.support.hutool.HutoolImpl;

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

/**
 * @Description:
 * @ClassName: WxaQrcodeApiPlus
 * @Author: wanghao
 * @date: 2021.11.13 20:59
 * @version: 1.0
 */
public class WxaQrcodeApiPlus extends WxaQrcodeApi {
    private static final String getWxaCodeUnLimitURL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";
    private static final Boolean check_path = false;
    private static String env_version = "release";

    public static void setEnv(Env env) {
        env_version = env.getEnv();
    }

    public static void setCheckPath(Boolean checkPath) {
        checkPath = checkPath;
    }

    public static InputStream getUnLimit(String scene, String page) {
        return getUnLimit(scene, page, 430);
    }

    public static InputStream getUnLimit(String scene, String page, int width) {
        return getUnLimit(scene, page, width, true, null);
    }

    public static InputStream getUnLimit(String scene, String page, int width, String r, String g, String b) {
        Map<String, String> lineColor = new HashMap();
        lineColor.put("r", r);
        lineColor.put("g", g);
        lineColor.put("b", b);
        return getUnLimit(scene, page, width, false, lineColor);
    }

    private static InputStream getUnLimit(String scene, String page, int width, boolean autoColor, Map<String, String> lineColor) {
        Kv kv = Kv.by("page", page)
                .set("scene", scene)
                .set("width", String.valueOf(width))
                .set("auto_color", autoColor)
                .set("line_color", lineColor)
                .set("env_version", env_version)
                .set("check_path", check_path);
        AccessTokenApi.removeAccessToken();
        String url = getWxaCodeUnLimitURL + AccessTokenApi.getAccessToken().getAccessToken();
        return HttpUtils.download(url, JsonUtils.toJson(kv));
    }

    public static InputStream getUnLimit(String scene, String page, int width, boolean autoColor, Map<String, String> lineColor, boolean isHyaline) {
        Kv kv = Kv.by("page", page)
                .set("scene", scene)
                .set("width", String.valueOf(width))
                .set("auto_color", autoColor)
                .set("line_color", lineColor)
                .set("is_hyaline", isHyaline)
                .set("env_version", env_version)
                .set("check_path", check_path);
        String url = getWxaCodeUnLimitURL + AccessTokenApi.getAccessToken().getAccessToken();
        return HttpUtils.download(url, JsonUtils.toJson(kv));
    }

    public enum Env {
        develop("develop"),//开发版
        release("release"),//正式版
        trial("trial");//体验版
        // 成员变量
        private final String env;

        // 构造方法
        Env(String env) {
            this.env = env;
        }

        public String getEnv() {
            return env;
        }
    }
}
