package com.yutou.qqbot.utlis;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;

public class XiaoMiRouter {
    private static XiaoMiRouter router;
    private final static String key = "a2ffa5c9be07488bbb04a3a47d3c5f6a";
    private String token = null;

    private XiaoMiRouter() {
    }

    public static XiaoMiRouter getInstance() {
        if (router == null) {
            router = new XiaoMiRouter();
        }
        return router;
    }

    public void setNotToken() {
        token = null;
    }

    private String getToken() {
        if (token != null) {
            return token;
        }
        String nonce = nonceCreat();
        String oldPwd = DigestUtils.sha256Hex(nonce + DigestUtils.sha256Hex("34864394" + key));
        JSONObject json = new JSONObject();
        json.put("username", "admin");
        json.put("password", oldPwd);
        json.put("logtype", 2);
        json.put("nonce", nonce);
        json = JSON.parseObject(HttpTools.http_post("http://192.168.31.1/cgi-bin/luci/api/xqsystem/login", HttpTools.toUrlParams(json).getBytes(StandardCharsets.UTF_8), 1, null));
        if (json.getInteger("code") == 0) {
            token = json.getString("token");
            return token;
        }
        return null;
    }

    private String getHomeUrl() {
        return getHomeUrl(true);
    }

    private String getHomeUrl(boolean check) {
        if (check && !checkToken()) {
            token = null;
        }
        return "http://192.168.31.1/cgi-bin/luci/;stok=" + getToken();
    }

    public String getDeviceListUrl() {
        return getHomeUrl() + "/api/misystem/devicelist";
    }

    private String nonceCreat() {
        return String.format("%s_%s_%s_%s", 0, "7c:83:34:bd:a3:9f", (int) (System.currentTimeMillis() / 1000), (int) (Math.random() * 10000));
    }

    private String getWan1StatusUrl() {
        return getHomeUrl() + "/api/xqnetwork/get_wan_status";
    }

    private String getWan2StatusUrl() {
        return getHomeUrl() + "/api/xqnetwork/pppoe_status?wan_name=WAN2";
    }

    public boolean checkToken() {
        String url = getHomeUrl(false) + "/api/misystem/messages";
        JSONObject json = JSON.parseObject(HttpTools.get(url));
        return json.getInteger("code") == 0;
    }

    public String getWan1IPAddress() {
        String url = getWan1StatusUrl();
        JSONObject json = JSON.parseObject(HttpTools.get(url));
        try {
            return json.getJSONObject("ipv4").getJSONObject("ip").getString("address");
        } catch (Exception e) {
            e.printStackTrace();
            return getIPv4();
        }
    }

    public String getWan2IPAddress() {
        String url = getWan2StatusUrl();
        JSONObject json = JSON.parseObject(HttpTools.get(url));
        try {
            return json.getJSONObject("ip").getString("address");
        } catch (Exception e) {
            e.printStackTrace();
            return getIPv4();
        }
    }

    public String getIPv4() {
        return HttpTools.get("https://4.ipw.cn/");
    }

    public static void main(String[] args) {
        System.out.println(XiaoMiRouter.getInstance().getWan1IPAddress());
        XiaoMiRouter.getInstance().clear();
        System.out.println(XiaoMiRouter.getInstance().getWan2IPAddress());
    }

    public void clear() {
        token="";
    }
}
