package com.pwt.common.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.pwt.common.config.Config;
import com.pwt.common.constant.CacheConstants;
import com.pwt.common.exception.base.BaseException;
import com.pwt.common.utils.http.HttpClientUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author linyun
 * @version V1.0
 * @Title: CloudPlatformService
 * @Description: 云平台数据上传
 * @date 2022/12/09
 **/
public class CloudPlatformService {

    private static final long TOKEN_EXPIRE_TIME = 24 * 60 * 60 * 1000;
    private static final String TOKEN_KEY ="Authorization";
    private static final String TOKEN_HEADER ="Bearer ";

    //TODO
    //云平台接口调用前需判断站点开关和云平台开关是否都处于开启状态

    /**
     * 获取云平台token
     * @return token
     */
    private static String getToken()
    {
        String token = (String) CacheMap.getCache(CacheConstants.CLOUD_TOKEN_KEY);
        if (token != null)
        {
            return token;
        }
        Map<String, String> paramMap = new HashMap<>();
        String account = Config.getCloudAccount();
        String password = Config.getCloudPassword();
        paramMap.put("account", account);
        paramMap.put("password", password);
        String result = HttpClientUtil.postJson(Config.getCloudAddress() + "/api/login/account", JSON.toJSONString(paramMap));
        token = JSONObject.parseObject(result).getString("data");
        if (StringUtils.isBlank(token))
        {
            throw new BaseException("请检查云平台账户密码是否正确");
        }
        CacheMap.put(CacheConstants.CLOUD_TOKEN_KEY, token, TOKEN_EXPIRE_TIME);
        return token;
    }

    /**
     * 换电开始
     * @param vehicleCode 车辆编号
     * @param deviceCode 设备编号【换电机器人编号】
     * @return 结果
     */
    public static String changeStart(String vehicleCode, String deviceCode)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("vehicleCode", vehicleCode);
        param.put("deviceCode", deviceCode);
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/change/start", header, param);
    }

    /**
     * 换电结束
     * @param vehicleCode 车辆编号
     * @param deviceCode 设备编号【换电机器人编号】
     * @param downBatteryCode "换下电池编号
     * @param downBatteryLevel 换下电池电量
     * @param upBatteryCode 换上电池编号
     * @param upBatteryLevel 换上电池电量
     * @return 结果
     */
    public static String changeFinish(String vehicleCode, String deviceCode, String downBatteryCode, int downBatteryLevel, String upBatteryCode, int upBatteryLevel)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("vehicleCode", vehicleCode);
        param.put("deviceCode", deviceCode);
        param.put("downBatteryCode", downBatteryCode);
        param.put("downBatteryLevel", downBatteryLevel + "");
        param.put("upBatteryCode", upBatteryCode);
        param.put("upBatteryLevel", upBatteryLevel + "");
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/change/finish", header, param);
    }

    /**
     * 车辆充电开始
     * @param vehicleCode 车辆编号
     * @param deviceCode 设备编号【充电桩编号】
     * @return 结果
     */
    public static String chargeStart(String vehicleCode, String deviceCode)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("vehicleCode", vehicleCode);
        param.put("deviceCode", deviceCode);
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/charge/start", header, param);
    }

    /**
     * 车辆充电完成
     * @param vehicleCode 车辆编号
     * @param deviceCode 设备编号【充电桩编号】
     * @param level 充电电量
     * @return 结果
     */
    public static String chargeFinish(String vehicleCode, String deviceCode, int level)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("vehicleCode", vehicleCode);
        param.put("deviceCode", deviceCode);
        param.put("level", level + "");
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/charge/finish", header, param);
    }

    /**
     * 机组充电开始
     * @param batteryCode 电池编号
     * @param deviceCode 设备编号【充电机组编号】
     * @return 结果
     */
    public static String powerStart(String batteryCode, String deviceCode)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("batteryCode", batteryCode);
        param.put("deviceCode", deviceCode);
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/power/start", header, param);
    }

    /**
     * 机组充电完成
     * @param batteryCode 电池编号
     * @param deviceCode 设备编号【充电机组编号】
     * @param level 充电电
     * @return 结果
     */
    public static String powerFinish(String batteryCode, String deviceCode, int level)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("batteryCode", batteryCode);
        param.put("deviceCode", deviceCode);
        param.put("level", level + "");
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/power/finish", header, param);
    }

    /**
     * 电站告警开始
     * @param deviceCode 设备编号
     * @param faultCode 故障代码
     * @param grade 故障等级
     * @return 结果
     */
    public static String alarmStart(String deviceCode, int faultCode, int grade)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("deviceCode", deviceCode);
        param.put("faultCode", faultCode + "");
        param.put("grade", grade + "");
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/alarm/start", header, param);
    }

    /**
     * 电站告警完成
     * @param deviceCode 设备编号
     * @param faultCode 故障代码
     * @return 结果
     */
    public static String alarmFinish(String deviceCode, int faultCode)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("deviceCode", deviceCode);
        param.put("faultCode", faultCode + "");
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/alarm/finish", header, param);
    }

    /**
     * 电站开关状态
     * @param enabled true:开  false:关
     * @return 结果
     */
    public static String status(boolean enabled)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("status", enabled ? "enable" : "disable");
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/station/status", header, param);
    }

    /**
     * 电站排队车辆
     * @param waitNumber 排队数量
     * @return 结果
     */
    public static String wait(int waitNumber)
    {
        Map<String, String> header = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        header.put(TOKEN_KEY, TOKEN_HEADER + getToken());
        param.put("wait", waitNumber + "");
        return HttpClientUtil.post(Config.getCloudAddress() + "/api/control/station/wait", header, param);
    }
}
