package com.pwt.netty.common.vehicle2.client;

import com.pwt.Globals;
import com.pwt.StationService;
import com.pwt.common.constant.LogConstants;
import com.pwt.netty.common.vehicle2.Vehicle2Message;
import com.pwt.netty.util.ByteBufUtil;
import com.pwt.netty.util.MessageUtil;
import io.netty.buffer.ByteBuf;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Getter
@Setter
@Slf4j(topic = LogConstants.VEHICLE_BIZ)
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public final class PowerSendRequest extends Vehicle2Message {

    private LocalDateTime dateTime;


    /**
     * 车辆状态
     * <p>
     *   a）车辆状态 bit0-（0-熄火 ， 1-ready），bit 1（ 档 位 0-其他，1-p 挡）， bit2（驻车制动 0- 松开，1-制动）
     * </p>
     * <br/> data type: byte
     */
    private byte vehicleStatus;


    /**
     * data type: ASCII
     */
    private String batterySn;

    /**
     * 电池soc, unit %
     * <br /> data type: u16
     */
    private int batterySOC;

    /**
     * 里程数, unit km
     * <br /> data type: u16
     */
    private int mileage;

    /**
     * 累计充电电量
     * <p>
     *  单位：kwh
     *  偏移量：0
     *  比例：1
     *  data type: unit24
     * </p>
     */
    private int totalChargeEnergy;


    /**
     * 车辆累计放电量
     */
    private int totalDischarge;

    /**
     * 回馈电量
     */
    private int totalFeedback;


    /**
     * 累计充电换电电量
     * <p>
     *  单位：kwh
     *  偏移量：0
     *  比例：1
     *  data type: unit24
     * </p>
     */
    private int totalExchangeEnergy;

    /**
     * 车辆累计插枪充电量
     */
    private int totalPlugGunCharging;

    /**
     * 综合能耗: CEC
     */
    private int  comprehensiveEnergyConsumption;

    // 本次实时
    private int currentDischarge;
    private int currentFeedback;
    private int currentPlugGunCharging;
    private int currentCEC;

    // 上次结算
    private int lastDischarge;
    private int lastFeedback;
    private int lastGunCharging;
    private int lastCEC;

    // 未结算电费电量
    private int UnsettleElectricity;

    /**
     * 电池包额定容量
     * <br/>单位：Ah
     * <br/>偏移量：0
     * <br/>数值范围：0-6553.5
     * <br/>比例：0.1
     * <br/>data type: uint16
     */
    private int batteryRatedCapacity;

    /**
     * 电池包额定电压
     * <br/>单位：V
     * <br/>偏移量：0
     * <br/>数值范围：0-6553.5
     * <br/>比例：0.1
     * <br/>data type: uint16
     */
    private int batteryRatedVoltage;


    public PowerSendRequest() {
        super((byte) 0x01);
    }

    @Override
    public int getProtocolLength() {
        return 71; // 24 + 47
    }

    @Override
    protected void encodeInternal(ByteBuf byteBuf) throws Exception {
        if(this.dateTime == null) {
            this.dateTime = LocalDateTime.now();
        }
        byteBuf.writeBytes(ByteBufUtil.dateTime2bytes(dateTime));

//        byteBuf.writeByte(vehicleStatus);
        // 电池SN 24 bytes
//        ByteBufUtil.WriteString(byteBuf, batterySn, BATTERY_SN_LENGTH, "0");
//        byteBuf.writeShortLE(batterySOC);
//        byteBuf.writeShortLE(mileage);
//        byteBuf.writeMediumLE(totalChargeEnergy);
        byteBuf.writeMediumLE(totalDischarge);
        byteBuf.writeMediumLE(totalFeedback);
//        byteBuf.writeMediumLE(totalExchangeEnergy);
        byteBuf.writeMediumLE(totalPlugGunCharging);
        byteBuf.writeMediumLE(comprehensiveEnergyConsumption);
        byteBuf.writeMediumLE(currentDischarge);
        byteBuf.writeMediumLE(currentFeedback);
        byteBuf.writeMediumLE(currentPlugGunCharging);
        byteBuf.writeMediumLE(currentCEC);
        byteBuf.writeMediumLE(lastDischarge);
        byteBuf.writeMediumLE(lastFeedback);
        byteBuf.writeMediumLE(lastGunCharging);
        byteBuf.writeMediumLE(lastCEC);
        byteBuf.writeMediumLE(UnsettleElectricity);
//        byteBuf.writeShortLE(batteryRatedCapacity);
//        byteBuf.writeShortLE(batteryRatedVoltage);
    }

    @Override
    protected void decodeInternal(ByteBuf byteBuf) throws Exception {
        if (byteBuf != null) {
            log.info("换电控制器度电数据：{}", MessageUtil.convertByteBufToString(byteBuf));
        }
        byte[] bytes = new byte[ByteBufUtil.VEHICLE_DATE_LENGTH];
        byteBuf.readBytes(bytes);
        dateTime = ByteBufUtil.bytes2DateTime(bytes);
//        vehicleStatus = byteBuf.readByte();
        batterySn = this.getVin();
        batterySOC = 46;
//        batterySn = byteBuf.readCharSequence(BATTERY_SN_LENGTH, StandardCharsets.US_ASCII).toString().replaceAll("(\0|\\s)", "");
//        batterySOC = byteBuf.readUnsignedShortLE();
//        mileage = byteBuf.readUnsignedShortLE();
//        totalChargeEnergy = byteBuf.readUnsignedMediumLE();
        totalDischarge = byteBuf.readUnsignedMediumLE();
        totalFeedback = byteBuf.readUnsignedMediumLE();
//        totalExchangeEnergy = byteBuf.readUnsignedMediumLE();
        totalPlugGunCharging = byteBuf.readUnsignedMediumLE();
        comprehensiveEnergyConsumption = byteBuf.readUnsignedMediumLE();
        currentDischarge = byteBuf.readUnsignedMediumLE();
        currentFeedback = byteBuf.readUnsignedMediumLE();
        currentPlugGunCharging = byteBuf.readUnsignedMediumLE();
        currentCEC = byteBuf.readUnsignedMediumLE();
        lastDischarge = byteBuf.readUnsignedMediumLE();
        lastFeedback = byteBuf.readUnsignedMediumLE();
        lastGunCharging = byteBuf.readUnsignedMediumLE();
        lastCEC = byteBuf.readUnsignedMediumLE();
        UnsettleElectricity = byteBuf.readUnsignedMediumLE();
//        batteryRatedCapacity = byteBuf.readShortLE();
//        batteryRatedVoltage = byteBuf.readShortLE();
        batteryRatedCapacity = 350;
    }
}
