package com.aidex.common.gps.common;

import cn.hutool.http.HttpRequest;
import com.aidex.common.exception.SysException;
import com.aidex.common.utils.StringUtils;
import com.aidex.system.service.impl.SysUserServiceImpl;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class GpsRequest {

    private static final Logger log = LoggerFactory.getLogger(GpsRequest.class);

    @Autowired
    private GeneratorGspApi generatorGspApi;


    public JSONArray postArray(Map<String,Object> params, Integer timeOut){
        JSONObject res = postRequest(params,timeOut);
        Boolean status = res.getBoolean("success");
        Integer code = res.getInteger("errorCode");
        String msg = res.getString("errorDescribe");

        if(status){
            return res.getJSONArray("data");
        }
        throw new SysException(code,"GPS数据获取错误:"+msg);

    }

    public JSONArray postArray(Map<String,Object> params){
        JSONObject res = postRequest(params,1000);
        Boolean status = res.getBoolean("success");
        Integer code = res.getInteger("errorCode");
        String msg = res.getString("errorDescribe");

        if(status){
            return res.getJSONArray("data");
        }
        throw new SysException(code,"GPS数据获取错误:"+msg);

    }


    public JSONObject post(Map<String,Object> params, Integer timeOut){
        JSONObject res = postRequest(params,timeOut);
        Boolean status = res.getBoolean("success");
        Integer code = res.getInteger("errorCode");
        String msg = res.getString("errorDescribe");

        if(status){
            return res.getJSONObject("data");
        }
        throw new SysException(code,"GPS数据获取错误:"+msg);

    }

    public JSONObject post(Map<String,Object> params){
        JSONObject res = postRequest(params,1000);
        Boolean status = res.getBoolean("success");
        Integer code = res.getInteger("errorCode");
        String msg = res.getString("errorDescribe");

        if(status){
            return res.getJSONObject("data");
        }
        throw new SysException(code,"GPS数据获取错误:"+msg);

    }

    public JSONArray getArray(Map<String,Object> params, Integer timeOut){
        JSONObject res = postRequest(params,timeOut);
        Boolean status = res.getBoolean("success");
        Integer code = res.getInteger("errorCode");
        String msg = res.getString("errorDescribe");

        if(status){
            return res.getJSONArray("data");
        }
        throw new SysException(code,"GPS数据获取错误:"+msg);

    }

    public JSONArray getArray(Map<String,Object> params){
        JSONObject res = postRequest(params,1000);
        Boolean status = res.getBoolean("success");
        Integer code = res.getInteger("errorCode");
        String msg = res.getString("errorDescribe");

        if(status){
            return res.getJSONArray("data");
        }
        throw new SysException(code,"GPS数据获取错误:"+msg);

    }


    public JSONObject get(Map<String,Object> params, Integer timeOut){
        JSONObject res = postRequest(params,timeOut);
        Boolean status = res.getBoolean("success");
        Integer code = res.getInteger("errorCode");
        String msg = res.getString("errorDescribe");

        if(status){
            return res.getJSONObject("data");
        }
        throw new SysException(code,"GPS数据获取错误:"+msg);

    }

    public JSONObject get(Map<String,Object> params){
        JSONObject res = postRequest(params,1000);
        Boolean status = res.getBoolean("success");
        Integer code = res.getInteger("errorCode");
        String msg = res.getString("errorDescribe");

        if(status){
            return res.getJSONObject("data");
        }
        throw new SysException(code,"GPS数据获取错误:"+msg);

    }

    private JSONObject postRequest(Map<String,Object> params, Integer timeOut){
        String api = generatorGspApi.GeneratorApi();
        String res = HttpRequest.post(api).form(params).timeout(timeOut).execute().body();
        log.info("GPS请求数据打印：请求接口："+params.get("w")+"  返回数据："+res);
        return JSON.parseObject(res);
    }

    private JSONObject getRequest(Map<String,Object> params, Integer timeOut){
        String api = generatorGspApi.GeneratorApi();
        String res = HttpRequest.get(api).form(params).timeout(timeOut).execute().body();
        log.info("GPS请求数据打印：请求接口："+params.get("w")+"  返回数据："+res);
        return JSON.parseObject(res);
    }
}
