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

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(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public final class HeartBeatRequest extends Vehicle2Message {

    private LocalDateTime dateTime;

    /***
     * 锁止状态: 1 unlocked, 2 locked
     */
    private byte lockStatus;

    /**
     * 钥匙状态: 0 OFF, 1 ACC, 2 ON, 0XFF 不支持
     */
    private byte keyStatus;

    /**
     * 电池阀驱动状态：0 关闭， 1打开
     */
    private byte solenoidStatus;

    public HeartBeatRequest() {
        super((byte)(0x03));
    }

    @Override
    public int getProtocolLength() {
        return 35; // 24 + 11
    }

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

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