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.Screen;


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;

                //GRISAGE
                //Grid1
                if (Grid1.Width < (1024 / 4) || Grid2.Height < (768 / 4))
                {
                    try
                    {Grid1.Children[1].Visibility = Visibility.Hidden;}
                    catch (Exception) { }
                }
                else
                {
                    try
                    { Grid1.Children[1].Visibility = Visibility.Visible; }
                    catch (Exception) { }
                }
                //Grid2
                if (Grid2.Width < (1024 / 4) || Grid2.Height < (768 / 4))
                {
                    try
                    { Grid2.Children[1].Visibility = Visibility.Hidden; }
                    catch (Exception) { }
                }
                else
                {
                    try
                    { Grid2.Children[1].Visibility = Visibility.Visible; }
                    catch (Exception) { }
                }
                //Grid3
                if (Grid3.Width < (1024 / 4) || Grid3.Height < (768 / 4))
                {
                    try
                    { Grid3.Children[1].Visibility = Visibility.Hidden; }
                    catch (Exception) { }
                }
                else
                {
                    try
                    { Grid3.Children[1].Visibility = Visibility.Visible; }
                    catch (Exception) { }
                }
                //Grid4
                if (Grid4.Width < (1024 / 4) || Grid4.Height < (768 / 4))
                {
                    try
                    { Grid4.Children[1].Visibility = Visibility.Hidden; }
                    catch (Exception) { }
                }
                else
                {
                    try
                    { Grid4.Children[1].Visibility = Visibility.Visible; }
                    catch (Exception) { }
                }
            }
            catch (Exception) { }
        }

        private void UserControlPivot_EH_SurfaceButtonPlayer1_ContactDown(object sender, EventArgs e)
        {
            
            if (!isCreatedP1)
            {
                //1-Creation du control Screen
                UserControlScreen Screen1 = new UserControlScreen();
                Screen1.Name = "Screen1";
                Screen1.contexteGrid = Grid1.Name.ToString();
                //2-Rajout du screen dans la grid correspondante
                Grid1.Children.Add(Screen1);

                Screen1.UC_Screen_NewSession += new System.EventHandler(this.SessionInput_EH_SurfaceButtonSubmit_ContactDown);
            }            
        }

        private void UserControlPivot_EH_SurfaceButtonPlayer2_ContactDown(object sender, EventArgs e)
        {
            if (!isCreatedP2)
            {
               
            } 
        }

        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_ItemVideo2_ContactDown(object sender, EventArgs e)
        {
            //ToDo
        }

        //Button Submit du control SessionInput
        private void SessionInput_EH_SurfaceButtonSubmit_ContactDown(object sender, EventArgs e)
        {
            try
            {
                //3-Suppression du UC Screen
                Grid root = (Grid)(((UserControlScreen)sender).Parent);
                root.Children.Remove(((UserControlScreen)sender));
                //4-Creation du User Panel
                UserControlUserPanel UserPanel1 = new UserControlUserPanel();
                UserPanel1.Name = "UserPanel1";
                //5-Rajout sur la Grid Root
                root.Children.Add(UserPanel1);
                
            }
            catch (Exception ex) { }
        }



    }
}