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

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

import java.nio.charset.StandardCharsets;

/**
 * 换电回复
 */
@Getter
@Setter
public class BatterySwapResponse extends Vehicle1Message {


    /**
     * 登录状态: 成功
     */
    public static final byte STATUS_OK = 0;


    /**
     * 登录状态: 失败
     */
    public static final byte STATUS_ERROR = 1;

    // 未知VIN
    public static final byte ERROR_UNKNOWN_VIN = 1;

    // 里程不足
    public static final byte MILES_NOT_ENOUGH= 2;
    // 车辆状态不满足
    public static final byte VEHICLE_STATUS_NOT_READY= 3;

    private String vin;

    private byte status;

    private byte errorCode;


    public BatterySwapResponse() {
        super((byte) 0x12);
    }

    @Override
    public void encode(ByteBuf byteBuf) throws Exception {
        // 命令 1byte
        byteBuf.writeByte(type);

        // vin 17 byte
        if (this.vin == null || this.vin.length() != VIN_LENGTH) {
            // 如果vin不对，发送17个0
            byteBuf.writeCharSequence("00000000000000000", StandardCharsets.US_ASCII);
        } else {
            byteBuf.writeCharSequence(this.getVin(), StandardCharsets.US_ASCII);
        }
        // 成功标识 1byte
        byteBuf.writeByte(this.status);
        // 失败原因 1byte
        byteBuf.writeByte(this.errorCode);
    }

    @Override
    public void decode(ByteBuf byteBuf) throws Exception {
        this.vin = byteBuf.readCharSequence(VIN_LENGTH, StandardCharsets.US_ASCII).toString();
        this.status = byteBuf.readByte();
        this.errorCode = byteBuf.readByte();
    }

    @Override
    public int getProtocolLength() {
        return 0x14; //22
    }
}
