package com.sifang.modules.customer.utils;


import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.sifang.modules.customer.constant.SourceConstants;
import com.sifang.modules.customer.constant.YueXiuConstants;
import com.sifang.modules.setting.req.GrantTreeVO;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.HttpsUtils;
import com.sifang.modules.sys.util.ListUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Map;
import java.util.UUID;


public class MonitorTokenUtils {

    private static Logger logger = LoggerFactory.getLogger(MonitorTokenUtils.class);

     private static String assetReportForPersonHost = "https://www.nhhyy.com/monitor-api";


    /**
     * 获取token
     *
     * @param username 用户名
     * @return
     * @throws Exception
     */

    private static JSONObject getLvsToken( String username) {
        LinkedMultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<>();
        String urlhe = "oauth/miniapp/token"+"?username="+username+"&password=21232f297a57a5a743894a0e4a801fc3&grant_type=password";
        String url =assetReportForPersonHost+ urlhe;
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.set("Authorization","Basic c2FiZXIzOnNhYmVyM19zZWNyZXQ=");
       // httpHeaders.set("Blade-Requested-With","BladeHttpRequest");
        httpHeaders.set("Tenant-Id","000000");
        httpHeaders.set("oauthId","system");
        HttpEntity<LinkedMultiValueMap<String, String>> httpEntity = new HttpEntity<>(bodyMap, httpHeaders);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<JSONObject> accessTokenJsonOb = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JSONObject.class);
        logger.info("getLvsToken -----------:{}",accessTokenJsonOb);
        if (null != accessTokenJsonOb) {
            return accessTokenJsonOb.getBody();
        }
        return null;
    }

    /**
     * 获取token
     */
    public static String findOauthToken(){
        String pubKey="";
        JSONObject obj= getLvsToken("roleAll");
        if("200".equals(obj.getString("code"))){
            JSONObject data = obj.getJSONObject("data");
             pubKey= data.getString("access_token");
        }
        return pubKey;
    }



    public static JSONObject clientPostYXMap(String parameter, String url,String token){
        url=assetReportForPersonHost +url;
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Authorization","Basic c2FiZXIzOnNhYmVyM19zZWNyZXQ=");
        httpHeaders.set("Blade-Requested-With","BladeHttpRequest");
        httpHeaders.set("Blade-Auth","bearer "+token);
        httpHeaders.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
        HttpEntity<String> httpEntity = new HttpEntity<>(parameter, httpHeaders);
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(20 * 1000);
        requestFactory.setReadTimeout(60 * 1000);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);
        ResponseEntity<JSONObject> accessTokenJsonOb = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JSONObject.class);
        logger.info("clientPostZYMap -----------:{}",accessTokenJsonOb);
        if (null != accessTokenJsonOb) {
            return accessTokenJsonOb.getBody();
        }
        return null;
    }

    public static JSONObject clientGetYX(Map<String, Object> map, String url,String token){
        url=assetReportForPersonHost+url+"?roleIds="+map.get("roleIds");
        logger.info("clientPostYXMap====maps:{}",map);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Authorization","Basic c2FiZXIzOnNhYmVyM19zZWNyZXQ=");
        httpHeaders.set("Blade-Requested-With","BladeHttpRequest");
        httpHeaders.set("Blade-Auth","bearer "+token);
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<String> httpEntity = new HttpEntity<>(new Gson().toJson(map), httpHeaders);
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(20 * 1000);
        requestFactory.setReadTimeout(60 * 1000);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);
        ResponseEntity<JSONObject> accessTokenJsonOb = restTemplate.exchange(url, HttpMethod.GET, httpEntity, JSONObject.class);
        logger.info("clientPostZYMap -----------:{}",accessTokenJsonOb);
        if (null != accessTokenJsonOb) {
            return accessTokenJsonOb.getBody();
        }
        return null;
    }

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

        JSONObject obj =getLvsToken("roleAll");
        String pubKey="";
        if("200".equals(obj.getString("code"))){
            JSONObject data = obj.getJSONObject("data");
            pubKey= data.getString("access_token");
            System.out.println(" ---ddd  " +pubKey);
            Map<String,Object> map = Maps.newHashMap();
            map.put("roleIds","2");
            JSONObject roles =  clientGetYX(map,"blade-system/menu/roleTreeKeysHy",pubKey);

            if("200".equals(roles.getString("code"))) {
                JSONObject dataq = roles.getJSONObject("data");
                System.out.println(dataq.getString("menu"));

                Gson gson = new Gson();
                GrantTreeVO cashList=gson.fromJson(roles.getString("data"),new com.google.common.reflect.TypeToken<GrantTreeVO>(){}.getType());

                System.out.println(cashList.getMenu());
            }
            System.out.println(roles);
        }
        // System.out.println(Thread.currentThread().getContextClassLoader().getResource("conf.properties"));
    }


}
