using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using FingersDance.Control.TimeLine;
using FingersDance.Data;
using FingersDance.Factory;
using GestureControl;

namespace FingersDance.Control.SyncSource
{
	public partial class UserControlSyncSource
	{
        #region Variables   
            public event EventHandler OnSuccessAnnotation;
        #endregion

		public UserControlSyncSource()
		{
			this.InitializeComponent();

			// Insert code required on object creation below this point.
		}

        public void Load(string path, Color col, Cutting cut)
        {
            this.UserControlPlayer.initPlayer(path);
            //Initialisation du Timer
            UCTimeLine.initTimer(col, cut);
            this.UserControlPlayer.playerPlay();
            this.userControlTimeLine.OnSuccessAnnotation += new EventHandler(userControlTimeLine_OnSuccessAnnotation);
            this.userControlTimeLine.AnnotationAdded += new EventHandler(userControlTimeLine_AnnotationAdded);
            this.userControlTimeLine.AnnotationRemoved += new EventHandler(userControlTimeLine_AnnotationRemoved);
            this.UserControlPlayer.PlayerStopOrPause += new EventHandler(UserControlPlayer_PlayerStopOrPause);
            UserControlPlayer.NewGestureRegognized += new EventHandler(UCTimeLine.UserControlPlayer_NewGestureRegognized);
        }

        void userControlTimeLine_AnnotationRemoved(object sender, EventArgs e)
        {
            try
            {
                DataDictionary Data = (new DataFactory()).Data;
                Data.RemoveAnnotation((Annotation)sender);
            }
            catch { }
        }

        //  Ajout Annotation
        void userControlTimeLine_AnnotationAdded(object sender, EventArgs e)
        {
            try
            {
                DataDictionary Data = (new DataFactory()).Data;
                Data.AddAnnotation((Annotation)sender);
            }
            catch { }
        }

        #region player
        private void UserControlPlayer_PlayerOpened(object sender, EventArgs e)
        {
            //Initialisation du slider
            UCTimeLine.initslider(UserControlPlayer.TotalMilliseconds);
            //Demarrage du Timer
            UCTimeLine.timerStart();

        }

        public void UserControlPlayer_PlayerStopOrPause(object sender, EventArgs e)
        {
            try
            {
                userControlTimeLine.thumbRotation((bool)sender);
            }catch(Exception ){}
        }

        public void setUserPlayerVolume(double val)
        {
            try
            {
                UserControlPlayer.setSound(val);
            }
            catch (Exception) { }
        }
        public void setUserPlayerMute(bool b)
        {
            try
            {
                UserControlPlayer.setMute(b);
            }
            catch (Exception) { }
        }

        #endregion

        #region TimeLine


        private void UserControlTimeLine_DragStarted(object sender, EventArgs e)
        {
            UserControlPlayer.playerPause();
        }


        private void UserControlTimeLine_DragCompleted(object sender, EventArgs e)
        {
        }
        private void userControlTimeLine_OnSuccessAnnotation(object sender, EventArgs e)
        {
            OnSuccessAnnotation(this, new EventArgs());
        }

        #endregion


        #region SynSource pour chaque X milliseconds
        private void UserControlTimeLine_TimerTick(object sender, EventArgs e)
        {
            if (!UCTimeLine.IsDragging)
            {
                UCTimeLine.Slider.Value = UserControlPlayer.Player.Position.TotalMilliseconds;
            }
            int SliderValue = (int)UCTimeLine.Slider.Value;
            if (UCTimeLine.FinishedDragging)
            {
                TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);

                UserControlPlayer.Player.Position = ts;
                UserControlPlayer.playerPlay();

                UCTimeLine.IsDragging = false;
                UCTimeLine.FinishedDragging = false;
            }
            UserControlPlayer.Time = (float)SliderValue / 1000;
            DataDictionary Data = (new DataFactory()).Data;
            UserControlPlayer.ApplyColor(Data.GetColors((int)((float)SliderValue / 1000)));
        }
        #endregion

        public UserControlTimeLine userControlTimeLine
        {
            get { return this.UCTimeLine; }
        }

        public void PlayerPause()
        {
            UserControlPlayer.playerPause();
        }
    }
}