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

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 LockResponse extends Vehicle1Message {

    public static final byte LOCK_STATUS_UNLOCK_SUCCESS = 0x11;
    public static final byte LOCK_STATUS_NOT_ACC_OFF = 0x12;
    public static final byte LOCK_STATUS_UNLOCK_FAILURE = 0x13;
    public static final byte LOCK_STATUS_LOCK_SUCCESS = 0x21;
    public static final byte LOCK_STATUS_LOCK_FAILURE = 0x22;
    public static final short LOCK_STATUS_VEHICLE_MISMATCH = 0xFE;
    public static final short LOCK_STATUS_ERROR = 0XFF;


    private LocalDateTime dateTime;
    private byte lockCommand;
    private byte lockStatus;

    public LockResponse() {
        super((byte)0x24);
    }

    @Override
    public void encode(ByteBuf byteBuf) throws Exception {
        byteBuf.writeByte(type);
        if(this.dateTime == null) {
            this.dateTime = LocalDateTime.now();
        }
        byteBuf.writeBytes(dateTime2bytes(dateTime));
        byteBuf.writeByte(lockCommand);
        byteBuf.writeByte(lockStatus);
    }

    @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();
        this.lockStatus = byteBuf.readByte();
    }

    @Override
    public int getProtocolLength() {
        return 0xB;
    } // 11
}
