package net.northking.cctp.upperComputer.driver.harmony.hyppium.data;

import com.google.gson.annotations.SerializedName;
import net.northking.cctp.upperComputer.driver.protocol.packet.CodecUtils;
import net.northking.cctp.upperComputer.driver.protocol.packet.ICommandData;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class UiEventFinger implements ICommandData {
    @SerializedName("X_POSI")
    private int firstTouchX;
    @SerializedName("Y_POSI")
    private int firstTouchY;
    @SerializedName("W1_ID")
    private String firstEventId;
    @SerializedName("W1_Type")
    private String firstEventType;
    @SerializedName("W1_Text")
    private String firstEventText;
    @SerializedName("W1_BOUNDS")
    private String firstEventBounds;
    @SerializedName("W1_HIER")
    private String firstEventHierarchy;
    @SerializedName("X2_POSI")
    private int lastTouchX;
    @SerializedName("Y2_POSI")
    private int lastTouchY;
    @SerializedName("W2_ID")
    private String lastEventId;
    @SerializedName("W2_Type")
    private String lastEventType;
    @SerializedName("W2_Text")
    private String lastEventText;
    @SerializedName("W2_BOUNDS")
    private String lastEventBounds;
    @SerializedName("W2_HIER")
    private String lastEventHierarchy;
    @SerializedName("LENGTH")
    private int stepLength;
    /**
     * 根据源码：应该最大值为固定40000
     */
    @SerializedName("MAX_VEL")
    private int maxVelocity;
    @SerializedName("VELO")
    private double velocity;
    @SerializedName("direction.X")
    private double directionX;
    @SerializedName("direction.Y")
    private double directionY;

    public UiEventFinger() {

    }

    public UiEventFinger(DataInput dataInput) throws IOException {
        buildFromInput(dataInput);
    }

    @Override
    public void buildFromInput(DataInput dataInput) throws IOException {
        firstTouchX = dataInput.readInt();
        firstTouchY = dataInput.readInt();
        firstEventId = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        firstEventType = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        firstEventText = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        firstEventBounds = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        firstEventHierarchy = CodecUtils.readNullable(dataInput, CodecUtils::readText);

        lastTouchX = dataInput.readInt();
        lastTouchY = dataInput.readInt();
        lastEventId = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        lastEventType = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        lastEventText = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        lastEventBounds = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        lastEventHierarchy = CodecUtils.readNullable(dataInput, CodecUtils::readText);

        stepLength = dataInput.readInt();
        maxVelocity = dataInput.readInt();
        velocity = dataInput.readDouble();
        directionX = dataInput.readDouble();
        directionY = dataInput.readDouble();
    }

    @Override
    public void encodeToOutput(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(firstTouchX);
        dataOutput.writeInt(firstTouchY);
        CodecUtils.writeNullable(dataOutput, firstEventId, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, firstEventType, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, firstEventText, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, firstEventBounds, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, firstEventHierarchy, CodecUtils::writeText);

        dataOutput.writeInt(lastTouchX);
        dataOutput.writeInt(lastTouchY);
        CodecUtils.writeNullable(dataOutput, lastEventId, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, lastEventType, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, lastEventText, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, lastEventBounds, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, lastEventHierarchy, CodecUtils::writeText);

        dataOutput.writeInt(stepLength);
        dataOutput.writeInt(maxVelocity);
        dataOutput.writeDouble(velocity);
        dataOutput.writeDouble(directionX);
        dataOutput.writeDouble(directionY);
    }
}
