using System.Collections.Generic;
using Cirno.Scripts;
using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Resources;
using Godot;

public partial class Actor : CharacterBody2D 
{
    [Export]
    public EnemyResource EnemyData { get; private set; }

    public float MovementSpeed => EnemyData.MovementSpeed;

    public float Health => EnemyData.MaxHealth;
    
    [ExportCategory("Defeat Script")]
    [Export] public Node2D DefeatScript { get; set; }

    [Export] public ActivationType ActivationType { get; private set; } = ActivationType.Toggle;

    [Export]
    public AiState StartingAiState { get; private set; }
    
    public  Vector2 MovementDirection { get; set; }
    public Vector2 FacingDirection { get; set; }

    private GameManager _gameManager;

    private List<ActorModule> _modules = new();

    [Signal]
    public delegate void OnDeathEventHandler();

    [Signal]
    public delegate void OnControlAssumedEventHandler();
    
    public bool IsDestroyed { get; set; }

    public override void _Ready()
    {
        _gameManager = this.GetGameManager();

        var children = GetChildren();
        foreach (var child in children)
        {
            if (child is not ActorModule actorModule) continue;
            _modules.Add(actorModule);
            actorModule.Init(this);
        }
    }

    public override void _Process(double delta)
    {
        foreach (var handler in _modules) 
        {
            handler.Update(delta);
        }
    }
    
    public override void _PhysicsProcess(double delta)
    {
        foreach (var handler in _modules) 
        {
            handler.PhysicsUpdate(delta);
        }
    }

    public void TriggerDeath()
    {
        EmitSignal(SignalName.OnDeath);
    }

    public void AssumeControl() 
    {
        EmitSignal(SignalName.OnControlAssumed);
    }

}