using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MenuHandler : MonoBehaviour
{

    public Image rightClickMenuBackground;
    Button first;
    Button second;
    Button third;

    List<string> choices;

    public bool clicked = false;
    public string message = "";

    private void Awake()
    {
        first = rightClickMenuBackground.transform.GetChild(0).gameObject.GetComponent<Button>();
        second = rightClickMenuBackground.transform.GetChild(1).gameObject.GetComponent<Button>();
        third = rightClickMenuBackground.transform.GetChild(2).gameObject.GetComponent<Button>();
    }

    public void SetMenu(string interactable)
    {
        message = "";
        choices = new List<string>();
        message = "";
        switch (interactable)
        {
            case "Router":
                choices.Add("Cut Internet");
                break;
            case "Light Switch":
                choices.Add("Flicker");
                break;
            case "Clock":
                choices.Add("Turn Off");
                break;
            case "Fax Machine":
                choices.Add("This is you.");
                break;
            case "Coffee Maker":
                choices.Add("Overheat");
                choices.Add("Just Water");
                break;
            case "Water Cooler":
                choices.Add("Explode");
                break;
            case "Shredder":
                choices.Add("Whir");
                break;
            case "Microwave":
                choices.Add("Overcook");
                choices.Add("Undercook");
                break;
        }

        message += interactable;
        clicked = false;

        if (choices.Count == 1)
        {
            rightClickMenuBackground.rectTransform.sizeDelta = new Vector2(100, 40);
            first.gameObject.transform.localPosition = Vector3.zero;
            first.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text = choices[0];
            second.gameObject.SetActive(false);
            third.gameObject.SetActive(false);
        }
        else if (choices.Count == 2)
        {
            rightClickMenuBackground.rectTransform.sizeDelta = new Vector2(100, 75);
            first.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text = choices[0];
            first.transform.localPosition = new Vector3(0, 15F);
            second.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text = choices[1];
            second.transform.localPosition = new Vector3(0, -15F);
            third.gameObject.SetActive(false);
        }
        else if (choices.Count == 3)
        {
            rightClickMenuBackground.rectTransform.sizeDelta = new Vector2(100, 110);
            first.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text = choices[0];
            first.transform.position = second.transform.position + new Vector3(0, 25F);
            second.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text = choices[1];
            second.gameObject.transform.position = Vector3.zero;
            third.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text = choices[2];
            third.transform.position = second.transform.position - new Vector3(0, 25F);
        }

        first.onClick.AddListener(ClickFirstChoice);
        second.onClick.AddListener(ClickSecondChoice);
        third.onClick.AddListener(ClickThirdChoice);
    }

    void ClickFirstChoice()
    {
        message += ": " + first.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text;
        clicked = true;
        first.onClick.RemoveAllListeners();
        second.onClick.RemoveAllListeners();
        third.onClick.RemoveAllListeners();
    }

    void ClickSecondChoice()
    {
        message += ": " + second.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text;
        clicked = true;
        first.onClick.RemoveAllListeners();
        second.onClick.RemoveAllListeners();
        third.onClick.RemoveAllListeners();
    }

    void ClickThirdChoice()
    {
        message += ": " + third.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text;
        clicked = true;
        first.onClick.RemoveAllListeners();
        second.onClick.RemoveAllListeners();
        first.onClick.RemoveAllListeners();
    }
}
