using System;
using System.Collections.Generic;
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 System.Windows.Controls.Primitives;
using Microsoft.Surface.Presentation.Controls;
using System.Windows.Threading;

using FingersDance.Data;
using FingersDance.ViewModels;

namespace FingersDance.Control.TimeLine
{
	public partial class UserControlTimeLine
    {

        #region Variables
        private DispatcherTimer timer;
        private bool isDragging = false;
        private bool finishedDragging = false;
        #endregion

        public event EventHandler DragStarted;
        public event EventHandler DragCompleted;
        public event EventHandler TimerTick;

        private double totalmilliseconds;

        private List<Annotation> AnnotList = new List<Annotation>();
        private float AnnotWidth = 300 + 15; // The surfaceListBox adds 15 pixels between each item
        private CuttingViewModel CuttingVM;
        private Boolean AnnotWaiting = false;
        private float AnnotTcBegin;

        #region Properties

        public SurfaceSlider Slider
        {
            get
            {
                return slider;
            }
            set
            {
                slider = value;
            }
        }
        
        public bool IsDragging
        {
            get
            {
                return isDragging;
            }
            set
            {
                isDragging = value;
            }
        }

        public bool FinishedDragging
        {
            get
            {
                return finishedDragging;
            }
            set
            {
                finishedDragging = value;
            }
        }

        public DispatcherTimer Timer
        {
            get
            {
                return timer;
            }
            set
            {
                timer = value;
            }
        }
        #endregion
        


        
		public UserControlTimeLine()
		{
			this.InitializeComponent();

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

        public void initslider(double totalmillisecondsPar)
        {
            totalmilliseconds = totalmillisecondsPar;
            slider.Maximum = totalmilliseconds;
            // When the timeline is resized, we catch the resize event to define TimelineView's good scale
            this.SizeChanged += new SizeChangedEventHandler(UserControlTimeLine_SizeChanged);

            // TEMP FOR DATA BINDING
            AnnotList = new List<Annotation>();
            AnnotList.Add(new Annotation(0, 10, "Axe Cam 1"));
            //AnnotList.Add(new Annotation(100 - (1 * AnnotWidth), 70, "Mvt Cam 2"));
            //AnnotList.Add(new Annotation(200 - (2 * AnnotWidth), 50, "Saut 3"));
            //AnnotList.Add(new Annotation(100 - (3 * AnnotWidth), 20, "Saut 4"));
            //AnnotList.Add(new Annotation(120 - (4 * AnnotWidth), 50, "Saut 5"));
            Cutting cut = new Cutting("titre de cutting", AnnotList);
            CuttingVM = new CuttingViewModel(cut, AnnotWidth);
            tv.DataContext = CuttingVM;

            slider_ContactTapGesture(this,null);

        }

        void UserControlTimeLine_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // When scaleX = 1, 1 second = 1 pixel. To calculate the new scale, we take the real width in account.
            Double futurScale = (this.ActualWidth-30) / (totalmilliseconds / 1000);
            //Double futurScale = this.ActualWidth / ((totalmilliseconds<30000) ? (totalmilliseconds/10) : (totalmilliseconds / 1000)); // TEMP FOR SHORT CONTENTS
            tv.RenderTransform = new ScaleTransform(futurScale,1);
            //Console.WriteLine("futurScale = " + futurScale);
            
        }

        #region Timer
        public void initTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            timer.Tick += new EventHandler(timer_Tick);
        }

        public void timerStart()
        {
            if (timer != null)
                timer.Start();
        }
        void timer_Tick(object sender, EventArgs e)
        {
            OnTimerTick();
            if (!isDragging)
            {
                //slider.Value = media.Position.TotalMilliseconds;
            }
            if (finishedDragging)
            {
                //int SliderValue = (int)slider.Value;
                //TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
               // media.Position = ts;
               // media.Play();
               // isDragging = false;
                //finishedDragging = false;
            }
        }
        protected virtual void OnTimerTick()
        {
            if (TimerTick != null)
                TimerTick(this, new EventArgs());
        }

        #endregion

        private void sliderPosition_DragStarted(object sender, DragStartedEventArgs e)
        {
            isDragging = true;
            OnDragStarted();
           // media.Pause();
        }
        protected virtual void OnDragStarted()
        {
            if (DragStarted != null)
                DragStarted(this, new EventArgs());
        }

        private void sliderPosition_DragCompleted(
            object sender, DragCompletedEventArgs e)
        {
            finishedDragging = true;
            OnDragCompleted();
        }
        protected virtual void OnDragCompleted()
        {
            if (DragCompleted != null)
                DragCompleted(this, new EventArgs());
        }

        private void slider_ContactTapGesture(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
        {
            //addAnnotation();
        }

        public void addAnnotation()
        {
            Boolean annotOk = true;
            // We open a new annotation
            if (CuttingVM != null && AnnotWaiting == false)
            {
                AnnotTcBegin = (float)slider.Value / 1000;

                // First we check if the new annotation will not cover any
                foreach (Annotation a in AnnotList)
                {
                    //Console.WriteLine("a.TcBegin = " + a.TcBegin + ", AnnotTcBegin = " + AnnotTcBegin + ", tcOut = " + (a.TcBegin + a.Dur));
                    if (a.TcBegin <= AnnotTcBegin && AnnotTcBegin <= (a.TcBegin + a.Dur))
                    {
                        annotOk = false;
                    }
                }
                // if not, we mark the beginning
                if (annotOk == true)
                {
                    AnnotList.Add(new Annotation(AnnotTcBegin, 0, AnnotList.Count.ToString()));
                    CuttingVM.setListFromAnnotations(AnnotList, AnnotWidth);
                    //Console.WriteLine("BEGIN currentTimecode = " + AnnotTcBegin + ", nb = " + AnnotList.Count + ", res = " + (AnnotTcBegin - (AnnotList.Count * AnnotWidth)));
                    tv.DataContext = null;
                    tv.DataContext = CuttingVM;
                    AnnotWaiting = true;
                }
            }
            // We close the current opened annotation...
            else if (CuttingVM != null && AnnotWaiting == true && (((float)slider.Value/1000)>AnnotTcBegin))
            {
                // ... by setting setting the good beginning and the good duration
                float currentTC = (float)slider.Value / 1000;
                float currentDuration = currentTC - AnnotTcBegin;

                // First we check if the new annotation will not cover any (AnnotTcBegin was already checked)
                foreach (Annotation a in AnnotList)
                {
                    //Console.WriteLine("a.TcBegin = " + a.TcBegin + ", tcOut = " + (a.TcBegin + a.Dur) + ", AnnotTcBegin = " + AnnotTcBegin + ", currentTC = " + currentTC);
                    if (a.TcBegin <= currentTC && currentTC <= (a.TcBegin + a.Dur) || (AnnotTcBegin < a.TcBegin && (a.TcBegin + a.Dur) < currentTC))
                    {
                        annotOk = false;
                    }
                }

                if (annotOk == true)
                {
                    AnnotList.RemoveAt(AnnotList.Count - 1);
                    AnnotList.Add(new Annotation(AnnotTcBegin, currentDuration, AnnotList.Count.ToString()));
                    CuttingVM.setListFromAnnotations(AnnotList, AnnotWidth);
                    //Console.WriteLine("currentTimecode = " + AnnotTcBegin + ", curDur = " + currentDuration + ", nb = " + AnnotList.Count + ", res = " + (AnnotTcBegin - (AnnotList.Count * AnnotWidth)));
                    tv.DataContext = null;
                    tv.DataContext = CuttingVM;
                    AnnotWaiting = false;
                }
            }
        }

	}
}