namespace TeamViewerApi.ViewModels
{
    #region Using
    using System;
    using System.Collections.Generic;
    using System.Linq;
    #endregion

    public class CreateSessionViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Session session;
        private TeamViewer.Groups groups;
        private string customGroupName;
        #endregion

        #region Constructor
        public CreateSessionViewModel()
        {
            this.Header = "Create meeting";

            this.session = new TeamViewer.Session();
            this.session.Customer = new TeamViewer.User();
            this.groups = TeamViewer.Groups.GetGroups(TokenViewModel.GetToken());

            if (this.groups != null)
                this.session.GroupName = this.groups.Items[0].Name;

            this.ConfirmCommand = new Command(this.CreateSession, (o) => 
            {
                bool result = true;

                if (string.IsNullOrWhiteSpace(this.session.GroupName) && string.IsNullOrWhiteSpace(this.customGroupName))
                    result = false;

                if (string.IsNullOrWhiteSpace(this.session.Customer.Name))
                    result = false;

                if (string.IsNullOrWhiteSpace(this.session.CustomerDescription))
                    result = false;

                if (this.session.DateIsValid < DateTime.Now)
                    result = false;

                return result;
            });

            this.CancelCommand = new Command((args) => 
            {
                this.OnSessionCreationFinished.Invoke(new SessionCreationEventArgs(this, false));
                
                MainWindowViewModel.Instance.RemoveView(this);
            });
        }
        #endregion

        #region Methods
        /// <summary>
        /// Create new meeting
        /// </summary>
        private void CreateSession(object o)
        {
            if (!string.IsNullOrWhiteSpace(this.CustomGroupName))
                this.session.GroupName = this.CustomGroupName;

            this.session.DateIsValid = this.session.DateIsValid.Value.ToUniversalTime();

            TeamViewer.Session session = TeamViewer.Session.CreateSession(TokenViewModel.GetToken(), this.session);

            this.OnSessionCreationFinished.Invoke(new SessionCreationEventArgs(this, true, session));

            MainWindowViewModel.Instance.RemoveView(this);
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Meeting
        /// </summary>
        public TeamViewer.Session Session
        {
            get { return this.session; }
            set
            {
                this.session = value;

                this.RaisePropertyChanged(() => this.Session);
            }
        }

        /// <summary>
        /// List of groups
        /// </summary>
        public TeamViewer.Groups Groups
        {
            get { return this.groups; }
            private set
            {
                this.groups = value;

                this.RaisePropertyChanged(() => this.Groups);
            }
        }

        /// <summary>
        /// Custom contact name for meeting
        /// </summary>
        public string CustomGroupName
        {
            get { return this.customGroupName; }
            set
            {
                if (this.customGroupName != value)
                {
                    this.customGroupName = value;

                    this.RaisePropertyChanged(() => this.CustomGroupName);
                }
            }
        }

        public delegate void SessionCreationEventHandler(SessionCreationEventArgs e);

        public event SessionCreationEventHandler OnSessionCreationFinished;
        #endregion

        #region Commands
        public Command ConfirmCommand { get; private set; }
        public Command CancelCommand { get; private set; }
        #endregion

        public class SessionCreationEventArgs : EventArgs
        {
            public bool Result { get; private set; }
            public TeamViewer.Session Session { get; private set; }
            public CreateSessionViewModel Sender { get; private set; }

            public SessionCreationEventArgs(CreateSessionViewModel sender, bool result, TeamViewer.Session session = null)
            {
                this.Sender = sender;
                this.Result = result;
                this.Session = session;
            }
        }
    }
}
