using System;
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 FingersDance.Data;
using Microsoft.Surface.Presentation;
using System.Globalization;
using Microsoft.Surface.Presentation.Controls;
using System.Reflection;
using FingersDance.ViewModels;

namespace FingersDance.Control.UserPanel
{
	public partial class UserControlUserPanel
	{
        #region Variables

        //public ContactEventHandler ContactDown;
        public event EventHandler OnSuccessAnnotation;  //Event to display ColorAnnotation from different Users   
        public event EventHandler OnTagVisualisation; //Event to Mute all Players
        public int id = 0;
        public uint idcolor = 0;    //The color of the Pivot Button
        //Project _Project = new Project();
        private CuttingViewModel cutVM;

        //variables for TagSound Control
        double baseOrientation = 0;
        double deltaOrientation = 0;
        double lastdeltaOrientation = 0;
        int lastSoundlevel = 0;
        bool rise = true; //rise is true if the curse of the volume is positive
        #endregion

        #region Properties
        public CuttingViewModel CuttingVM
        {
            get { return cutVM; }
            set { cutVM = value; }
        }
        #endregion

        //#region Properties
        //public Project Project
        //{
        //    get { return _Project; }
        //    set { _Project = value; }
        //}
        //#endregion

        #region Constructors
        public UserControlUserPanel()
		{
			this.InitializeComponent();

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

        public UserControlUserPanel(int idPar, Color col, CuttingViewModel cutVMPar, string path)
        {
            this.InitializeComponent();
            id = idPar;
            //_Project = p;
            cutVM = cutVMPar;
            this.UserControlSyncSource.Load(path, col, cutVM);
            UserControlSyncSource.OnSuccessAnnotation+=new EventHandler(UserControlSyncSource_OnSuccessAnnotation);
            //SAR -Initialize Tag Values
            InitializeDefinitions();
        }
        #endregion

        #region Tag Management

        private void InitializeDefinitions()
        {
            for (byte k = 1; k <= 5; k++)
            {
                ByteTagVisualizationDefinition tagDef = new ByteTagVisualizationDefinition();
                // The tag value that this definition will respond to. 
                tagDef.Value = k;
                // The .xaml file for the UI. 
                tagDef.Source = new Uri("FingersDance.Control.UserPanel;Component/TagVisuSoundControl.xaml", UriKind.Relative);

                // The maximum number for this tag value. tagDef.MaxCount = 2; 
                // The visualization stays for 2 seconds after the tag is lifted. 
                tagDef.LostTagTimeout = 2000.0;
                // The orientation offset (default). 
                tagDef.OrientationOffsetFromTag = 0.0;
                // The physical offset (horizontal inches, vertical inches). 
                tagDef.PhysicalCenterOffsetFromTag = new Vector(0.5, 0.25);
                // The tag removal behavior (default). 
                tagDef.TagRemovedBehavior = TagRemovedBehavior.Fade;
                // Orient UI to tag? (default). 
                tagDef.UsesTagOrientation = true;
                // Add the definition to the collection. 
                this.tagVisualizer.Definitions.Add(tagDef);
            }
        }

        //SAR - Event Rised when Tag Control is moved or rotated
        private void tagVisualizer_VisualizationMoved(object sender, TagVisualizerEventArgs e)
        {
            /*TagVisuSoundControl tagsoundcontrol = (TagVisuSoundControl)e.TagVisualization;
            BindingExpression be = tagsoundcontrol.volumeModel.GetBindingExpression(Label.ContentProperty);
            be.UpdateSource();*/

            TagVisuSoundControl tagsoundcontrol = (TagVisuSoundControl)e.TagVisualization;

            deltaOrientation = (tagsoundcontrol.Orientation - baseOrientation) % 360;
            if (deltaOrientation < 0)
                deltaOrientation = 360 + deltaOrientation;
            int niveau = (int)(deltaOrientation / 36);

            //if (((niveau == 9) || (niveau == 8) || (niveau == 7)) && lastSoundlevel == 0)
            if (((niveau == 9) || (niveau == 8)) && lastSoundlevel == 0)
                rise = false;
            //else if (((niveau == 1) || (niveau == 2) || (niveau == 3)) && lastSoundlevel == 0)
            else if (((niveau == 1) || (niveau == 2)) && lastSoundlevel == 0)
                rise = true;

            
            if (!rise)
            {
                niveau = (niveau - 10) % 10;
            }
            //ToDo
            //--1 Mute all other Players

            //--2 Elipse visibility
            tagsoundcontrol.TagVisuSoundLevelUpdate(niveau);
            //Set the Sound of the Player.
            if (Math.Abs(lastSoundlevel - niveau) == 1)
            {
                UserControlSyncSource.setUserPlayerVolume((double)niveau / 10.0);
            }
            lastSoundlevel = niveau;
            tagsoundcontrol.volumeModel.Content = niveau;//deltaOrientation.ToString();

        }
        private void OnVisualizationAdded(object sender, TagVisualizerEventArgs e)
        {
            TagVisuSoundControl tagsoundcontrol = (TagVisuSoundControl)e.TagVisualization;

            switch (tagsoundcontrol.VisualizedTag.Byte.Value)
            {
                case 1:
                    try
                    {
                        /*
                        Binding mybinding = new Binding("Orientation");
                        mybinding.Source = tagsoundcontrol;
                        mybinding.UpdateSourceTrigger= UpdateSourceTrigger.PropertyChanged ;
                        tagsoundcontrol.volumeModel.SetBinding(Label.ContentProperty,mybinding);
                        */

                        baseOrientation = tagsoundcontrol.Orientation % 360;
                        deltaOrientation = ((tagsoundcontrol.Orientation % 360) - baseOrientation) % 360;
                        tagsoundcontrol.volumeModel.Content = deltaOrientation;

                        //RiseEvent To Mute all other Players
                        OnTagVisualisation(this, new EventArgs());
                        PlayerMute(false);
                    }
                    catch (Exception ex)
                    {
                    }
                    break;
                case 2: tagsoundcontrol.volumeModel.Content = "Tag Value 2";
                    break;
                case 3: tagsoundcontrol.volumeModel.Content = "Tag Value 3";
                    break;
                case 4: tagsoundcontrol.volumeModel.Content = "Tag Value 4";
                    break;
                case 5: tagsoundcontrol.volumeModel.Content = "Tag Value 5";
                    break;
                default: tagsoundcontrol.volumeModel.Content = "UNKNOWN MODEL";
                    break;
            }
        }

        #endregion

        public void PlayerPause()
        {
            UserControlSyncSource.PlayerPause();
        }
        public void PlayerMute(bool b)
        {
            UserControlSyncSource.setUserPlayerMute(b);
        }

        private void UserControlSyncSource_OnSuccessAnnotation(object sender, EventArgs e)
        {
            OnSuccessAnnotation(this, new EventArgs());
        }

        public void UserControlSyncSource_DisplayAnnotation(int id, Brush b)
        {
            UserControlSyncSource.UserControlPlayer_DisplayAnnotation(id, b);
        }  
    }
}