using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ROS2;

public class MyGui : MonoBehaviour
{
    public SteeringLineRobot robot;
    public GameObject shadowbox;


    public string TopicName = "lfr/handbrake";

    private ROS2UnityComponent ros2Unity;
    private ROS2Node ros2Node;
    private IPublisher<std_msgs.msg.Bool> chatter_pub;
    private ISubscription<std_msgs.msg.Bool> chatter_sub;

    private void Start()
    {
        ros2Unity = GetComponent<ROS2UnityComponent>();

        if (ros2Unity.Ok())
        {
            if (ros2Node == null)
            {
                ros2Node = ros2Unity.CreateNode("ROS2LFRgui");
                chatter_pub = ros2Node.CreatePublisher<std_msgs.msg.Bool>(TopicName);
                chatter_sub = ros2Node.CreateSubscription<std_msgs.msg.Bool>(TopicName, msg => robot.handBrake = msg.Data);
            }
        }
    }


    private void OnGUI()
    {

        if (GUI.Button(new Rect(10, 20, 150, 20), "Toggle Holding Brake"))
        {
            if (robot.handBrake == true)
            {
                robot.handBrake = false;
            }
            else
            {
                robot.handBrake = true;
            }

        }

        std_msgs.msg.Bool msg = new std_msgs.msg.Bool();
        msg.Data = robot.handBrake;
        if (ros2Unity.Ok())
        {
            chatter_pub.Publish(msg);
        }

        if (GUI.Button(new Rect(10, 50, 150, 20), "Toggle Shade"))
        {
            if (shadowbox.activeSelf == false)
            {
                shadowbox.SetActive(true);
            }
            else
            {
                shadowbox.SetActive(false);
            }
        }

    }
}
