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

    public class SessionsViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Sessions sessions;
        private TeamViewer.Session selectedSession;
        private TeamViewer.Groups groups;
        #endregion

        #region Constructor
        public SessionsViewModel()
        {
            this.Header = "Sessions";

            this.FullListOption = true;
            this.States = new string[] { "Open", "Closed" };

            this.GetSessionsCommand = new Command(this.GetSessions, (o) => 
            {
                return !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });

            this.SaveSessionCommand = new Command(this.SaveSession, (o) =>
            {
                return this.selectedSession != null;
            });

            this.CreateSessionCommand = new Command(this.CreateSession, (o) =>
            {
                return !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });

            this.OpenCustomerLinkCommand = new Command((args) =>
            {
                if(this.selectedSession != null)
                    System.Diagnostics.Process.Start(this.selectedSession.CustomerLink);
            }, (o) =>
            {
                return this.selectedSession != null && !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });

            this.OpenSupportLinkCommand = new Command((args) =>
            {
                if (this.selectedSession != null)
                    System.Diagnostics.Process.Start(this.selectedSession.SupporterLink);
            }, (o) =>
            {
                return this.selectedSession != null && !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });
        }
        #endregion

        #region Methods
        /// <summary>
        /// Get sessions for account
        /// </summary>
        private void GetSessions(object o)
        {
            this.Sessions = TeamViewer.Sessions.GetSessions(TokenViewModel.GetToken(), fullList: this.FullListOption);
            
            if(this.groups == null)
                this.Groups = TeamViewer.Groups.GetGroups(TokenViewModel.GetToken());

            if (this.sessions != null && this.sessions.Items.Length > 0)
                this.SelectedSession = this.sessions.Items[0];
        }

        /// <summary>
        /// Save current session changes
        /// </summary>
        private void SaveSession(object o)
        {
            TeamViewer.Session.SaveSession(TokenViewModel.GetToken(), this.selectedSession);
        }

        /// <summary>
        /// Create new session
        /// </summary>
        private void CreateSession(object o)
        {
            CreateSessionViewModel vm = new CreateSessionViewModel();
            vm.OnSessionCreationFinished += this.OnSessionCreationViewClosed;
            Views.CreateSessionView view = new Views.CreateSessionView(vm);
            MainWindowViewModel.Instance.AddView(view);
        }
        #endregion

        #region Public properties
        /// <summary>
        /// List of sessions
        /// </summary>
        public TeamViewer.Sessions Sessions
        {
            get { return this.sessions; }
            private set
            {
                this.sessions = value;

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

        /// <summary>
        /// Current selected session
        /// </summary>
        public TeamViewer.Session SelectedSession
        {
            get { return this.selectedSession; }
            set
            {
                this.selectedSession = value;

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

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

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

        /// <summary>
        /// Retreive full session data
        /// </summary>
        public bool FullListOption { get; set; }

        /// <summary>
        /// Available session states
        /// </summary>
        public string[] States { get; private set; }
        #endregion

        #region Commands
        public Command GetSessionsCommand { get; private set; }
        public Command SaveSessionCommand { get; private set; }
        public Command CreateSessionCommand { get; private set; }
        public Command OpenCustomerLinkCommand { get; private set; }
        public Command OpenSupportLinkCommand { get; private set; }
        #endregion

        #region Events
        private void OnSessionCreationViewClosed(CreateSessionViewModel.SessionCreationEventArgs e)
        {
            if (e.Result)
            {
                this.Sessions = TeamViewer.Sessions.GetSessions(TokenViewModel.GetToken(), true);

                if (this.sessions != null && this.sessions.Items.Length > 0)
                    this.SelectedSession = this.sessions.Items.FirstOrDefault(x => x.Code == e.Session.Code);
            }

            e.Sender.OnSessionCreationFinished -= this.OnSessionCreationViewClosed;
        }
        #endregion
    }
}
