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

    private LocalDateTime dateTime;

    private byte status;

    public BaseCommandResponse(byte type) {
        super(type);
    }

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

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

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