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.nio.charset.StandardCharsets;

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

    // 就绪: 满足换电
    public static final int SWAP_STATUS_READY = 1;

    // 未就绪: 不满足换电
    public static final int SWAP_STATUS_NOT_READY = 2;
    // 设备故障
    public static final int SWAP_STATUS_EQUIPMENT_FAILURE= 3;

    // 换电流程启动
    public static final int SWAP_FLOW_START = 1;

    // 换电流程吊起车辆电池
    public static final int SWAP_FLOW_HOISTED_FROM_VEHICLE = 0X1;
    public static final int SWAP_FLOW_PUT_IN_CHARGER = 0X2;
    public static final int SWAP_FLOW_HOISTED_FROM_CHARGER = 0X3;
    public static final int SWAP_FLOW_HOISTED_PUT_IN_VEHICLE = 0X4;
    public static final int SWAP_FLOW_CHARGER_ROOT_RESET = 0X5;
    public static final int SWAP_FLOW_REBOT_RETURN_TO_ORIGIN = 0X6;
    public static final int SWAP_FLOW_SWAP_COMPLETE = 0X7;

    private String vin;
    private byte beat;

    /**
     * 站换换电就绪状态
     */
    private byte stationSwapStatus;

    /**
     * 站控换电流程
     */
    private byte stationSwapFlowStatus;

    public HeartBeatResponse() {
        super((byte) 0x11);
    }

    @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(beat);

        // 站控换电就绪状态 1byte
        byteBuf.writeByte(stationSwapStatus);
        // 站控换电流程 1byte
        byteBuf.writeByte(stationSwapFlowStatus);
    }

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

    @Override
    public int getProtocolLength() {
        return 0x15; //21
    }
}
