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

import com.pwt.netty.common.vehicle2.Vehicle2Message;
import com.pwt.netty.util.ByteBufUtil;
import io.netty.buffer.ByteBuf;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.time.LocalDateTime;

@Getter
@Setter
@ToString
@EqualsAndHashCode(callSuper = true)
public final class SetParamRequest extends Vehicle2Message {

    private LocalDateTime dateTime;

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

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

    /**
     * 车辆累计插枪充电量
     */
    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;


    public SetParamRequest() {
        super((byte) 0x11);
    }

    @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.writeMediumLE(totalDischarge);
        byteBuf.writeMediumLE(totalFeedback);
        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);
    }

    @Override
    protected void decodeInternal(ByteBuf byteBuf) throws Exception {
        byte[] bytes = new byte[ByteBufUtil.VEHICLE_DATE_LENGTH];
        byteBuf.readBytes(bytes);
        this.dateTime = ByteBufUtil.bytes2DateTime(bytes);
        totalDischarge = byteBuf.readUnsignedMediumLE();
        totalFeedback = 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();
    }
}
