package com.pwt.netty.common.plc;

import HslCommunication.Core.Transfer.IByteTransform;
import HslCommunication.Core.Transfer.ReverseBytesTransform;
import HslCommunication.Core.Types.IDataTransfer;
import com.pwt.Globals;
import com.pwt.common.enums.ChangePowerStep;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException;

import java.time.LocalDateTime;

public class PlcStateInfo implements IDataTransfer {

    private final IByteTransform byteTransform = new ReverseBytesTransform( );


    @Getter
    private long timestamp;

    public PlcStateInfo() {
        this.timestamp = System.currentTimeMillis();
    }

    @Getter
    private boolean[] batteryExists;


    /**
     * 换电设备x位置
     */
    @Getter
    private int xPosition;

    /**
     * 换电设备y位置
     */
    @Getter
    private int yPosition;

    /**
     * 换电设备z位置
     */
    @Getter
    private int zPosition;

    /**
     * 换电时行时间, 单位秒
     */
    @Getter
    private long chargeElapsedTime;

    /**
     * 换电步骤
     */
    @Getter
    private ChangePowerStep changePowerStep;

    /**
     *
     * 机器人状态
     * 0 正常 ，2 故障
     */
    @Getter
    private int robotState;

    @Override
    public short getReadCount() {
        return 20;
    }

    @Override
    public void ParseSource(byte[] bytes) {
          batteryExists = byteTransform.TransBool(bytes, 0, Globals.ACTUAL_HOLDER_COUNT);

//        batteryExists1 = byteTransform.TransBool(bytes, 0); // buffer[0] 的第0位
//        batteryExists2 = byteTransform.TransBool(bytes, 1);
//        batteryExists3 = byteTransform.TransBool(bytes, 2);
//        batteryExists4 = byteTransform.TransBool(bytes, 3);
//        batteryExists5 = byteTransform.TransBool(bytes, 4);
//        batteryExists6 = byteTransform.TransBool(bytes, 5);
//        batteryExists7 = byteTransform.TransBool(bytes, 6);
//        batteryExists8 = byteTransform.TransBool(bytes, 7);
//        batteryExists9 = byteTransform.TransBool(bytes, 8); // buffer[1] 的第0位
//        batteryExists10 = byteTransform.TransBool(bytes, 9); // buffer[1] 的第1位
//        batteryExists11 = byteTransform.TransBool(bytes, 10);
//        batteryExists12 = byteTransform.TransBool(bytes, 11);

        xPosition = byteTransform.TransInt32(bytes, 2); // buffer[2]
        yPosition = byteTransform.TransInt32(bytes, 6); // buffer[6] 即第7个字节开始
        zPosition = byteTransform.TransInt32(bytes, 10);
        chargeElapsedTime = byteTransform.TransUInt16(bytes, 14);
        int stepCode = byteTransform.TransUInt16(bytes, 16);
        changePowerStep = ChangePowerStep.getStepByCode(stepCode);
        robotState = byteTransform.TransInt16(bytes, 18);
    }

    @SneakyThrows
    @Override
    public byte[] ToSource() {
        throw new InvalidOperationException("not support.");
    }
}
