using System;

using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace TrailerPlayer.View
{
    public partial class TrailerView : UserControl
    {
        double z = 0.0;
        Storyboard paging;
        public TrailerView()
        {
            InitializeComponent();
            //Loaded += new RoutedEventHandler(TrailerView_Loaded);
            Loaded += new RoutedEventHandler(TrailerView_Loaded);
            Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
        }

        void Content_FullScreenChanged(object sender, EventArgs e)
        {
            //defaultViewer.Visibility = Visibility.Collapsed;
            //fullScreenViewer.Visibility = Visibility.Visible;
            //fullScreenViewer.DataContext = defaultViewer.SelectedItem;
            List<TrailerControl> added = new List<TrailerControl>(1);
            added.Add((TrailerControl)defaultViewer.Items[defaultViewer.SelectedIndex]);
            defaultViewer_SelectionChanged(null, new SelectionChangedEventArgs(new List<TrailerControl>(), added));
        }

        void TrailerView_Loaded(object sender, RoutedEventArgs e)
        {
            defaultViewer.SelectionChanged += new SelectionChangedEventHandler(defaultViewer_SelectionChanged);
            paging = new Storyboard();
            Main.Resources.Add("paging", paging);
            //defaultViewer.Loaded += new RoutedEventHandler(defaultViewer_Loaded);
            //Viewer.Loaded += new RoutedEventHandler(Viewer_Loaded);
        }

        //void Viewer_Loaded(object sender, RoutedEventArgs e)
        //{
        //    defaultViewer.SelectedIndex = 1;
        //    defaultViewer_SelectionChanged(null, null);
        //}
        List<TrailerControl> lista;
        /// <summary>
        /// Loads the Data got from the ViewModel to the List if there is any
        /// </summary>
        public void Begin()
        {
            lista = new List<TrailerControl>();
            int i = 0;
            foreach (var item in (ICollection)this.DataContext)
            {
                lista.Add(new TrailerControl());
                lista[i].DataContext = item;
                i++;
            }
            defaultViewer.DataContext = lista;
            defaultViewer.SelectedIndex = 0;
            List<TrailerControl> added = new List<TrailerControl>(1);
            added.Add((TrailerControl)defaultViewer.Items[defaultViewer.SelectedIndex]);
            defaultViewer_SelectionChanged(null, new SelectionChangedEventArgs(new List<TrailerControl>(), added));

        }

        void defaultViewer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (App.Current.Host.Content.IsFullScreen)
            {
                //the paging animation storyboard
                paging = new Storyboard();
                double targetOffset = defaultViewer.SelectedIndex * App.Current.Host.Content.ActualWidth;
                // Create a duration of 0.2 seconds.
                Duration duration = new Duration(TimeSpan.FromSeconds(0.2));
                DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
                myDoubleAnimation1.Duration = duration;
                paging.Duration = duration;
                paging.Children.Add(myDoubleAnimation1);
                Storyboard.SetTarget(myDoubleAnimation1, defaultViewer);
                // Set the attached properties of Canvas.Left and Canvas.Top
                // to be the target properties of the two respective DoubleAnimations
                Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(Canvas.Left)"));
                myDoubleAnimation1.To = -290 - targetOffset;//defaultViewer.SelectedIndex*itemWidth*-1;
                //Mike.Text = targetOffset.ToString();
                paging.Begin();
            }
            else
            {
                //if not fullscreen
                foreach (TrailerControl item in e.AddedItems)
                {
                    item.Selected();
                }
                //if not first time
                if (e.RemovedItems.Count != 0)
                    foreach (TrailerControl item in e.RemovedItems)
                    {
                        item.Unselected();
                    }
                //the paging animation storyboard
                double itemWidth = TrailerControl.ElementWidth;
                paging = new Storyboard();
                double selectedItemOffset = (defaultViewer.SelectedIndex + 0.5) * itemWidth;  //0.15: we  enlarge the selected item to 130%
                double windowWidth = 1050;
                double targetOffset = windowWidth / 2 - selectedItemOffset;
                // Create a duration of 0.2 seconds.
                Duration duration = new Duration(TimeSpan.FromSeconds(0.2));
                // Create DoubleAnimations and set their properties.
                DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
                myDoubleAnimation1.Duration = duration;
                paging.Duration = duration;
                paging.Children.Add(myDoubleAnimation1);
                Storyboard.SetTarget(myDoubleAnimation1, defaultViewer);
                Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(Canvas.Left)"));
                myDoubleAnimation1.To = targetOffset;//defaultViewer.SelectedIndex*itemWidth*-1;
                paging.Begin();

            }
        }
    }
}
