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


/// <summary>
/// Class which component which an LogicElementUI can be dragged into should derive from
/// </summary>
public abstract class LogicElementHolder : MonoBehaviour
{

    /// <summary>
    /// List of all Holders in scene
    /// </summary>
    private static List<LogicElementHolder> EnabledHolders = new List<LogicElementHolder>();

    [Tooltip("An Element with a higher priority will be used if two object overlap")]
    public int Priority = 0;


    #region Unity Functions
    protected virtual void OnEnable()
    {
        EnabledHolders.Add(this);
    }

    protected virtual void OnDisable()
    {
        EnabledHolders.Remove(this);
    }
    #endregion

    #region Class Funtions
    /// <summary>
    /// When a LogicElement has started being dragged from this holder
    /// </summary>
    /// <param name="element">Element which is being dragged</param>
    public abstract void OnRemove(LogicElementUI element);

    /// <summary>
    /// When a logicElement needs to be added back to this holder
    /// </summary>
    /// <param name="element">Element which is being Added</param>
    public abstract void OnAdd(LogicElementUI element);

    /// <summary>
    /// When a logic Element is hovering over this holder
    /// </summary>
    /// <param name="element">Element which is hovering</param>
    public virtual void OnHover(LogicElementUI element) { }

    /// <summary>
    /// First frame a logic Element is hovering over this holder
    /// </summary>
    /// <param name="element">Element which is hovering</param>
    public virtual void OnHoverStart(LogicElementUI element) { }

    /// <summary>
    /// first frame a logic Element is  no longer hovering over this holder
    /// </summary>
    /// <param name="element">Element which is hovering</param>
    public virtual void OnHoverEnd(LogicElementUI element) { }

    /// <summary>
    /// Called when an element in this holder is double clicked
    /// </summary>
    /// <param name="element">element which was double clicked</param>
    public abstract void OnDoubleClick(LogicElementUI element);


    /// <summary>
    /// Called to check if this holder can hold the provided element;
    /// </summary>
    /// <param name="element">element to check if it can hold</param>
    /// <returns>returns true if this can hold the element </returns>
    public virtual bool canHold(LogicBlock element) { return true; }


    #endregion Class Funtions

    #region Static Functions
    /// <summary>
    /// Gets an array of Any element holders a RectTransform is overlapping with
    /// </summary>
    /// <param name="rt">recttransform to check overlapping</param>
    /// <returns>array of Any element holders a rt is overlapping with</returns>
    public static LogicElementHolder[] OverlappingElements(RectTransform rt)
    {
        List<LogicElementHolder> retVal = new List<LogicElementHolder>();

        Rect rect = rt.GlobalRect();
        Rect holderRect;
        foreach (LogicElementHolder holder in EnabledHolders)
        {
            holderRect = (holder.transform as RectTransform).GlobalRect();

            if (rect.Overlaps(holderRect))
                retVal.Add(holder);
        }

        return retVal.ToArray();
    }


    public static void DebugDrawAll(Color color, float duration = 0.0f, bool depthTest = false)
    {
        EnabledHolders.ForEach(p => DebugExtensions.DrawRect(p.transform as RectTransform, color,duration, depthTest));
    }

    #endregion
}
