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 class HeartBeatResponse extends Vehicle2Message {

    private LocalDateTime dateTime;

    /**
     * 换电站工作状态: 1 待机, 2换电
     */
    private byte stationStatus;


    /**
     * 当前站控换电流程
     */
    private byte swapFlow;


    public HeartBeatResponse() {
        super((byte) 0x04);
    }

    @Override
    public int getProtocolLength() {
        return 33; // 24 + 9
    }

    @Override
    protected void encodeInternal(ByteBuf byteBuf) {
        if(this.dateTime == null) {
            this.dateTime = LocalDateTime.now();
        }
        byteBuf.writeBytes(ByteBufUtil.dateTime2bytes(dateTime));
        byteBuf.writeByte(stationStatus);
//        byteBuf.writeByte(swapFlow);
    }

    @Override
    protected void decodeInternal(ByteBuf byteBuf) {
        byte[] bytes = new byte[ByteBufUtil.VEHICLE_DATE_LENGTH];
        byteBuf.readBytes(bytes);
        this.dateTime = ByteBufUtil.bytes2DateTime(bytes);
        stationStatus = byteBuf.readByte();
//        swapFlow = byteBuf.readByte();
    }
}
