using Godot;
using System;
using System.Collections.Generic;
using System.Linq;

public partial class wavecontroller : Node2D
{
    public int CurrWave = 0;

    private Node _friendlyNode;
    private Node _enemyNode;
    private player _playerNode;

    private float _aiStatModifier = 1f;
    
    [Export]
    public upgrade_menu UpgradeMenu;
    
    private List<Marker2D> _enemySpawns;
    private List<Marker2D> _friendlySpawns;
    private PackedScene _aiShipScene;
    private string _aiShipPath = "res://scenes/entities/ai_fighter.tscn";

    public override void _Ready()
    {
        _friendlyNode = GetNode<Node>("Ships/Friendly");
        _enemyNode = GetNode<Node>("Ships/Enemy");
        _playerNode = GetNode<player>("Ships/Friendly/Player");

        _enemySpawns = GetNode<Node>("Spawns/Enemy").GetChildren().OfType<Marker2D>().ToList();
        _friendlySpawns = GetNode<Node>("Spawns/Friendly").GetChildren().OfType<Marker2D>().ToList();
        
        _aiShipScene = GD.Load<PackedScene>(_aiShipPath);
    }

    public void StartWave()
    {
        if (_playerNode.Health > _playerNode.MaxHealth) _playerNode.Health = 100;
        if (CurrWave % 5 != 0)
        {
            // normal wave
            foreach (var spawn in _enemySpawns) // spawn enemies
            {
                //var enemy = new ai_fighter();
                Random rand = new Random();

                var enemy = _aiShipScene.Instantiate<ai_fighter>();
                enemy.Faction = ship.ShipFaction.ENEMY;
                enemy.Type = (ship.ShipType)rand.Next(0, 3);
                enemy.GlobalPosition = spawn.GlobalPosition;
                enemy.StatModifier = _aiStatModifier;
                enemy.Scale = new Vector2(0.6f, 0.6f);
                enemy.HitboxModifier = 1.6f; // make enemy hitbox a bit bigger (balance)

                _enemyNode.AddChild(enemy);
            }
        }
        // spawn friendlies after 5 waves & if 1 or less remaining
        // also trigger upgrade menu at the start of the wave (but not first wave)
        if ((CurrWave - 1) % 5 == 0)
        {
            
            if (CurrWave != 1)
            {
                UpgradeMenu.Show();
                GetTree().Paused = true;
            }

            if (_friendlyNode.GetChildCount() <= 1)
            {
                foreach (var spawn in _friendlySpawns)
                {
                    Random rand = new Random();

                    var friendly = _aiShipScene.Instantiate<ai_fighter>();
                    friendly.Faction = ship.ShipFaction.FRIENDLY;
                    friendly.Type = (ship.ShipType)rand.Next(0, 3);
                    friendly.GlobalPosition = spawn.GlobalPosition;
                    friendly.StatModifier = (_aiStatModifier) * 1.1f; // make friendlies slightly stronger
                    friendly.Scale = new Vector2(0.6f, 0.6f);
                
                    _friendlyNode.AddChild(friendly);
                }
            }
        }

        else if (CurrWave % 5 == 0) // ace/boss wave
        {
             // spawn enemies
            
             Random rand = new Random();

             var spawn = _enemySpawns[rand.Next(0, 3)];
             
             var enemy = _aiShipScene.Instantiate<ai_fighter>();
             enemy.Faction = ship.ShipFaction.ACE;
             enemy.Type = (ship.ShipType)rand.Next(0, 3);
             enemy.GlobalPosition = spawn.GlobalPosition;
             enemy.StatModifier = (_aiStatModifier) * 1.2f; // make aces stronger
             enemy.Scale = new Vector2(0.6f, 0.6f);
             enemy.HitboxModifier = 1.3f; // make ace hitbox a bit bigger but smaller than regular enemies (balance)
             _enemyNode.AddChild(enemy);
             
             enemy.MaxHealth += 400; // make aces health a bit higher
             enemy.Health = enemy.MaxHealth;
             enemy.UpdateHealthLabel(enemy.Health);
             
             _aiStatModifier += 0.1f; // increase ai stat modifier by 0.1f after an ace wave
             if (_aiStatModifier >= 2f) _aiStatModifier = 2f; // cap ai stat modifier at 2f (10 ace waves)
        }
    }
    
}