using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

using FingersDance.Control.UserPanel;
using FingersDance.Control.ListVideo;
using FingersDance.Control.SessionInput;

namespace FingersDance
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class SurfaceWindow1 : SurfaceWindow
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// 
        #region Variables
        private bool isCreatedP1 = false;
        private bool isCreatedP2 = false;
        private bool isCreatedP3 = false;
        private bool isCreatedP4 = false;
        #endregion

        ActionFactory.ActionGenerator _Factory = new FingersDance.ActionFactory.ActionFactory().GetGenerator();

        public SurfaceWindow1()
        {
            InitializeComponent();

            //Permet d'initialiser l'inertie du Pivot une fois relaché.
            InitPivotInertia();
            //Permet d'initialiser les quand Grid à la position initiale du pivot
            InitGridPositions();
            // Add handlers for Application activation events
            AddActivationHandlers();
        }

        private void InitPivotInertia()
        {
            try
            {
                ScatterViewItem item = (ScatterViewItem)scaterview.Items.GetItemAt(0);
                item.DecelerationRate = double.NaN;
            }
            catch (Exception ex) { }
        }

        private void InitGridPositions()
        {
            //Initialise les 4 Grids à la positions initiale du Pivot.   
            try
            {
                ScatterViewItem item = (ScatterViewItem)scaterview.Items.GetItemAt(0);
                Grid1.Width =item.Center.X;
                Grid1.Height = item.Center.Y;
                
                Grid2.Width = item.Center.X;
                Grid2.Height = 768 - item.Center.Y;

                Grid3.Width = 1024 - item.Center.X;
                Grid3.Height = 768 - item.Center.Y;

                Grid4.Width = 1024 - item.Center.X;
                Grid4.Height = item.Center.Y;
            }
            catch (Exception ex) { }
        }


        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        private void scaterview_ContactChanged(object sender, ContactEventArgs e)
        {
            //Permet la MAJ des 4 Grids à la position du pivot
            /*try
            {
                Grid1.Width = e.GetPosition(mainSurfaceWindow).X;
                Grid1.Height = e.GetPosition(mainSurfaceWindow).Y;

                Grid3.Width = e.GetPosition(mainSurfaceWindow).X;
                Grid3.Height = 768 - e.GetPosition(mainSurfaceWindow).Y;

                Grid4.Width = 1024 - e.GetPosition(mainSurfaceWindow).X;
                Grid4.Height = 768 - e.GetPosition(mainSurfaceWindow).Y;

                Grid2.Width = 1024 - e.GetPosition(mainSurfaceWindow).X;
                Grid2.Height = e.GetPosition(mainSurfaceWindow).Y;
            }
            catch (Exception ex) { }*/
        }

        private void ScatterViewItemPivot_ContactChanged(object sender, ContactEventArgs e)
        {
            //Permet la MAJ des 4 Grids à la position du pivot
            try
            {
                Grid1.Width = e.GetPosition(mainSurfaceWindow).X;
                Grid1.Height = e.GetPosition(mainSurfaceWindow).Y;

                Grid3.Width = e.GetPosition(mainSurfaceWindow).X;
                Grid3.Height = 768 - e.GetPosition(mainSurfaceWindow).Y;

                Grid4.Width = 1024 - e.GetPosition(mainSurfaceWindow).X;
                Grid4.Height = 768 - e.GetPosition(mainSurfaceWindow).Y;

                Grid2.Width = 1024 - e.GetPosition(mainSurfaceWindow).X;
                Grid2.Height = e.GetPosition(mainSurfaceWindow).Y;
            }
            catch (Exception ex) { }
        }

        private void UserControlPivot_EH_SurfaceButtonPlayer1_ContactDown(object sender, EventArgs e)
        {
            
            if (!isCreatedP1)
            {
                UserControlListVideo ListVideo1 = new UserControlListVideo();
                ListVideo1.Name = "ListVideo1";
                
                Grid1.Children.Add(ListVideo1);

                //Creation des Events pour chaque item de la video
                ListVideo1.EH_ItemVideo1_ContactDown += new System.EventHandler(this.ListVideo1_EH_ItemVideo1_ContactDown);
                ListVideo1.EH_ItemVideo2_ContactDown += new System.EventHandler(this.ListVideo1_EH_ItemVideo2_ContactDown);
                /*
                UserControlUserPanel UserPanel1 = new UserControlUserPanel();
                UserPanel1.Name = "UserPanel1";
                Grid1.Children.Add(UserPanel1);
                isCreatedP1 = true;*/
            }            
        }

        private void UserControlPivot_EH_SurfaceButtonPlayer2_ContactDown(object sender, EventArgs e)
        {
            if (!isCreatedP2)
            {
                UserControlUserPanel UserPanel2 = new UserControlUserPanel();
                UserPanel2.Name = "UserPanel2";
                Grid2.Children.Add(UserPanel2);
                isCreatedP2 = true;
            } 
        }

        private void UserControlPivot_EH_SurfaceButtonPlayer3_ContactDown(object sender, EventArgs e)
        {
            if (!isCreatedP3)
            {
                UserControlUserPanel UserPanel3 = new UserControlUserPanel();
                UserPanel3.Name = "UserPanel3";
                Grid3.Children.Add(UserPanel3);
                isCreatedP3 = true;
            } 
        }

        private void UserControlPivot_EH_SurfaceButtonPlayer4_ContactDown(object sender, EventArgs e)
        {
            if (!isCreatedP4)
            {
                UserControlUserPanel UserPanel4 = new UserControlUserPanel();
                UserPanel4.Name = "UserPanel4";
                Grid4.Children.Add(UserPanel4);
                isCreatedP4 = true;
            } 
        }

        private void ListVideo1_EH_ItemVideo1_ContactDown(object sender, EventArgs e)
        {

            //Creation d'un ScatterView Item
            //ScatterViewItem scatterViewItemSessionInput = new ScatterViewItem();
            //scatterViewItemSessionInput.Name = "ScatterViewItemSessionInput";
            
            
            //Creation d'une nouvelle seance.
                
            UserControlSessionInput SessionInput = new UserControlSessionInput();
            SessionInput.Name = "SessionInput";

            try{
                Grid actualGrid = (Grid)(((UserControlListVideo)sender).Parent);
                actualGrid.Children.Add(SessionInput);

                SessionInput.EH_SurfaceButtonSubmit_ContactDown += new System.EventHandler(this.SessionInput_EH_SurfaceButtonSubmit_ContactDown);
                actualGrid.Children.Remove((UserControlListVideo)sender);

                //Init Scatter View Properties and Add it to the scatter view.
                /*scatterViewItemSessionInput.Content = SessionInput;
                scatterViewItemSessionInput.CanMove = true;
                scatterViewItemSessionInput.CanScale = false;
                scatterViewItemSessionInput.CanRotate = true;
                scatterViewItemSessionInput.Center = new Point((double)actualGrid.ActualWidth,(double)actualGrid.ActualHeight);
                scaterview.Items.Add(scatterViewItemSessionInput);*/
            }
            catch (Exception ex) { }
            isCreatedP1 = true;
        }

        private void ListVideo1_EH_ItemVideo2_ContactDown(object sender, EventArgs e)
        {
            //ToDo
        }

        //Button Submit du control SessionInput
        private void SessionInput_EH_SurfaceButtonSubmit_ContactDown(object sender, EventArgs e)
        {
            try
            {
                Grid actualGrid = (Grid)(((UserControlSessionInput)sender).Parent);
                actualGrid.Children.Remove((UserControlSessionInput)sender);
                //Creation du User Panel
                UserControlUserPanel UserPanel1 = new UserControlUserPanel();
                UserPanel1.Name = "UserPanel1";
                actualGrid.Children.Add(UserPanel1);
                
            }
            catch (Exception ex) { }
        }



    }
}