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

public abstract class StructureController : LevelObjectBase
{
    public const int DESTROYED_STRUCTURE_LAYER = 11;
    public const int STRUCTURE_LAYER = 9;

    [Tooltip("The maximum development level for the structure")]
    public int MaxLevel = 1;
    [Tooltip("The initial development level for the structure (0-MaxLevel)")]
    public int StartingLevel = 1;
    [Tooltip("The current durability level for the structure (0-100")]
    //public int Durability = 1;
    public GameObject Halo;
    public Sprite ReadyHalo;
    public Sprite ActiveHalo;
    public GameObject RBCollider;

    // Current usage is only in WallController - sprite mapping and assignment
    public SpriteStateMap[] spriteStateMap;
    protected Dictionary<int, SpriteStateMap> spriteStateMapDictionary;
    protected SpriteRenderer selfRenderer;


    //private const string Necromancer = "Necromancer";
    public int CurrentLevel;

    [System.Serializable]
    public struct SpriteStateMap
    {
        public int Level; // Use this to match
        public Sprite ObjectSprite; // Sprite to use
        public GameObject Halo; //Halo to use
    }

    public override void Init()
    {
        base.Init();
        spriteStateMapDictionary = new Dictionary<int, SpriteStateMap>();
        foreach (var item in spriteStateMap)
        {
            spriteStateMapDictionary.Add(item.Level, item);
        }
        selfRenderer = GetComponent<SpriteRenderer>();
        CurrentLevel = StartingLevel;
        CurrentHP = StartingHP;
        RenderObjectLevel();
    }

    protected virtual void RenderObjectLevel()
    {
        var spriteLevel = spriteStateMapDictionary[CurrentLevel];

        if (Halo != null)
        {
            SetHaloState(false);
        }
        Halo = spriteLevel.Halo;
        SetHaloState(Active);

        selfRenderer.sprite = spriteLevel.ObjectSprite;

        if (CurrentLevel <= 0 && CurrentHP <= 0)
        {
            Destroyed = true;
            RBCollider.layer = DESTROYED_STRUCTURE_LAYER;
        }
        else
        {
            Destroyed = false;
            RBCollider.layer = STRUCTURE_LAYER;
        }
    }
    //// Current Sprite / default sprite
    //public string currentSpriteState;

    public bool Active { get; set; }

    protected override void HPZero()
    {
        PowerDown();
    }

    public virtual bool PowerUp()
    {
        if (CurrentLevel < MaxLevel)
        {
            CurrentLevel++;
            if (CurrentHP <= 0)
            {
                CurrentHP += MaxHP;
            }
            RenderObjectLevel();
            return true;
        }
        else if (CurrentHP< MaxHP)
        {
            CurrentHP = MaxHP;
            return true;
        }
        return false;
    }

    public virtual bool PowerDown()
    {
        if (CurrentLevel > 0)
        {
            CurrentLevel--;
            CurrentHP += MaxHP;
            RenderObjectLevel();
            return true;
        }
        RenderObjectLevel();
        return false;
    }


    protected void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log($"{this.name}:{collision.name} entered {this.name} space.");
        var obj = collision.GetComponent<LevelObjectBase>();
        if (obj != null && obj.ObjectType == LevelObjectType.Player)
            SetActive();
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        Debug.Log($"{this.name}:{collision.name} exited {this.name} space.");
        var obj = collision.GetComponent<LevelObjectBase>();
        if (obj != null && obj.ObjectType == LevelObjectType.Player)
            SetInactive();
    }
    public void SetActive() {
        Active = true;
        SetHaloState(Active);
    }  
    public void SetInactive() {
        Active = false;
        SetHaloState(Active);
    }

    private void SetHaloState(bool state)
    {
        if(Halo != null)
        {
            Halo.SetActive(state);
        }
    }

    public virtual bool PlayerAction(Necromancer player)
    {
        if (player.HasGoo)
        {
            if (PowerUp())
            {
                player.UseGoo(1);
                return true;
            }
        }
        return false;
    }
}
