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

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

import java.nio.charset.StandardCharsets;

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

    private byte vehicleStatus;

    private String vin;

    private String batterySn;

    private byte batterySOC;

    /**
     * 里程数
     */
    private short mileage;

    /**
     * 运营公司
     */
    private String operatingCompany;

    /**
     * 产权所属公司
     */
    private String ownershipCompany;

    public LoginRequest() {
        super((byte) 0x20);
    }

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

        // 车辆状态 1 byte
        byteBuf.writeByte(vehicleStatus);
        // vin 17 bytes
        ByteBufUtil.WriteString(byteBuf, vin, VIN_LENGTH, "0");

        // 电池SN 24 bytes
        ByteBufUtil.WriteString(byteBuf, batterySn, BATTERY_SN_LENGTH, "0");

        // soc 1 byte
        byteBuf.writeByte(batterySOC);
        // miles 2 bytes
        byteBuf.writeShortLE(mileage);
        // 运营公司 8 bytes
        ByteBufUtil.WriteString(byteBuf, operatingCompany, COMPANY_CODE_LENGTH, "0");
        // 产权所属公司 8 bytes
        ByteBufUtil.WriteString(byteBuf, ownershipCompany, COMPANY_CODE_LENGTH, "0");
    }

    @Override
    public void decode(ByteBuf byteBuf) throws Exception {
        this.vehicleStatus = byteBuf.readByte();
        this.vin = byteBuf.readCharSequence(VIN_LENGTH, StandardCharsets.US_ASCII).toString();
        this.batterySn = byteBuf.readCharSequence(BATTERY_SN_LENGTH, StandardCharsets.US_ASCII).toString();
        this.batterySOC = byteBuf.readByte();
        this.mileage =  byteBuf.readShortLE();
        this.operatingCompany = byteBuf.readCharSequence(COMPANY_CODE_LENGTH, StandardCharsets.US_ASCII).toString();
        this.ownershipCompany = byteBuf.readCharSequence(COMPANY_CODE_LENGTH, StandardCharsets.US_ASCII).toString();
    }

    @Override
    public int getProtocolLength() {
        return 0X3E; //62
    }
}
