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

namespace Cirno.Scripts.UI;

public partial class IntroScenePlayer : CanvasLayer
{
    [Export] public bool StartRunning { get; private set; }
    
    [Export]
    public Control PanelsHolder { get; private set; }
    
    [Export]
    public Array<Texture2D> Images { get; private set; }

    [Export]
    public float TransitionTime = 5f;
    
    [Export]
    public string NextSceneName { get; set; }
    
    [Export]
    public AnimationPlayer AnimationPlayer { get; private set; }
    
    private int _currentPanelIndex;
    
    private List<TextureRect> _panels = new ();
    
    public TextureRect CurrentPanel => _panels[_currentPanelIndex];
    
    private bool _running = false;

    private bool _isEnding = false;

    private void DeferredStartAnimation()
    {
        AnimationPlayer.Play("intro");
    }
    
    public override void _Ready()
    {
        _running = StartRunning;

        DeferredStartAnimation();
    }

    public void Run()
    {
        var first = _panels.First();
        first.Visible = true;
        first.SetModulate(new Color(first.Modulate.R, first.Modulate.G, first.Modulate.B, 1f));
    }

    public override void _Process(double delta)
    {
        if (!_isEnding && Input.IsAnythingPressed())
        {
            Finished();
        }
    }
    
    private void Finished()
    {
        if (_isEnding) return;
        
        _isEnding = true;
        GlobalState.Instance.GotoScene(NextSceneName);
        //GetTree().ChangeSceneToFile(NextSceneName);
    }
}