package com.lutao.cloud.oil.typhoon;



import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.ImmutableMap;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author zhaoqiang
 * @title: DataAPIUtils.java
 * @package: org.casst.back.task.util
 * @description: 数据接口工具类
 * @created 2020年11月4日 上午2:16:39
 */
@RefreshScope
public class DataAPIUtils {

    private final static String CLIENT_ID = BusiConstant.APP_USER_NAME_01;

    /**
     * @Description: 获取请求json
     * @Param: [java.lang.String]
     * @Return: java.lang.String
     * @Author: vincent.zhao
     * @Date: 2021/12/3 17:31
     */
    public static String readJsonFile(String jsonFileName) throws IOException {
        String result = "";
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("classpath:requestjson/" + jsonFileName);

        try (InputStream is = resource.getInputStream();
             InputStreamReader isr = new InputStreamReader(is);
             BufferedReader br = new BufferedReader(isr);) {
            String data = null;
            while ((data = br.readLine()) != null) {
                result = result + data;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(result);
    }


    /**
     * @Description: 获取同步token
     * @Param: [java.lang.String, java.lang.String, java.lang.String]
     * @Return: java.lang.String
     * @Author: vincent.zhao
     * @Date: 2021/12/3 17:33
     */
    public static synchronized void getSyncTokenIP59255615() throws Exception {

        if (StringUtils.isEmpty(BusiConstant.YJBZYC_TOKEN_59_255_61_5)) {

            String tokenUrl = "http://" + BusiConstant.IP_59_255_61_5 + ":" + BusiConstant.PORT_9090 + BusiConstant.TAKEN_URL;

            String data = "client_id=" + CLIENT_ID + "&client_secret=" + BusiConstant.APP_USER_NAME_PASSWORD.get(CLIENT_ID);

            //String result = HttpUtils.sendHttpGet(tokenUrl, data, ImmutableMap.of("content-type", "application/json"));
            String result = HttpUtil.createGet(tokenUrl).header("content-type", "application/json")
                    .header("client_id", CLIENT_ID)
                    .header("client_secret", BusiConstant.APP_USER_NAME_PASSWORD.get(CLIENT_ID))
                    .execute().body();

            BusiConstant.YJBZYC_TOKEN_59_255_61_5 = JSONUtil.parseObj(result).getStr("access_token");
        }
    }

    /**
     * @Description: 获取token
     * @Param: [java.lang.String, java.lang.String, java.lang.String]
     * @Return: java.lang.String
     * @Author: vincent.zhao
     * @Date: 2021/12/3 17:33
     */
    public static synchronized void getTokenIP59255615() throws Exception {

        String tokenUrl = "http://" + BusiConstant.IP_59_255_61_5 + ":" + BusiConstant.PORT_9090 + BusiConstant.TAKEN_URL;

        String data = "client_id=" + CLIENT_ID + "&client_secret=" + BusiConstant.APP_USER_NAME_PASSWORD.get(CLIENT_ID);
        //String result = HttpUtils.sendHttpGet(tokenUrl, data, ImmutableMap.of("content-type", "application/json"));


        String result = HttpUtil.createGet(tokenUrl).header("content-type", "application/json")
                .header("client_id", CLIENT_ID)
                .header("client_secret", BusiConstant.APP_USER_NAME_PASSWORD.get(CLIENT_ID))
                .execute().body();

        BusiConstant.YJBZYC_TOKEN_59_255_61_5 = JSONUtil.parseObj(result).getStr("access_token");
    }


    /**
     * @Description: 获取同步token
     * @Param: [java.lang.String, java.lang.String, java.lang.String]
     * @Return: java.lang.String
     * @Author: vincent.zhao
     * @Date: 2021/12/3 17:33
     */
    public static synchronized void getSyncTokenIP59255616() throws Exception {

        if (StringUtils.isEmpty(BusiConstant.YJBZYC_TOKEN_59_255_61_6)) {
            String tokenUrl = "http://" + BusiConstant.IP_59_255_61_6 + ":" + BusiConstant.PORT_9090 + BusiConstant.TAKEN_URL;

            String data = "client_id=" + CLIENT_ID + "&client_secret=" + BusiConstant.APP_USER_NAME_PASSWORD.get(CLIENT_ID);
            //String result = HttpUtils.sendHttpGet(tokenUrl, data, ImmutableMap.of("content-type", "application/json"));
            String result = HttpUtil.createGet(tokenUrl).header("content-type", "application/json")
                    .header("client_id", CLIENT_ID)
                    .header("client_secret", BusiConstant.APP_USER_NAME_PASSWORD.get(CLIENT_ID))
                    .execute().body();

            String token = JSONUtil.parseObj(result).getStr("access_token");

            BusiConstant.YJBZYC_TOKEN_59_255_61_6 = token;
        }
    }

    public static void main(String[] args) throws Exception {
        getSyncTokenIP59255615();
    }

}
