package com.example.fitbot.util.processing;

import java.util.Objects;

public class MotionData {

    // Data of the motion sensor
    public Vector3 acceleration, rotation;

    // Delimiter for the data received from the motion sensor
    private static final String DATA_DELIMITER = ";";

    /**
     * Constructor for the MotionData class.
     *
     * @param accelerationX The acceleration in the X axis in m/s^2.
     * @param accelerationY The acceleration in the Y axis in m/s^2.
     * @param accelerationZ The acceleration in the Z axis in m/s^2.
     * @param rotationX     The rotation in the X axis in degrees.
     * @param rotationY     The rotation in the Y axis in degrees.
     * @param rotationZ     The rotation in the Z axis in degrees.
     */
    public MotionData(double accelerationX, double accelerationY, double accelerationZ, double rotationX, double rotationY, double rotationZ) {
        this.acceleration = new Vector3(accelerationX, accelerationY, accelerationZ);
        this.rotation = new Vector3(rotationX, rotationY, rotationZ);
    }

    /**
     * Constructor for the MotionData class.
     *
     * @param acceleration The acceleration vector in m/s^2.
     * @param rotation     The rotation vector in degrees.
     */
    public MotionData(Vector3 acceleration, Vector3 rotation) {
        this.acceleration = acceleration;
        this.rotation = rotation;
    }

    /**
     * Function for decoding a string into a MotionData object.
     * This string must contain the data of the motion sensor
     * separated by the delimiter. (;)
     *
     * @param data The string containing the data of the motion sensor.
     * @return An instance of MotionData.
     */
    public static MotionData decode(String data) {
        Objects.requireNonNull(data); // Ensure data is not null

        String[] parts = data.split(DATA_DELIMITER);
        if (parts.length != 6)
            return null;

        return new MotionData(
                Double.parseDouble(parts[0]),
                Double.parseDouble(parts[1]),
                Double.parseDouble(parts[2]),
                Double.parseDouble(parts[3]),
                Double.parseDouble(parts[4]),
                Double.parseDouble(parts[5])
        );
    }
}
