package com.muyu.vehicleGateway.vehicle.model;

import com.muyu.vehicleGateway.web.domain.VehicleInfo;
import com.muyu.vehicleGateway.web.util.VehicleUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * @ClassName VehicleData
 * @Description 车辆模拟数据对象
 * @Author LiHD
 * @Date 2024/9/27 22:11
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class VehicleData {

    /**
     * VIN
     */
    private String vin;

    /**
     * 行驶路线
     */
    private String drivingRoute;

    /**
     * 经度
     */
    private String longitude;

    /**
     * 维度
     */
    private String latitude;

    /**
     * 速度
     */
    private String speed;

    /**
     * 里程
     */
    private BigDecimal mileage;

    /**
     * 总电压
     */
    private String voltage;

    /**
     * 总电流
     */
    private String current;

    /**
     * 绝缘电阻
     */
    private String resistance;

    /**
     * 档位
     */
    @Builder.Default
    private String gear = "P";

    /**
     * 加速踏板行程值
     */
    private String accelerationPedal;

    /**
     * 制动踏板行程值
     */
    private String brakePedal;

    /**
     * 燃料消耗率
     */
    private String fuelConsumptionRate;

    /**
     * 电机控制器温度
     */
    private String motorControllerTemperature;

    /**
     * 电机转速
     */
    private String motorSpeed;

    /**
     * 电机转矩
     */
    private String motorTorque;

    /**
     * 电机温度
     */
    private String motorTemperature;

    /**
     * 电机电压
     */
    private String motorVoltage;

    /**
     * 电机电流
     */
    private String motorCurrent;

    /**
     * 动力电池剩余电量SOC
     */
    private BigDecimal remainingBattery;

    /**
     * 电池总容量
     */
    private BigDecimal batteryLevel;

    /**
     * 当前状态允许的最大反馈功率
     */
    private String maximumFeedbackPower;

    /**
     * 当前状态允许最大放电功率
     */
    private String maximumDischargePower;

    /**
     * BMS自检计数器
     */
    private String selfCheckCounter;

    /**
     * 动力电池充放电电流
     */
    private String totalBatteryCurrent;

    /**
     * 动力电池负载端总电压V3
     */
    private String totalBatteryVoltage;

    /**
     * 单次最大电压
     */
    private String singleBatteryMaxVoltage;

    /**
     * 单体电池最低电压
     */
    private String singleBatteryMinVoltage;

    /**
     * 单体电池最高温度
     */
    private String singleBatteryMaxTemperature;

    /**
     * 单体电池最低温度
     */
    private String singleBatteryMinTemperature;

    /**
     * 动力电池可用容量
     */
    private String availableBatteryCapacity;

    /**
     * 车辆状态
     */
    @Builder.Default
    private int vehicleStatus = 1;

    /**
     * 充电状态
     */
    @Builder.Default
    private int chargingStatus = 1;

    /**
     * 运行状态
     */
    @Builder.Default
    private int operatingStatus = 1;

    /**
     * SOC
     */
    @Builder.Default
    private int socStatus = 1;

    /**
     * 可充电储能装置工作状态
     */
    @Builder.Default
    private int chargingEnergyStorageStatus = 1;

    /**
     * 驱动电机状态
     */
    @Builder.Default
    private int driveMotorStatus = 1;

    /**
     * 定位是否有效
     */
    @Builder.Default
    private int positionStatus = 1;

    /**
     * EAS(汽车防盗系统)状态
     */
    @Builder.Default
    private int easStatus = 1;

    /**
     * PTC(电动加热器)状态
     */
    @Builder.Default
    private int ptcStatus = 1;

    /**
     * EPS(电动助力系统)状态
     */
    @Builder.Default
    private int epsStatus = 1;

    /**
     * ABS(防抱死)状态
     */
    @Builder.Default
    private int absStatus = 1;

    /**
     * MCU(电机/逆变器)状态
     */
    @Builder.Default
    private int mcuStatus = 1;

    /**
     * 动力电池加热状态
     */
    @Builder.Default
    private int heatingStatus = 1;

    /**
     * 动力电池当前状态
     */
    @Builder.Default
    private int batteryStatus = 1;

    /**
     * 动力电池保温状态
     */
    @Builder.Default
    private int batteryInsulationStatus = 1;

    /**
     * DCDC(电力交换系统)状态
     */
    @Builder.Default
    private int dcdcStatus = 1;

    /**
     * CHG(充电机)状态
     */
    @Builder.Default
    private int chgStatus = 1;

    /**
     * 车辆状态 报文
     */
    private String vehicleStatusMsg;
    /**
     * 智能硬件 报文
     */
    private String smartHardwareMsg;
    /**
     * 电池报文
     */
    private String batteryMsg;

    public String getMsg(){
        //第一位VIN
        return vin +
                // 当前时间戳
                System.currentTimeMillis() +
                //第二位经度 longitude latitude
                getValue(longitude, 11) +
                //第三位维度 longitude latitude
                getValue(latitude, 10) +
                //车速
                getValue(speed, 6) +
                //总里程
                getValue(mileage == null ? "" : mileage.toString(), 11) +
                // 总电压
                getValue(voltage, 6) +
                //总电流
                getValue(current, 5) +
                //绝缘电阻 79 - 87
                getValue(resistance, 9) +
                //档位
                (gear == null ? "D" : gear) +
                // 加速踏板行程值
                getValue(accelerationPedal, 2) +
                // 制动踏板行程值
                getValue(brakePedal, 2) +
                // 燃料消耗率
                getValue(fuelConsumptionRate, 5) +
                //电机控制器温度
                getValue(motorControllerTemperature, 6) +
                //电机转速
                getValue(motorSpeed, 5) +
                //点击转矩
                getValue(motorTorque, 4) +
                //电机温度
                getValue(motorTemperature, 6) +
                //电机电压
                getValue(motorVoltage, 5) +
                //电机电流
                getValue(motorCurrent, 8) +
                //动力电池剩余电量SOC
                getValue(remainingBattery == null ? "" : remainingBattery.toString(), 6) +
                //当前状态允许的最大反馈功率
                getValue(maximumFeedbackPower, 6) +
                //当前状态允许最大放电功率
                getValue(maximumDischargePower, 6) +
                //BMS自检计数器
                getValue(selfCheckCounter, 2) +
                //动力电池充放电电流
                getValue(totalBatteryCurrent, 5) +
                //动力电池负载端总电压V3
                getValue(totalBatteryVoltage, 6) +
                //单次最大电压
                getValue(singleBatteryMaxVoltage, 4) +
                //单体电池最低电压
                getValue(singleBatteryMinVoltage, 4) +
                //单体电池最高温度
                getValue(singleBatteryMaxTemperature, 6) +
                //单体电池最低温度
                getValue(singleBatteryMinTemperature, 6) +
                //动力电池可用容量
                getValue(availableBatteryCapacity, 6) +
                //车辆状态
                vehicleStatus +
                //充电状态
                chargingStatus +
                //运行状态
                operatingStatus +
                //SOC
                socStatus +
                //可充电储能装置工作状态
                chargingEnergyStorageStatus +
                //驱动电机状态
                driveMotorStatus +
                //定位是否有效
                positionStatus +
                //EAS
                easStatus +
                //PTC
                ptcStatus +
                //EPS
                epsStatus +
                //ABS
                absStatus +
                //MCU
                mcuStatus +
                //动力电池加热状态
                heatingStatus +
                //动力电池当前状态
                batteryStatus +
                //动力电池保温状态
                batteryInsulationStatus +
                //DCDC
                dcdcStatus +
                //CHG
                chgStatus;
    }

    public String getValue(String val , int valLength){
        if(val == null){
            val = "";
        }
        int length = val.length();
        if (length > valLength){
            return val.substring( 0 , valLength);
        }
        val = val + "0".repeat(valLength - length);
        return val;
    }

    /**
     * 汽车对象构造企业VIN
     * @param vehicleInfo 汽车对象
     * @return 汽车数据对象
     */
    public static VehicleData vehicleBuild (VehicleInfo vehicleInfo) {
        return VehicleData.builder()
                .vin(vehicleInfo.getVin())
                .gear("P")
                .remainingBattery(vehicleInfo.getRemainingBattery())
                .batteryLevel(vehicleInfo.getBatteryLevel())
                .mileage(vehicleInfo.getTotalMileage())
                .vehicleStatus(1)
                .chargingStatus(1)
                .operatingStatus(1)
                .socStatus(1)
                .chargingEnergyStorageStatus(1)
                .driveMotorStatus(1)
                .positionStatus(1)
                .easStatus(1)
                .ptcStatus(1)
                .epsStatus(1)
                .absStatus(1)
                .mcuStatus(1)
                .heatingStatus(1)
                .batteryStatus(1)
                .batteryInsulationStatus(1)
                .dcdcStatus(1)
                .chgStatus(1)
                .build();
    }

    /**
     * 模拟基础项
     */
    public void imitateBase(){
        // 总电压
        this.voltage = VehicleUtils.genValue(110, 750);
        // 总电流
        this.current = VehicleUtils.genValue(3, 50);
        // 绝缘电阻
        this.resistance = VehicleUtils.genValue(0,30000);
        // 加速踏板行程值
        this.accelerationPedal = VehicleUtils.genValue(0, 10);
        // 制动踏板行程值
        this.brakePedal = VehicleUtils.genValue(0, 10);
    }


    /**
     * 模拟电机数据
     */
    public void imitateMotor(){
        // 电机控制器温度
        this.motorControllerTemperature = VehicleUtils.genValue(0, 100);
        // 电机转速
        this.motorSpeed = VehicleUtils.genValue(0, 99999);
        // 电机转矩
        this.motorTorque = VehicleUtils.genValue(0, 1000);
        // 电机温度
        this.motorTemperature = VehicleUtils.genValue(0, 150);
        // 电机电压
        this.motorVoltage = VehicleUtils.genValue(110, 300);
        // 电机电流
        this.motorCurrent = VehicleUtils.genValue(0, 15000);
    }

    /**
     * 模拟电池包数据
     */
    public void imitateBatteryPack(){
        // 当前状态允许的最大反馈功率
        this.maximumFeedbackPower = VehicleUtils.genValue(0, 100);
        // 当前状态允许最大放电功率
        this.maximumDischargePower = VehicleUtils.genValue(0, 100);
        // BMS自检计数器
        this.selfCheckCounter = VehicleUtils.genValue(0, 15);
        // 动力电池充放电电流
        this.totalBatteryCurrent = VehicleUtils.genValue(0, 15);
        // 动力电池负载端总电压V3
        this.totalBatteryVoltage = VehicleUtils.genValue(220, 750);
        // 单体电池最高电压
        this.singleBatteryMaxVoltage = VehicleUtils.genValue(3, 5);
        // 单体电池最低电压
        this.singleBatteryMinVoltage = VehicleUtils.genValue(3, 5);
        // 单体电池最高温度
        this.singleBatteryMaxTemperature = VehicleUtils.genValue(0, 100);
        // 单体电池最低温度
        this.singleBatteryMinTemperature = VehicleUtils.genValue(0, 100);
        // 动力电池可用容量
        this.availableBatteryCapacity = VehicleUtils.genValue(0,100 );
    }
    /**
     车辆状态
     vehicleStatus;
     充电状态
     chargingStatus;
     运行状态
     operatingStatus;
     SOC
     socStatus;
     可充电储能装置工作状态
     chargingEnergyStorageStatus;
     驱动电机状态
     driveMotorStatus;
     定位是否有效
     positionStatus;
     */

    /**
     EAS(汽车防盗系统)状态
     easStatus;
     PTC(电动加热器)状态
     ptcStatus;
     EPS(电动助力系统)状态
     epsStatus;
     ABS(防抱死)状态
     absStatus;
     MCU(电机/逆变器)状态
     mcuStatus;
     */

    /**
     动力电池加热状态
     heatingStatus;
     动力电池当前状态
     batteryStatus;
     动力电池保温状态
     batteryInsulationStatus;
     DCDC(电力交换系统)状态
     dcdcStatus;
     CHG(充电机)状态
     chgStatus;
     */

}
