﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace AsmodeeDigital.Common.Samples.UI
{
    public class Popup : MonoBehaviour
    {
        [Serializable]
        public class UI
        {
            public Text Title;
            public Text Message;
            public Button[] Buttons;
            public Button CloseButton;

            public Transform Blocker;
        }

        public UI ui;

        private Action _actionOnClick;

        public virtual void Show()
        {
            _actionOnClick = null;

            if (ui.Blocker != null)
                ui.Blocker.gameObject.SetActive(true);

            if(this.gameObject != null)
                this.gameObject.SetActive(true);
        }

        public virtual void Show(string title, string message)
        {
            this.Show();

            ui.Title.text = title;
            ui.Message.text = message;
        }

        public virtual void Show(string title, string message, Action actionOnClick)
        {
            this.Show(title , message);

            _actionOnClick = actionOnClick;
        }

        public virtual void Hide()
        {
            if (ui.Blocker != null)
                ui.Blocker.gameObject.SetActive(false);

            this.gameObject.SetActive(false);
        }

        public void ActionOnClick()
        {
            if (_actionOnClick != null)
                _actionOnClick();
        }
    }
}
