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

[Serializable]
public class WaveParameters
{
    // Remove delay between waves.
    //[SerializeField] private AnimationCurve _delayBetweenWaves = AnimationCurve.Linear(0.0f, 10.0f, 100.0f, 5.0f);
    [SerializeField] private AnimationCurve _startMonsterCount = AnimationCurve.Linear(0.0f, 5.0f, 100.0f, 25.0f);
    [SerializeField] private AnimationCurve _delayBetweenMonsters = AnimationCurve.Linear(0.0f, 1.0f, 100.0f, 0.5f);
    [SerializeField] private AnimationCurve _bonusSpawnRate = AnimationCurve.Linear(0.0f, 15.0f, 100.0f, 15.0f);
    [SerializeField] private AnimationCurve _maxBonusOnScreen = AnimationCurve.Linear(0.0f, 5.0f, 100.0f, 10.0f);
    [SerializeField] private AnimationCurve _bonusSize = AnimationCurve.Linear(0.0f, 1.5f, 100.0f, 1.5f);

    //public float GetDelayBetweenWave(int currentWave)
    //{
    //    return _delayBetweenWaves.Evaluate(currentWave);
    //}

    public float GetMonsterCount(int currentWave)
    {
        return _startMonsterCount.Evaluate(currentWave);
    }

    public float GetDelayBetweenMonsters(int currentWave)
    {
        return _delayBetweenMonsters.Evaluate(currentWave);
    }

    public float GetBonusSpawnRate(int currentWave)
    {
        return _bonusSpawnRate.Evaluate(currentWave);
    }

    public float GetMaxBonusOnScreen(int currentWave)
    {
        return _maxBonusOnScreen.Evaluate(currentWave);
    }

    public float GetBonusSize(int currentWave)
    {
        return _bonusSize.Evaluate(currentWave);
    }
}

public class WaveManager : MonoBehaviour
{
    [SerializeField] private WaveParameters _waveParameters = new WaveParameters();

    private int _waveCount = 0;
    private float _waveTimer = 0.0f;

    private MonsterManager _monsterSpawner = null;
    private BonusSpawner _bonusSpawner = null;

    private bool _isRunning = false;
    private bool _isSpawning = false;

    public bool IsRunning { get { return _isRunning; } }
    public int WaveCount { get { return _waveCount; } }

    public MonsterManager MonsterSpawner => _monsterSpawner;
    public BonusSpawner BonusSpawner => _bonusSpawner;

    public WaveParameters WaveParameters { get => _waveParameters; }

    #region Events
    private event Action<int> _onWaveChangeEvent = null;
    public event Action<int> OnWaveChange
    {
        add
        {
            _onWaveChangeEvent -= value;
            _onWaveChangeEvent += value;
        }
        remove
        {
            _onWaveChangeEvent -= value;
        }
    }
    #endregion Events

    public void LaunchWaves()
    {
        _monsterSpawner = GetComponentInChildren<MonsterManager>();
        _monsterSpawner.Init(this);
        _bonusSpawner = GetComponentInChildren<BonusSpawner>();
        _bonusSpawner.Init(this);
        _isRunning = true;
        NewWave();
    }

    public void NewWave()
    {
        _waveTimer = 0.0f;
        _waveCount++;
        _onWaveChangeEvent?.Invoke(_waveCount);
        GameSceneManager.Instance.UiManager.GamePanel.DisplayWaveCount(_waveCount);
        _isSpawning = true;
        _monsterSpawner.StartNewWave();
    }

    private void Update()
    {
        //if (_isRunning && GameManager.Instance.IsGamePlaying && _isSpawning == false)
        //{
        //    _waveTimer += Time.deltaTime;
        //    if (_waveTimer >= _waveParameters.GetDelayBetweenWave(_waveCount))
        //    {
        //        NewWave();
        //    }
        //}
    }

    public void OnSpawnEnd()
    {
        _isSpawning = false;
    }
}
