using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Iisu;
using bbiwarg.Utility;

namespace bbiwarg.Input.InputProviding
{
    class VideoInputProvider : InputProvider
    {

        public override int CurrentFrameID { get { return currentMovieFrame.Value; } }
        public String MoviePath { get; private set; }
        public bool IsPaused { get; private set; }

        private IParameterHandle<int> currentMovieFrame;
        private IParameterHandle<int> frameCount;
        private IParameterHandle<int> playStep;

        public VideoInputProvider(String moviePath)
        {
            MoviePath = moviePath;
        }

        protected override IDeviceConfiguration createDeviceConfiguration()
        {
            IDeviceConfiguration conf = base.createDeviceConfiguration();
            conf.MoviePath = MoviePath;
            return conf;
        }

        protected override void registerHandles()
        {
            base.registerHandles();

            device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 1; // 0=once, 1=loop, 2=pingPong
            currentMovieFrame = device.RegisterParameterHandle<int>("SOURCE.MOVIE.CurrentFrame");
            frameCount = device.RegisterParameterHandle<int>("SOURCE.MOVIE.FrameCount");
            playStep = device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayStep");
        }

        public void pause()
        {
            IsPaused = true;
        }

        public void play()
        {
            IsPaused = false;
        }

        public void goToNextFrame()
        {
            nextFrame();
        }

        public void goToPreviousFrame()
        {
            playStep.Value = -1;
            nextFrame();
            playStep.Value = 1;
        }

        protected override void run() {
            while (IsActive) {
                if (!IsPaused)
                    nextFrame();
                else
                    Thread.Sleep(30);
            }
        }
    }
}
