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

    public class MeetingsViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Meetings meetings;
        private TeamViewer.Meeting selectedMeeting;
        #endregion

        #region Constructor
        public MeetingsViewModel()
        {
            this.Header = "Meetings";

            this.ConferenceTypes = new string[] { "TeamViewer", "Custom", "None" };

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

            this.SaveMeetingCommand = new Command(this.SaveMeeting, (o) =>
            {
                return this.selectedMeeting != null;
            });

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

            this.DeleteMeetingCommand = new Command(this.DeleteMeeting, (o) =>
            {
                return this.selectedMeeting != null;
            });

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

            this.CreateInstantMeetingCommand = new Command((args) =>
            {
                TeamViewer.Meeting instant = TeamViewer.Meeting.CreateInstantMeeting(TokenViewModel.GetToken());

                if (instant != null)
                    System.Diagnostics.Process.Start(instant.Link);
            }, (o) =>
            {
                return !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });
        }
        #endregion

        #region Methods
        /// <summary>
        /// Get meetings for account
        /// </summary>
        private void GetMeetings(object o)
        {
            this.Meetings = TeamViewer.Meetings.GetMeetings(TokenViewModel.GetToken());

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

        /// <summary>
        /// Save current meeting changes
        /// </summary>
        private void SaveMeeting(object o)
        {
            TeamViewer.Meeting.SaveMeeting(TokenViewModel.GetToken(), this.selectedMeeting);
        }

        /// <summary>
        /// Create new meeting
        /// </summary>
        private void CreateMeeting(object o)
        {
            CreateMeetingViewModel vm = new CreateMeetingViewModel();
            vm.OnMeetingCreationFinished += this.OnMeetingCreationViewClosed;
            Views.CreateMeetingView view = new Views.CreateMeetingView(vm);
            MainWindowViewModel.Instance.AddView(view);
        }

        /// <summary>
        /// Delete current meeting
        /// </summary>
        private void DeleteMeeting(object o)
        {
            TeamViewer.Meeting.DeleteMeeting(TokenViewModel.GetToken(), this.selectedMeeting);

            this.Meetings = TeamViewer.Meetings.GetMeetings(TokenViewModel.GetToken());

            if (this.meetings != null && this.meetings.Items.Length > 0)
                this.SelectedMeeting = this.meetings.Items[0];
        }
        #endregion

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

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

        /// <summary>
        /// Current selected meeting
        /// </summary>
        public TeamViewer.Meeting SelectedMeeting
        {
            get { return this.selectedMeeting; }
            set
            {
                this.selectedMeeting = value;

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

        public string[] ConferenceTypes { get; private set; }
        #endregion

        #region Commands
        public Command GetMeetingsCommand { get; private set; }
        public Command SaveMeetingCommand { get; private set; }
        public Command CreateMeetingCommand { get; private set; }
        public Command CreateInstantMeetingCommand { get; private set; }
        public Command DeleteMeetingCommand { get; private set; }
        public Command OpenParticipantLinkCommand { get; private set; }
        #endregion

        #region Events
        private void OnMeetingCreationViewClosed(CreateMeetingViewModel.MeetingCreationEventArgs e)
        {
            if (e.Result)
            {
                this.Meetings = TeamViewer.Meetings.GetMeetings(TokenViewModel.GetToken());

                if (this.meetings != null && this.meetings.Items.Length > 0)
                    this.SelectedMeeting = this.meetings.Items.FirstOrDefault(x => x.MeetingId == e.Meeting.MeetingId);
            }

            e.Sender.OnMeetingCreationFinished -= this.OnMeetingCreationViewClosed;
        }
        #endregion
    }
}
