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 UserControlUserPanel Panel1 = null;
        private UserControlUserPanel Panel2 = null;
        private UserControlUserPanel Panel3 = null;
        private UserControlUserPanel Panel4 = null;
        #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)
        {  }

        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_SurfaceButtonPlayer_ContactDown(object sender, EventArgs e)
        {
            switch ((int)sender)
            {
                case 1:
                    if (Panel1 == null)
                    {
                        //1-Creation du control Screen
                        UserControlScreen Screen = new UserControlScreen(1);
                        Screen.Name = "Screen1";
                        Screen.contexteGrid = Grid1.Name.ToString();
                        //2-Rajout du screen dans la grid correspondante
                        Grid1.Children.Add(Screen);

                        Screen.UC_Screen_NewSession += new System.EventHandler(this.SessionInput_EH_SurfaceButtonSubmit_ContactDown);
                    }
                    break;
                case 2:
                    if (Panel2 == null)
                    {
                        //1-Creation du control Screen
                        UserControlScreen Screen = new UserControlScreen(2);
                        Screen.Name = "Screen2";
                        Screen.contexteGrid = Grid2.Name.ToString();
                        //2-Rajout du screen dans la grid correspondante
                        Grid2.Children.Add(Screen);

                        Screen.UC_Screen_NewSession += new System.EventHandler(this.SessionInput_EH_SurfaceButtonSubmit_ContactDown);
                    }
                    break;
                case 3:
                    if (Panel3 == null)
                    {
                        //1-Creation du control Screen
                        UserControlScreen Screen = new UserControlScreen(3);
                        Screen.Name = "Screen3";
                        Screen.contexteGrid = Grid3.Name.ToString();
                        //2-Rajout du screen dans la grid correspondante
                        Grid3.Children.Add(Screen);

                        Screen.UC_Screen_NewSession += new System.EventHandler(this.SessionInput_EH_SurfaceButtonSubmit_ContactDown);
                    }
                    break;
                case 4:
                    if (Panel4 == null)
                    {
                        //1-Creation du control Screen
                        UserControlScreen Screen = new UserControlScreen(4);
                        Screen.Name = "Screen4";
                        Screen.contexteGrid = Grid4.Name.ToString();
                        //2-Rajout du screen dans la grid correspondante
                        Grid4.Children.Add(Screen);

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

        //Button Submit du control SessionInput
        private void SessionInput_EH_SurfaceButtonSubmit_ContactDown(object sender, EventArgs e)
        {
            try
            {
                //1-Suppression du UC Screen
                Grid root = (Grid)(((UserControlScreen)sender).Parent);
                root.Children.Remove(((UserControlScreen)sender));
                //2-Creation du User Panel
                switch (((UserControlScreen)sender).id)
                {
                    case 1:
                        Panel1 = new UserControlUserPanel(((UserControlScreen)sender).Path);
                        Panel1.Name = "UserPanel1";
                        //3-Rajout sur la Grid Root
                        root.Children.Add(Panel1);
                        break;
                    case 2:
                        Panel2 = new UserControlUserPanel(((UserControlScreen)sender).Path);
                        Panel2.Name = "UserPanel2";
                        //3-Rajout sur la Grid Root
                        root.Children.Add(Panel2);
                        break;
                    case 3:
                        Panel3 = new UserControlUserPanel(((UserControlScreen)sender).Path);
                        Panel3.Name = "UserPanel3";
                        //3-Rajout sur la Grid Root
                        root.Children.Add(Panel3);
                        break;
                    case 4:
                        Panel4 = new UserControlUserPanel(((UserControlScreen)sender).Path);
                        Panel4.Name = "UserPanel4";
                        //3-Rajout sur la Grid Root
                        root.Children.Add(Panel4);
                        break;
                }
            }
            catch (Exception ex) { }
        }



    }
}