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

namespace SimplifyXR
{
    public enum MultipleChoiceSelection
    {
        left,
        right
    }

    public class MultipleChoiceEventArgs
    {
        public string multipleChoiceID;
        //public int multipleChoiceSelection;
        public MultipleChoiceSelection selectedChoice;
    }

    [System.Serializable]
    public class MultipleChoiceButton
    {
        public string buttonText;
        public Color buttonColor;
    }

    [System.Serializable]
    public class MultipleChoiceMessage
    {
        public string multipleChoiceID;
        public bool requiresSubmit = true;
        public string message;
        public string messageWhenSelected;
        public MultipleChoiceButton leftButton = new MultipleChoiceButton { buttonText = "PASS", buttonColor = new Color32(0x06, 0x84,0x00, 0xFF) };
        public MultipleChoiceButton rightButton = new MultipleChoiceButton { buttonText = "FAIL", buttonColor = new Color32(0x84, 0x00,0x05, 0xFF) };
    }

    public class InstructionMultipleChoice : MonoBehaviour
    {
        public UnityEvent<bool> OnMultipleChoiceDisplayed;
        public UnityEvent<bool> OnMultipleChoiceSelected;
        public UnityEvent<string> OnMultipleChoiceSelectedLeft;
        public UnityEvent<string> OnMultipleChoiceSelectedRight;
        public UnityEvent<string> OnMultipleChoiceSubmittedLeft;
        public UnityEvent<string> OnMultipleChoiceSubmittedRight;

        private string multipleChoiceID;
        private bool hasSelected = false;
        private bool currentChoiceSelection = false;
        private bool requiresSubmit = false;

        //elememts 
        private InstructionElementRoot root;
        private Image imageLeft;
        private Image imageRight;
        private GameObject highlightLeft;
        private GameObject highlightRight;
        private GameObject choiceText;
        private GameObject choiceSelectedText;
        private TextMeshProUGUI answerTextLeft;
        private TextMeshProUGUI answerTextRight;

        public void Start()
        {
            root.GetElementAsType<Button>("Multiple Choice Button Left").onClick.AddListener(() => SelectChoice(false));
            root.GetElementAsType<Button>("Multiple Choice Button Right").onClick.AddListener(() => SelectChoice(true));

            imageLeft = root.GetElementAsType<Image>("Multiple Choice Color Left");
            imageRight = root.GetElementAsType<Image>("Multiple Choice Color Right");
            
            highlightLeft = root.GetElementAsType<GameObject>("Multiple Choice Highlight Left");
            highlightRight = root.GetElementAsType<GameObject>("Multiple Choice Highlight Right");

            answerTextLeft = root.GetElementAsType<TextMeshProUGUI>("Multiple Choice Answer Text Left");
            answerTextRight = root.GetElementAsType<TextMeshProUGUI>("Multiple Choice Answer Text Right");
          
            choiceText = root.GetElementAsType<GameObject>("Multiple Choice Text");
            choiceSelectedText = root.GetElementAsType<GameObject>("Multiple Choice Selected Text");
        }

        public void OnDestroy()
        {
            root.GetElementAsType<Button>("Multiple Choice Button Left").onClick.RemoveAllListeners();
            root.GetElementAsType<Button>("Multiple Choice Button Right").onClick.RemoveAllListeners();
        }

        public void SelectMultipleChoice(bool selection)
        {
            if (root.multipleChoice.gameObject.activeSelf) //or activeinhierarchy?
            {
                SelectChoice(selection);
            }
            else
            {
                Debug.LogError("Multiple Choice must be active when making a selection");
            }
        }

        private void SelectChoice(bool selection)
        {
            currentChoiceSelection = selection;
            hasSelected = true;

            if (selection)
            {
                ChangeImageAlpha(imageLeft, 0.5f);
                ChangeImageAlpha(imageRight, 1.0f);

                highlightLeft.SetActive(false);
                highlightRight.SetActive(true);
            }
            else
            {
                ChangeImageAlpha(imageLeft, 1.0f);
                ChangeImageAlpha(imageRight, 0.5f);

                highlightLeft.SetActive(true);
                highlightRight.SetActive(false);
            }

            choiceText.SetActive(false);
            choiceSelectedText.SetActive(true);

            if (currentChoiceSelection)
                OnMultipleChoiceSelectedRight?.Invoke(multipleChoiceID);
            else
                OnMultipleChoiceSelectedLeft?.Invoke(multipleChoiceID);

            OnMultipleChoiceSelected?.Invoke(currentChoiceSelection);
        }

        private void ChangeImageAlpha(Image image, float alpha )
        {
            var tempImageColor = image.color;
            tempImageColor.a = alpha;
            image.color = tempImageColor;
        }

        public bool RequirementsComplete()
        {
            if (requiresSubmit)
            {
                if (hasSelected)
                    return true;
                else
                    return false;
            }

            return true;
        }

        public void OnEnable()
        {
            root = GetComponentInParent<InstructionElementRoot>();
        }
      
        public void UpdateMultipleChoice(MultipleChoiceMessage message)
        {
            multipleChoiceID = message.multipleChoiceID;
            choiceText.GetComponent<TextMeshProUGUI>().text = message.message;
            choiceSelectedText.GetComponent<TextMeshProUGUI>().text = message.messageWhenSelected;

            imageLeft.color = message.leftButton.buttonColor;
            imageRight.color = message.rightButton.buttonColor;

            answerTextLeft.text = message.leftButton.buttonText;
            answerTextRight.text = message.rightButton.buttonText;

            hasSelected = false;

            //reset visuals and selection logic
            ChangeImageAlpha(imageLeft, 1.0f);
            ChangeImageAlpha(imageRight, 1.0f);

            highlightLeft.SetActive(false);
            highlightRight.SetActive(false);

            choiceText.SetActive(true);
            choiceSelectedText.SetActive(false);

            requiresSubmit = message.requiresSubmit;

            OnMultipleChoiceDisplayed?.Invoke(message.requiresSubmit);
        }

        public void SubmitMultipleChoiceSelection()
        {
            if (root.multipleChoice.gameObject.activeSelf)
            {
                if (hasSelected)
                {
                    //should we also reset hasSelected after? 
                    if (currentChoiceSelection)
                        OnMultipleChoiceSubmittedRight.Invoke(multipleChoiceID);
                    else
                        OnMultipleChoiceSubmittedLeft.Invoke(multipleChoiceID);
                }
                else
                    Debug.LogError("Cannot submit multiple choice as a selection has not been made");
            }
            else
            {

            }
        }

    }
}

