using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

class SkillsUIManager : MonoBehaviour
{
    public const string ITEM_LOCALIZATION = "UiPanels/";
    public const string PANEL_NAME = "SkillsPanel";

    public static SkillsUIManager Instance { get; protected set; }


    [SerializeField] public GameObject DynamicPanel;

    [SerializeField] public KeyCode keyToOpen;

    public void Start()
    {
        keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode),PlayerPrefs.GetString("Skills"));
    }

    public virtual void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Debug.Log(gameObject);
            Debug.Log(Instance);
            Destroy(gameObject);
        }
    }

    public void Update()
    {
        if (Input.GetKeyDown(keyToOpen))
        {
            if (!DynamicPanel)
            {
                this.OpenPanel();
            }
            else
            {
                this.ClosePanel();
            }
        }
    }

    /// <summary>
    /// Function to fetch currently opened panel asociated with this UI manager
    /// </summary>
    /// <param name="panelConrtoller"></param>
    public void SetPanelController(GameObject panelConrtoller)
    {
        // ToDo change fetched types
        //DynamicPanel = dynamicPanelController;
    }

    public virtual bool OpenPanel()
    {
        Console.WriteLine("Panel opened");

        GameObject globalGUI = GameObject.FindGameObjectWithTag("GUI");

        if (!globalGUI)
            throw new Exception("Panel could not be opened - can't find global GUI object!!");

        GameObject uiPanelTemplate = GetTemplatePanel();

        DynamicPanel = GameObject.Instantiate(uiPanelTemplate, uiPanelTemplate.transform.position, Quaternion.identity, globalGUI.transform);   // 4'th arg allow set object as child

        DynamicPanel.transform.localPosition = uiPanelTemplate.transform.position;   // prevent overwritten position by... environment???

        DynamicPanel.name = uiPanelTemplate.name;

        SetupPanel();

        return true;
    }

    public virtual bool ClosePanel()
    {
        Console.WriteLine("Panel closed");

        try
        {
            Destroy(DynamicPanel);

            return true;
        }
        catch (Exception ex) { Debug.LogError(ex.Message); }

        return true;
    }

    public virtual void SetupPanel()
    {
        if (!DynamicPanel)
            throw new Exception("Panel not found - UIBaseManager don't have UI Panel instance!!");

        // bind this class instance
        //DynamicPanel.GetComponent<TaskPanelController>().Bind(this);
    }

    /// <summary>
    /// Function which return panel status
    /// True - if panel is opened and it's instance is handled
    /// false - if panel is closed and it's instance is not handled
    /// </summary>
    /// <returns></returns>
    public bool GetPanelStatus()
    {
        return DynamicPanel != null;
    }

    protected GameObject GetTemplatePanel()
    {
        // Resources = default path - Asset/Resources ... .obj
        return Resources.Load(ITEM_LOCALIZATION + PANEL_NAME) as GameObject;
    }
}