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

[CreateAssetMenu(fileName = "New UnitManager", menuName = "ScriptableObjects/UnitManager")]
public class UnitManager : ScriptableObject
{
    private List<Unit> unitList;
    private List<Unit> enemyUnitList;
    private List<Unit> friendlyUnitList;

    private void Awake() {
        unitList = new List<Unit>();
        enemyUnitList = new List<Unit>();
        friendlyUnitList = new List<Unit>();
    }

    private void OnEnable() {
        Unit.OnAnyUnitDead += Unit_OnAnyUnitDead;
        Unit.OnAnyUnitSpawned += Unit_OnAnyUnitSpawned;
        unitList = new List<Unit>();
        enemyUnitList = new List<Unit>();
        friendlyUnitList = new List<Unit>();
    }
    private void OnDisable() {
        Unit.OnAnyUnitDead -= Unit_OnAnyUnitDead;
        Unit.OnAnyUnitSpawned -= Unit_OnAnyUnitSpawned;
    }


    private void Unit_OnAnyUnitSpawned(object sender, EventArgs empty) {
        Unit unit = sender as Unit;
        if (unit.IsEnemy()) {
            enemyUnitList.Add(unit);
        } else {
            friendlyUnitList.Add(unit);
        }
        unitList.Add(unit);
    }

    private void Unit_OnAnyUnitDead(object sender, EventArgs empty) {
        Unit unit = sender as Unit;
        if (unit.IsEnemy()) {
            enemyUnitList.Remove(unit);
        } else {
            friendlyUnitList.Remove(unit);
        }
        unitList.Remove(unit);
    }

    public List<Unit> GetUnitList() {
        return unitList;
    }
    public List<Unit> GetEnemyUnitList() {
        return enemyUnitList;
    }
    public List<Unit> GetFriendlyUnitList() {
        return friendlyUnitList;
    }
}