client/src/Iri.Modernisation.Controls/ViewModel/AnnotationMaker/AnnotationMakerVM.cs
author totetm <>
Fri, 12 Feb 2010 16:22:57 +0100
changeset 47 9b26023b8c83
parent 46 ab3057b82260
permissions -rw-r--r--
Fixed| bug si on ferme un livre en le lisant ReFixed|faire fonctionner le seek, même quand play n'est pas activer

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using SLExtensions.Input;
using Iri.Modernisation.BaseMVVM.Commands;
using Iri.Modernisation.Data.Models;
using System.Collections.Generic;
using System.Linq;
using Iri.Modernisation.Controls.View;
using System.IO;
using Iri.Modernisation.Data.LDTClass;
namespace Iri.Modernisation.Controls.ViewModel
{

    /// <summary>
    /// ViewModel du module d'annotation
    /// </summary>
    public class AnnotationMakerVM : BaseMVVM.ViewModel.ViewModel
    {
       
        private PolemicElement _refElement;
        /// <summary>
        /// Element référent
        /// </summary>
        public PolemicElement RefElement
        {
            get
            {
                return _refElement;
            }
            set
            {
                _refElement = value;
                if(value!=null)
                {
                _newAnnotation = new Annotation(((PolemicElement)value).Chapter);
                Title = String.Empty;
                Description = String.Empty;
                Tags = String.Empty;
                SelectedType = FactoryVideoLivre.AnnotationDescriptions[0];
                BasicRelation = new PolemicLink() { FromElement = ((PolemicElement)value), ToElement = _newAnnotation, Type = null};
                PolemicRelation = new PolemicLink();
                PolemicRelation.FromElement = _newAnnotation;
                Begin = ((PolemicElement)value).TimerIn.TotalMilliseconds;
                End = ((PolemicElement)value).TimerOut.TotalMilliseconds;
                }
                OnPropertyChanged(String.Empty);
                //OnPropertyChanged("RefElement");
                //OnPropertyChanged("IsControlEnable");
                Commands.AnnotationMaker.FixAnnotationTime.Execute();
                
            }
        }

        /// <summary>
        /// Savoir si le module de liens est activé
        /// </summary>
        public bool IsBinderActive { get; set; }
        /// <summary>
        /// Liste des Tags Référants
        /// </summary>
        public List<String> RefTags
        {
            get
            {
                if (_refElement != null)
                {
                    return _refElement.Tags;
                }
                else
                {
                    return new List<String>();
                }
            }
        }

        /// <summary>
        /// Annotation créé
        /// </summary>
        private Annotation _newAnnotation;

        
        private TimeSpan _begin;
        /// <summary>
        /// Temps de début de l'annotation
        /// </summary>
        public double Begin 
        {
            get
            {
                return _begin.TotalMilliseconds;
            }
            set
            {
                _begin = TimeSpan.FromMilliseconds(value);
                _newAnnotation.TimerIn = TimeSpan.FromMilliseconds(value);
                OnPropertyChanged("Begin");
            }
        }

        
        private TimeSpan _end;
        /// <summary>
        /// Temps de fin de l'annotation
        /// </summary>
        public double End
        {
            get
            {
                return _end.TotalMilliseconds;
            }
            set
            {
                _end = TimeSpan.FromMilliseconds(value);
                _newAnnotation.TimerOut = TimeSpan.FromMilliseconds(value);
                OnPropertyChanged("End");
            }
        }

        private PolemicTypeDescription _selectedType;
        /// <summary>
        /// Convertion Rectange
        /// </summary>
        public PolemicTypeDescription SelectedType
        {
            get
            {
                return _selectedType;
            }
            set
            {
                _selectedType = value;
                OnPropertyChanged("SelectedType");
               // Type = (PolemicElementType)((Rectangle)_selectedType).Resources["PolemicType"];
            }
        }

        private String _title;
        /// <summary>
        /// Titre de l'annotation
        /// </summary>
        public String Title
        {
            get
            {
                if (_title != String.Empty)
                {
                    return _title;
                }
                else
                {
                    return  "+Ajouter un titre";
                   
                }
            }
            set
            {
                _title = value;
                _newAnnotation.Title = value;
                OnPropertyChanged("Title");
            }
        }

        private String _description;

        /// <summary>
        /// Description de l'annotation
        /// </summary>
        public String Description
        {
            get
            {
                if(_description != String.Empty)
                {
                    return _description;
                }
                else
                {
                    return "+Ajouter Description";
                }
            }
            set
            {
                _description = value;
                _newAnnotation.Description = value;
                OnPropertyChanged("Description");
            }

        }

        /// <summary>
        /// Tags de l'annotation
        /// </summary>
        public List<String> _tags =new List<String>();
        /// <summary>
        /// Tags de l'annotation
        /// </summary>
        public String Tags
        {
            get
            {
                if(_tags.Count !=0 || _tags!=null)
                {
                    return String.Join(",", _tags.ToArray());
                }
                else
                {
                    return "+Ajouter Tag";
                }
                
                   
                
            }
            set
            {
                String val = (String)value;
                _tags = val.Split(',').ToList();
                _newAnnotation.Tags = val.Split(',').ToList();
                OnPropertyChanged("Tags");
               
                
            }
        }

     
        private PolemicLink _basicRelation;
        /// <summary>
        /// Relation basique
        /// </summary>
        public PolemicLink BasicRelation
        {
            get
            {
                return _basicRelation;
            }
            set
            {
                _basicRelation = value;
                OnPropertyChanged("BasicRelation");
            }


        }

      
        private PolemicLink _polemicRelation;
        /// <summary>
        /// Relation polémique
        /// </summary>
        public PolemicLink PolemicRelation
        {
            get
            {

                return _polemicRelation;
            }
            set
            {
                _polemicRelation = value;
                OnPropertyChanged("PolemicRelation");
            }


        }

        /// <summary>
        /// Détermine si le control est activé ou non
        /// </summary>
        public bool IsControlEnable
        {
            get
            {
                return _refElement != null;
            }
        }

        /// <summary>
        /// Permet d'initialiser les commands;
        /// </summary>
        private void InitializeCommands()
        {
            Commands.ContextualBinderLayer.BeginBind.Executed += new EventHandler<ExecutedEventArgs>(BeginBind_Executed);
            Commands.ContextualBinderLayer.SelectBind.Executed += new EventHandler<ExecutedEventArgs>(SelectBind_Executed);
            Commands.ContextualBinderLayer.ActiveBind.Executed += new EventHandler<ExecutedEventArgs>(ActiveBind_Executed);
            Commands.ContextualBinderLayer.DesactiveBind.Executed += new EventHandler<ExecutedEventArgs>(DesactiveBind_Executed);
            Commands.AnnotationMaker.OkClick.Executed += new EventHandler<ExecutedEventArgs>(OkClickAnnotationMaker_Executed);
            Commands.EscapeKeyPressed.Executed += new EventHandler<ExecutedEventArgs>(EscapeKeyPressed_Executed);
        }

        void EscapeKeyPressed_Executed(object sender, ExecutedEventArgs e)
        {

            PolemicRelation = new PolemicLink();
            PolemicRelation.FromElement = _newAnnotation;
                
            
        }

        private void DesactiveBind_Executed(object sender, ExecutedEventArgs e)
        {
            IsBinderActive = false;
        }

        private void ActiveBind_Executed(object sender, ExecutedEventArgs e)
        {
            IsBinderActive = true;
        }

      

        private void SelectBind_Executed(object sender, ExecutedEventArgs e)
        {
            if (IsControlEnable && IsBinderActive )
            {
                PolemicRelation.ToElement = (PolemicElement)e.Source;
                OnPropertyChanged("PolemicRelation");
            }
                
        }

        private void BeginBind_Executed(object sender, ExecutedEventArgs e)
        {
            if (IsControlEnable)
            {
                PolemicRelation.Type = ((ContextualLinkBinder)e.Parameter).PolemicType;
                PolemicRelation.ToElement = null;
                OnPropertyChanged("PolemicRelation");  
            }

        }

      

        private void OkClickAnnotationMaker_Executed(object sender, ExecutedEventArgs e)
        {
            _newAnnotation.Type = _selectedType ;
            _basicRelation.ToElement = _newAnnotation;
            _basicRelation.Type = _newAnnotation.Type;
            _refElement.Chapter.Annotations.Add(new Annotation(_newAnnotation));
            _refElement.Chapter.Book.BasicLinks.Add(_basicRelation);
            if(_polemicRelation.ToElement != null)
            {
                _refElement.Chapter.Book.PolemicLinks.Add(_polemicRelation);
            }

            LDTElement newAnnotation = new LDTElement(_newAnnotation);
            
           /* 
            newAnnotation.Title = Title;
            newAnnotation.Abstract = Description;
            newAnnotation.Begin = Begin;
            newAnnotation.Dur = End - Begin;
            newAnnotation.Tags = _tags; 
            newAnnotation.Color = int.Parse(_selectedType.Color.A.ToString() + _selectedType.Color.R.ToString() + _selectedType.Color.G.ToString() + _selectedType.Color.B.ToString());
           */
            
            LDTFile newfile = new LDTFile()
            {
               
                Medias  = new List<LDTMedia>()
                {
                    new LDTMedia()
                    {
                        Src = _refElement.Chapter.Book.IriPath
                    }
                },
                BasicRelations = new List<LDTRelation>()
                {
                    new LDTRelation()
                    {
                        IdElementFrom = _basicRelation.FromElement.Id,
                        IdElementTo = _basicRelation.ToElement.Id,
                        //Title = _basicRelation.Title
                    }
                },

                Annotations = new List<LDTAnnotationsContent>()
                {
                    new LDTAnnotationsContent()
                    {
                        Content = new List<LDTAnnotationsDecoupage>()
                        {
                            new LDTAnnotationsDecoupage()
                            {
                                Elements = new List<LDTElement>()
                                {
                                    newAnnotation
                                }
                            }
                        }
                    }
                }

            };
            if (this._polemicRelation.ToElement != null)
            {
                LDTRelation polemicRelation = new LDTRelation()
                {
                    IdElementFrom = _polemicRelation.FromElement.Id,
                    IdElementTo = _polemicRelation.ToElement.Id,
                    Title = _polemicRelation.Title
                };
                newfile.BasicRelations.Add(polemicRelation);
            }


            //MessageBox.Show(newfile.ToString());

            /* HttpSend For Web Mode */
            Application app = Application.Current as Application;
            HttpSender helper = new HttpSender(new Uri(FactoryVideoLivre.UpdateBookService, UriKind.RelativeOrAbsolute), "POST",
                new KeyValuePair<string, string>("newAnnotation",System.Windows.Browser.HttpUtility.UrlEncode(
                                                                    System.Windows.Browser.HttpUtility.HtmlEncode(
                                                                        _refElement.Chapter.Book.GetLDTFile().ToString())
                                                    )),
                new KeyValuePair<String,String>("ldtFileURI",_refElement.Chapter.Book.LdtPath));
            helper.ResponseComplete += new HttpResponseCompleteEventHandler(helper_ResponseComplete);
            helper.Execute();
             
      



            Commands.AnnotationMaker.NewAnnotationFinished.Execute(_newAnnotation,_refElement);

            /********/


            _refElement = null;
            Title = String.Empty;
            Description = String.Empty;
            Tags = String.Empty;
            BasicRelation = null;
            PolemicRelation = null;

            OnPropertyChanged("IsControlEnable");
         
           
        }
        
        private String _sendAnnotationResponse;
        public String SendAnnotationResponse
        {
            get
            {
                return _sendAnnotationResponse;
            }
            set
            {
                _sendAnnotationResponse = value;
                OnPropertyChanged("SendAnnotationResponse");
            }
        }
        void helper_ResponseComplete(HttpResponseCompleteEventArgs e)
        {
            SendAnnotationResponse = e.Response;
            Commands.HttpSenderResponse.Execute(e.Response);
        
        }

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        public AnnotationMakerVM()
        {

            InitializeCommands();

        }

        /// <summary>
        /// Constructeur par référence
        /// </summary>
        /// <param name="refAParam">Element référant</param>
        public AnnotationMakerVM(PolemicElement refAParam)
        {
            InitializeCommands();
            _refElement = refAParam;
            _newAnnotation = new Annotation(RefElement.Chapter);
           
            _basicRelation = new PolemicLink() { FromElement = RefElement, ToElement = _newAnnotation, Type = null};
            _polemicRelation = new PolemicLink();
            _polemicRelation.FromElement = _newAnnotation;
        
           // _begin = RefElement.TimerIn;
           // _end = RefElement.TimerOut;

            OnPropertyChanged(String.Empty);
        }


        public PolemicTypeDescription[] ListAnnotationDescription
        {
            get
            {
                return FactoryVideoLivre.AnnotationDescriptions;
            }
           
        }



    }
   
}