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

public class QuestionPanel : Panel
{
    public List<ButtonPanelModel> Answers = new List<ButtonPanelModel>();


    public QuestionPanel() { }

    public QuestionPanel(Vector3 _position, Vector2 _size, Vector3 _scale) : base(_position, _size, _scale) { }

    public override GameObject BuildPanel()
    {
        var questionPanel = base.BuildPanel();

        foreach (ButtonPanelModel answerModel in Answers)
        {
            // Render button from list
            GameObject answerPrefab = SelectButton(answerModel);

            answerPrefab.transform.SetParent(questionPanel.transform, false);
        }

        return questionPanel;
    }

/*    public void AddButton(PanelButtonStepModel _button)
    {
        Answers.Add(_button);
    }*/

    // set button actions
    public void SetAnswerButtonAction(string _buttonName, Action onClickFunction)
    {
        var panelButtonInstance = FindButtonByName(_buttonName);

        panelButtonInstance.GetComponent<Button>().onClick.AddListener(() => onClickFunction());
    }

    public void SetAnswerButtonActions(string _buttonName, UnityAction _buttonAction)
    {
        var panelButtonInstance = FindButtonByName(_buttonName);

        panelButtonInstance.GetComponent<Button>().onClick.AddListener(_buttonAction);
    }

    public void SetAnswerButtonActions(string _buttonName, UnityEvent _buttonActionsEvent)
    {
        var panelButtonInstance = FindButtonByName(_buttonName);

        _buttonActionsEvent.Invoke();
        panelButtonInstance.GetComponent<Button>().onClick.AddListener(() => _buttonActionsEvent.Invoke());
    }



    /// <summary>
    /// Function to decide which button instance create && map button model to its instance
    /// </summary>
    /// <param name="buttonModel"></param>
    private GameObject SelectButton(ButtonPanelModel buttonModel)
    {
        switch (buttonModel.Type)
        {
            case PanelButtonEnum.Base:
                {
                    return PanelComponentFactory.BuildCustomButton(buttonModel);
                }
            case PanelButtonEnum.Continue:
                {
                    return PanelComponentFactory.BuildContinueButton(buttonModel.Position, buttonModel.Size, buttonModel.Scale);
                }
            case PanelButtonEnum.Accept:
                {
                    return PanelComponentFactory.BuildAcceptButton(buttonModel.Position, buttonModel.Size, buttonModel.Scale);
                }
            case PanelButtonEnum.Reject:
                {
                    return PanelComponentFactory.BuildRejectButton(buttonModel.Position, buttonModel.Size, buttonModel.Scale);
                }
            default:
                {
                    throw new System.Exception("Invalid button!!! Button enum type exists but system dont know how to build it");
                }
        }
    }
}