using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using RosMessageTypes.Std;

public class ROSRobotResetPosServer : MonoBehaviour
{
    [SerializeField] private string serviceName = "/unity/trigger_event";
    [SerializeField] public ResetPosition target;

    void Start()
    {
        // Implement the service handler inside Unity
        ROSConnection.GetOrCreateInstance().ImplementService<TriggerRequest, TriggerResponse>(serviceName, HandleTriggerRequest);
    }

    private TriggerResponse HandleTriggerRequest(TriggerRequest request)
    {
        // Perform your Unity-side logic here (e.g., reset simulation state)
        bool wasSuccessful = TryExecuteUnityAction(); 
        string responseMessage = wasSuccessful ? "Robot's position successfully reset" : "Failed to run Unity reset action";

        // Construct and return the response
        return new TriggerResponse
        {
            success = wasSuccessful,
            message = responseMessage
        };
    }

    private bool TryExecuteUnityAction()
    {
        target.startRotation = true;
        return true; 
    }
}
