using System;
using System.Collections;
using System.Threading.Tasks;
using Cirno.Scripts.Components.FSM;
using Cirno.Scripts.Enums;
using Cirno.Scripts.Resources;
using Godot;

namespace Cirno.Scripts.Activables;

public partial class LevelTeleporter : Teleporter
{
    [Export] public string LevelPath { get; set; }
    [Export] public MapResource  Map { get; private set; }

    [Export] public bool SaveInventory { get; set; }

    protected override async Task Teleport(IStateMachine<PlayerState, CharacterBody2D> player)
    {
        if (string.IsNullOrWhiteSpace(LevelPath) && Map is null) return;
        //player.RequestMovementDisable(true);
        player.SetState(PlayerState.Cutscene);

        await TweenPlayer(player.MainObject);

        _particles.Emitting = true;

        //await player.Teleport();
        player.SetState(PlayerState.Teleporting);
        await Task.Delay((int)(0.6f * 1000));

        await Task.Delay((int)(TeleportAnimationLength * 1000));

        if (SaveInventory)
        {
            // Save inventory
            GlobalState.Instance.SessionSettings.Items = InventoryManager.Instance.Save();
        }
        else
        {
            GlobalState.Instance.SessionSettings.EquippedWeaponId = string.Empty;
        }

        if (!string.IsNullOrWhiteSpace(LevelPath))
        {
            if (GlobalState.Instance.SessionSettings.GameMode is GameMode.Roguelite)
            {
                GlobalState.Instance.SessionSettings.LevelNumber += 1;
            }
            
            GlobalState.Instance.GotoScene(LevelPath);
        }
        else
        {
            GlobalState.Instance.GotoScene(Map);
        }
        
    }
}