package com.artfess.rescue.integrate.consts;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.artfess.base.exception.BaseException;
import com.artfess.rescue.base.model.*;
import com.artfess.rescue.integrate.config.YhxtConfig;
import com.artfess.rescue.integrate.enums.YhxtEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Component
public class YhxtConsts {

    @Autowired
    private YhxtConfig yhxtConfig;

    //获取token
    public String getToken() {
        Map<String, Object> map = new HashMap<>();
        map.put("account", yhxtConfig.getUserKey());
        String url = "http://" + yhxtConfig.getUrl() + ":" + yhxtConfig.getPort() +YhxtEnum.TOKEN_URL.getValue();
        try{
            String resultJson = HttpUtil.get(url, map);
            JSONObject jsonObject = JSONObject.parseObject(resultJson);
            return jsonObject.get("token").toString();
        }catch (BaseException e){
            throw new BaseException("请求token失败");
        }
    }

    //获取路线信息
    public List<BizBaseRoute> getRoute() {
        return (List<BizBaseRoute>) getDataByPath(YhxtEnum.ROUTE_URL.getValue(),BizBaseRoute.class);
    }

    //获取路段信息
    public List<BizBaseRoad> getRoad() {
        return (List<BizBaseRoad>) getDataByPath(YhxtEnum.ROAD_URL.getValue(),BizBaseRoad.class);
    }

    //获取桥梁信息
    public List<BizBaseBridge> getBridge() {
        return (List<BizBaseBridge>) getDataByPath(YhxtEnum.BRIDGE_URL.getValue(),BizBaseBridge.class);
    }

    //获取隧道信息
    public List<BizBaseTunnel> getTunnel() {
        return (List<BizBaseTunnel>) getDataByPath(YhxtEnum.TUNNEL_URL.getValue(),BizBaseTunnel.class);
    }

    //获取边坡管理
    public List<BizBaseSideSlope> getSideSlope() {
        return (List<BizBaseSideSlope>) getDataByPath(YhxtEnum.SIDE_SLOPE_URL.getValue(),BizBaseSideSlope.class);
    }

    //获取沿线设施
    public List<BizBaseRoadsideFacilities> getRoadSideFacilities() {
        return (List<BizBaseRoadsideFacilities>) getDataByPath(YhxtEnum.ROAD_SIDE_FACILITIES_URL.getValue(),BizBaseRoadsideFacilities.class);
    }

    //获取沿线设施
    public List<BizBaseCulvert> getCulvert() {
       return (List<BizBaseCulvert>) getDataByPath(YhxtEnum.CULVERT_URL.getValue(),BizBaseCulvert.class);
    }

    /**
     * 请求第三方接口并返回数据，通用方法
     * @param url
     * @param clazz
     * @return
     */
    private List<?> getDataByPath(String url, Class<?> clazz) {
        String token = getToken();
        String fullUrl = "http://" + yhxtConfig.getUrl() + ":" + yhxtConfig.getPort() + url;
            String result =  HttpRequest.get(fullUrl).header("Authorization", "Bearer " + token)
                    .timeout(-1).execute().body();
            try {
                JSONObject jsonResp = JSONObject.parseObject(result);
                if (jsonResp.getBoolean("state")) {
                    JSONArray list = jsonResp.getJSONArray("value");
                    if (list.isEmpty()) {
                        throw new BaseException("未获取到相关信息");
                    }
                    return list.toJavaList(clazz);
                } else {
                    throw new BaseException("获取数据失败");
                }
            } catch (JSONException e) {
                throw new BaseException("JSON解析错误", e);
            }
    }
}