using UnityEngine;

public abstract class ProductionBuilding : House
{
    [SerializeField]
    private int _range;
    [SerializeField]
    private float _multiplier;

    public override float PopulationGiven => GlobalConfig.Instance.Current.populationGivenPerHouseUpgrade;
    protected abstract Enum.ResourceNodeType RessourceNodeType { get; }
    public int Range { get => _range; set => _range = value; }

    private Vector3 _position;

    public override void Start()
    {
        base.Start();
        _position = Position;
        ApplyMultiplier(_multiplier);
    }

    public override void LevelStart()
    {
        base.LevelStart();
    }

    public override void LevelDestroy()
    {
        ApplyMultiplier(1 / _multiplier);
        base.LevelDestroy();
    }

    private void ApplyMultiplier(float multiplier)
    {
        for (int i = -_range; i <= _range; i++)
        {
            for (int j = -_range; j <= _range; j++)
            {
                Vector3 checkPosition = new Vector3(Mathf.Floor(_position.x) + i, Mathf.Floor(_position.y) + j, 0.0f);
                var tile = LevelManager.Instance.Get<ResourceTile>(t => t.Position == checkPosition);
                if (tile == default) continue;

                var checkResourceType = tile.ResourceNodeType;
                if (checkResourceType != RessourceNodeType) continue;

                tile.YieldSpeedMultiplier *= multiplier;
            }
        }
    }
}