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

public class AntaresAddon : MonoBehaviour
{
    public string objectDescription = "Write your note here";
    public bool descFoldout = false, multiTagFoldout = false;
    public Vector2 descriptionScroll = new Vector2(), mTagsScroll = new Vector2();
    public GameObject multitagHolder = null;
    protected Dictionary<string, GameObject> mtagsObj;
    public List<string> mtagsPrefab;
    public bool hideInHierarchy, hideInHierarchyByMenuCommand;

    void Start()
    {
        if (AntaresAddonController.FreeMemoryOnPlay()) objectDescription = "";
        TryToRestoreList();
        GetMultitagsList();
        AntaresAddonController.AddMultitagToObject(gameObject, GetMultitagsList());
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <param name="mtags"></param>
    public void MultitagAdd(params string[] mtags)
    {
        if (multitagHolder == null)
        {
            multitagHolder = new GameObject(name + "_MultiTag Holder");
            multitagHolder.transform.parent = AntaresAddonController.GetMultiTagsCollector();
        }

        if (mtagsObj == null) mtagsObj = new Dictionary<string, GameObject>();
        if (mtagsPrefab == null) mtagsPrefab = new List<string>();

        int count = mtags.Length;
        for (int i = 0; i < count; i++)
        {
            if (mtagsObj.ContainsKey(mtags[i]) || mtags[i] == "No Tags Found") continue;

            GameObject mt = new GameObject("Tag : " + mtags[i]);
            mt.transform.parent = multitagHolder.transform;
            mt.tag = mtags[i];
            mtagsObj.Add(mtags[i], mt);
            if (!mtagsPrefab.Contains(mtags[i])) mtagsPrefab.Add(mtags[i]);
        }
    }

    /// <summary>
    /// Called when object distroyed
    /// </summary>
    void OnDisable()
    {
        AntaresAddonController.OnObjectWithMultitagDisabled(gameObject);
    }

    /// <summary>
    /// (!)Do not use it yourself
    /// </summary>
    public void TryToRestoreList()
    {
        if (mtagsObj != null || mtagsPrefab == null) return;
        if (multitagHolder != null)
        {
            Transform[] childs = multitagHolder.GetComponentsInChildren<Transform>();
            int count = childs.Length;
            mtagsObj = new Dictionary<string, GameObject>();
            for (int i = 0; i < count; i++)
            {
                if (childs[i].gameObject.tag != "")
                {
                    mtagsObj.Add(childs[i].gameObject.tag, childs[i].gameObject);
                }
            }
        }

        foreach (string st in mtagsPrefab) MultitagAdd(st);
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <param name="mtags"></param>
    public void MultitagRemove(params string[] mtags)
    {
        if (mtagsObj == null || mtags.Length == 0 || mtags[0] == "") return;
        int count = mtags.Length;
        for (int i = 0; i < count; i++)
        {
            if (!mtagsObj.ContainsKey(mtags[i])) continue;
            switch (Application.isPlaying)
            {
                case true:
                    Destroy(mtagsObj[mtags[i]]);
                    mtagsObj.Remove(mtags[i]);
                    mtagsPrefab.Remove(mtags[i]);
                    break;
                default:
                    DestroyImmediate(mtagsObj[mtags[i]]);
                    mtagsObj.Remove(mtags[i]);
                    mtagsPrefab.Remove(mtags[i]);
                    break;
            }

            if (MultitagsCount() == 0) RemoveAllMultitags();
        }
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <param name="mtag"></param>
    /// <returns></returns>
    public bool HasMultitag(string mtag)
    {
        if (mtagsObj == null) return false;
        return mtagsObj.ContainsKey(mtag);
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <param name="mtag"></param>
    /// <returns></returns>
    public GameObject GetMultitagObject(string mtag)
    {
        if (!HasMultitag(mtag)) return null;
        return mtagsObj[mtag];
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <param name="mtags"></param>
    /// <returns></returns>
    public bool HasMultitagAny(params string[] mtags)
    {
        if (mtagsObj == null) return false;
        int count = mtags.Length;
        for (int i = 0; i < count; i++)
        {
            if (mtagsObj.ContainsKey(mtags[i])) return true;
        }
        return false;
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <param name="mtags"></param>
    /// <returns></returns>
    public bool HasMultitagsBoth(params string[] mtags)
    {
        if (mtagsObj == null) return false;
        int count = mtags.Length;
        bool retValue = false;
        for (int i = 0; i < count; i++)
        {
            if (HasMultitag(mtags[i])) retValue = true;
            else return false;
        }
        return retValue;
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <returns></returns>
    public int MultitagsCount()
    {
        if (multitagHolder != null)
            return multitagHolder.transform.childCount;
        return 0;
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <returns></returns>
    public string[] GetMultitagsList()
    {
        if (multitagHolder == null) return new string[] { "No Tags Found" };
        if (mtagsObj == null) mtagsObj = new Dictionary<string, GameObject>();
        int childs = multitagHolder.transform.childCount;
        if (childs == 0) return new string[] { "No Tags Found" };
        string[] keys = new string[childs];
        mtagsObj.Clear();
        GameObject ch;
        for (int i = 0; i < childs; i++)
        {
            ch = multitagHolder.transform.GetChild(i).gameObject;
            if (!mtagsObj.ContainsKey(ch.tag))
            {
                mtagsObj.Add(ch.tag, ch);
                keys[i] = ch.tag;
            }
        }
        return keys;
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    /// <returns></returns>
    public string[] GetMultitagsListNoRuntime()
    {
        if (mtagsPrefab == null || mtagsPrefab.Count == 0) return new string[] { "No Tags Found" };
        string[] retString = new string[mtagsPrefab.Count];
        mtagsPrefab.CopyTo(retString, 0);
        return retString;
    }

    /// <summary>
    /// (!)Use AntaresAddonController instead this method
    /// </summary>
    public void RemoveAllMultitags()
    {
        switch (Application.isPlaying)
        {
            case true:
                Destroy(multitagHolder);
                break;
            default:
                DestroyImmediate(multitagHolder);
                break;
        }
        mtagsObj.Clear();
        mtagsPrefab.Clear();
    }
}

