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

public abstract class Building 
{
    public static Action<Building> OnBuildingConstructed;
    public static Action<Building> OnBuildingDemolished;

    public abstract Type BuildingType { get; }
    public abstract Tile SourceTile { get; }
    public abstract Tile[] TilesOccupied { get; }
    public abstract int Height { get; }
    public abstract int Width { get; }

    public abstract void Construct(Tile sourceTile);
    public abstract void Demolish();

    public enum Type
    {
        None = 0,
        Stockpile,
    }
}

public class Stockpile : Building
{
    public override Type BuildingType => Type.Stockpile;

    public override Tile SourceTile => m_tiles[0];

    public override Tile[] TilesOccupied => m_tiles;

    public override int Height => 1;

    public override int Width => 1;

    private Tile[] m_tiles;

    public override void Construct(Tile sourceTile)
    {
        var index = sourceTile.Position;
        m_tiles = new Tile[Height * Width];
        m_tiles[0] = sourceTile;
        //m_tiles[1] = MapManager.Instance.GetTile(index.x + 1,   index.y);
        //m_tiles[2] = MapManager.Instance.GetTile(index.x,       index.y + 1);
        //m_tiles[3] = MapManager.Instance.GetTile(index.x + 1,   index.y + 1);
        foreach(Tile t in m_tiles)
        {
            t.Type = Tile.TileType.Building;
            t.Building = this;
        }
        OnBuildingConstructed?.Invoke(this);
        Debug.LogFormat("Stockpile created at tile {0}", sourceTile);
    }

    public override void Demolish()
    {
        foreach (Tile t in m_tiles)
        {
            t.Type = Tile.TileType.Ground;
            t.Building = null;
        }
        Debug.LogFormat("Stockpile destroyed at tile {0}", m_tiles[0]);
        OnBuildingDemolished?.Invoke(this);
    }
}