package com.jpsoft.shinestar.modules.common.utils;

import cn.hutool.http.HttpRequest;
import com.jpsoft.shinestar.config.ParkingConfig;
import com.jpsoft.shinestar.modules.base.entity.ParkingDevice;
import com.jpsoft.shinestar.modules.common.service.IRedisService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author 墨鱼_mo
 * @date 2019-9-17 14:43
 */
@Component
public class ParkApiUtil {

    @Autowired
    private ParkingConfig parkingConfig;

    @Autowired
    private IRedisService redisService;


    public String getXToken(ParkingDevice parkingDevice){

        String body = HttpRequest.get(parkingDevice.getIpPort()+"/ipms/subSystem/generate/token?userName=" + parkingConfig.getUserName()).execute().body();
        JSONObject jsonbody = JSONObject.fromObject(body);
        String data = jsonbody.getString("data");

        String accessToken = JSONObject.fromObject(data).getString("accessToken");
        if (StringUtils.isNotBlank(accessToken)){

            redisService.put("accessToken",parkingDevice.getKey(),accessToken,86400L);

            return accessToken;


        }
        else {
            return "";
        }
    }

    public JSONObject httpRequest(ParkingDevice  parkingDevice,String url, String accessToken,String method,HashMap map){

        if (method.equals("POST")){

           /* Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("carNum","鄂DA9883");
            dataMap.put("queryType",1);*/
            String jsonMapString = com.alibaba.fastjson.JSONObject.toJSONString(map);

            HashMap<String,String> heardMap = new HashMap<>();
            heardMap.put("accessToken",accessToken);
            heardMap.put("Content-Type","application/json;charset=UTF-8");



            String body = HttpRequest.post(parkingDevice.getIpPort()+url).addHeaders(heardMap).body(jsonMapString).execute().body();
            JSONObject jsonbody = JSONObject.fromObject(body);
            return jsonbody;






        }
        else {

            String body = HttpRequest.get(parkingDevice.getIpPort()+"/ipms/device/sluice/channel").header("accessToken",accessToken).form(map).execute().body();
            return null;
        }

    }

    public JSONObject httpRequestSecond(String url,String method,HashMap map) {

        if (method.equals("POST")) {

           /* Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("carNum","鄂DA9883");
            dataMap.put("queryType",1);*/
            String jsonMapString = com.alibaba.fastjson.JSONObject.toJSONString(map);

            HashMap<String, String> heardMap = new HashMap<>();
            heardMap.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

            String sign = Sign.parkSign(map,parkingConfig.getKey());
            map.put("sign",sign);

            if (!map.containsKey("sign")){
              //  String sign = Sign.parkSign(map,parkingConfig.getKey());
                map.put("sign",Sign.parkSign(map,parkingConfig.getKey()));
            }
           // String body = HttpRequest.post(url).addHeaders(heardMap).body(jsonMapString).execute().body();
            String body = HttpRequest.post(url).addHeaders(heardMap).form(map).execute().body();
            JSONObject jsonbody = JSONObject.fromObject(body);
            return jsonbody;


        } else {

            String body = HttpRequest.get("/ipms/device/sluice/channel").form(map).execute().body();
            return null;
        }

    }


}
