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

import com.pwt.netty.common.vehicle1.Vehicle1Message;
import io.netty.buffer.ByteBuf;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

import static com.pwt.netty.util.ByteBufUtil.bytes2DateTime;
import static com.pwt.netty.util.ByteBufUtil.dateTime2bytes;

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class LockRequest extends Vehicle1Message {

    private LocalDateTime dateTime;
    private byte lockCommand;

    public LockRequest() {
        super((byte) 0x14);
    }

    @Override
    public void encode(ByteBuf byteBuf) throws Exception {
        byteBuf.writeByte(type);
        if(this.dateTime == null) {
            this.dateTime = LocalDateTime.now();
        }
        // TODO 需要确认是按单个byte写还是按整个写，这个对小端有影响
        byteBuf.writeBytes(dateTime2bytes(this.dateTime));
        byteBuf.writeByte(lockCommand);
    }

    @Override
    public void decode(ByteBuf byteBuf) throws Exception {
        byte[] date = new byte[DATE_LENGTH];
        byteBuf.readBytes(date);
        this.dateTime = bytes2DateTime(date);
        this.lockCommand = byteBuf.readByte();
    }

    @Override
    public int getProtocolLength() {
        return 0xA;
    } // 10
}
