using Godot;
using System;
using Godot.NativeInterop;

public partial class VideoControl : Control
{

    [Signal]
    public delegate void FrameChangedEventHandler(int frame_nr);

    [Signal]
    public delegate void NextFrameCalledEventHandler(int frame_nr);

    [Signal]
    public delegate void VideoLoadedEventHandler();

    [Signal]
    public delegate void VideoEndedEventHandler();

    [Signal]
    public delegate void PlaybackStartedEventHandler();
    
    [Signal]
    public delegate void PlaybackPausedEventHandler();

    [Signal]
    public delegate void PlaybackReadyEventHandler();

    public const float PLAYBACK_SPEED_MIN = 0.25f;
    public const float PLAYBACK_SPEED_MAX = 4.0f;

    [Export(PropertyHint.File)] public string Path;
    [Export] public bool EnableAudio = true;
    [Export] public bool EnableAutoPlay = false;

    [Export(PropertyHint.Range, $"0.25,4.0,0.05")]
    public float PlaybackSpeed = 1.0f;

    [Export] public bool PitchAdjust = true;
    [Export] public bool Loop = false;
    
    [ExportGroup("Extra's")]
    [Export] public ColorProfile ColorProfile = ColorProfile.Auto;

    [Export] public bool Debug = false;

    public GoZenVideo? Video;

    public TextureRect VideoTexture = new TextureRect();
    public AudioStreamPlayer AudioStream = new AudioStreamPlayer();

    public bool IsPlaying = false;
    public int CurrentFrame = 0;

    private float _timeElapsed = 0.0f;
    private float _timeFrame = 0.0f;
    private int _skippedFrames = 0;

    private int _rotation = 0;
    private int _padding = 0;
    private float _frameRate = 0.0f;
    private int _frameCount = 0;

    private Vector2I _resolution = Vector2I.Zero;
    private ShaderMaterial _shaderMaterial;

    private long[] _threads = [];
    private AudioEffectPitchShift _audioPitchEffect = new AudioEffectPitchShift();

    private ImageTexture _yTexture;
    private ImageTexture _uTexture;
    private ImageTexture _vTexture;
    
}

public enum ColorProfile
{
    Auto,
    Bt470,
    Bt601,
    Bt709,
    Bt2020,
    Bt2100
}