using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using CorgiLabs.Utility;


public class FloorBreaker : GhostEventSource
{
    private static Dictionary<Vector3Int, FloorBreaker> m_ActiveBreakers = new Dictionary<Vector3Int, FloorBreaker>();

    [SerializeField] private Tilemap m_CollisionTilemap;
    [SerializeField] private Tile m_BrokenFloorTile;

    public static FloorBreaker Get(Vector3Int pos)
    {
        return m_ActiveBreakers.ContainsKey(pos) ? m_ActiveBreakers[pos] : null;
    }

    public override void StartEvent()
    {
        if (BrokenTile.GetRandom(out Vector3Int pos))
        {
            base.StartEvent();

            m_CollisionTilemap.SetTile(pos, m_BrokenFloorTile);
            m_ActiveBreakers.Add(pos, this);
            ServiceLocator.Get<GhostSpawner>().SpawnAt(ServiceLocator.Get<Grid>().GetCellCenterWorld(pos));
        }
    }

    public void Repair(Vector3Int pos)
    {
        m_CollisionTilemap.SetTile(pos, null);
        m_ActiveBreakers.Remove(pos);

        EventManager.TriggerEvent<GhostEventSource>(EventID.GhostEventSuccess, this);
    }
}
