using Godot;
using Mr.BrickAdventures.scripts.components;

namespace Mr.BrickAdventures.Autoloads;

public partial class ConsoleManager : Node
{
    private GameStateStore Store => GameStateStore.Instance;
    private GameManager GameManager => GameManager.Instance;
    private AchievementManager AchievementManager => AchievementManager.Instance;
    private SkillManager _skillManager;
    private SkillUnlockerComponent _skillUnlockerComponent;

    public override void _Ready()
    {
        _skillManager = SkillManager.Instance;
    }

    private void AddCoinsCommand(int amount)
    {
        if (Store == null) return;
        Store.Player.Coins += amount;
        EventBus.EmitCoinsChanged(Store.GetTotalCoins());
    }

    private void SetCoinsCommand(int amount)
    {
        if (Store == null) return;
        Store.Player.Coins = Mathf.Max(0, amount);
        EventBus.EmitCoinsChanged(Store.GetTotalCoins());
    }

    private void SetLivesCommand(int amount)
    {
        if (Store == null) return;
        Store.Player.Lives = amount;
        EventBus.EmitLivesChanged(amount);
    }

    private void AddLivesCommand(int amount)
    {
        Store?.AddLives(amount);
    }

    private void SetHealthCommand(float amount)
    {
        var playerHealthComponent = GameManager?.Player?.GetNode<HealthComponent>("HealthComponent");
        if (playerHealthComponent != null)
            playerHealthComponent.Health = amount;
    }

    private void ResetSessionCommand()
    {
        Store?.ResetSession();
    }

    private void UnlockSkillCommand(string skillName)
    {
        if (!EnsureSkillManagement()) return;

        var skill = _skillManager.GetSkillByName(skillName);
        if (skill == null) return;

        Store?.UnlockSkillPermanently(skill);
        _skillManager.ActivateSkill(skill);
        _skillUnlockerComponent.EmitSignal(SkillUnlockerComponent.SignalName.SkillUnlocked, skill);
    }

    private void UnlockAllSkillsCommand()
    {
        if (!EnsureSkillManagement()) return;
        _skillUnlockerComponent.UnlockAllSkills();
    }

    private void RemoveSkillCommand(string skillName)
    {
        if (!EnsureSkillManagement()) return;

        var skill = _skillManager.GetSkillByName(skillName);
        if (skill == null) return;

        Store?.RemoveUnlockedSkill(skill.Name);
        _skillManager.DeactivateSkill(skill);
    }

    private void RemoveAllSkillsCommand()
    {
        if (!EnsureSkillManagement()) return;

        foreach (var skill in _skillManager.AvailableSkills)
        {
            Store?.RemoveUnlockedSkill(skill.Name);
            _skillManager.DeactivateSkill(skill);
        }
    }

    private void GoToNextLevelCommand()
    {
        GameManager?.OnLevelComplete();
    }

    private void UnlockAchievementCommand(string achievementId)
    {
        AchievementManager?.UnlockAchievement(achievementId);
    }

    private void ResetAchievementCommand(string achievementId)
    {
        AchievementManager?.LockAchievement(achievementId);
    }

    private bool EnsureSkillManagement()
    {
        var player = GameManager?.Player;
        if (player == null || !IsInstanceValid(player)) return false;

        _skillUnlockerComponent ??= player.GetNode<SkillUnlockerComponent>("SkillUnlockerComponent");
        return _skillManager != null && _skillUnlockerComponent != null;
    }
}
