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

    public class CreateMeetingViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Meeting meeting;
        #endregion

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

            this.ConferenceTypes = new string[] { "TeamViewer", "Custom", "None" };
            this.meeting = new TeamViewer.Meeting();
            this.meeting.ConferenceInformation = new TeamViewer.ConferenceCallInformation();
            this.meeting.ConferenceInformation.Type = this.ConferenceTypes[0];

            this.ConfirmCommand = new Command(this.CreateMeeting, (o) => 
            {
                if (string.IsNullOrWhiteSpace(this.meeting.Subject) || !this.meeting.DateStart.HasValue || !this.meeting.DateEnd.HasValue)
                    return false;
                
                return true;
            });

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

        #region Methods
        /// <summary>
        /// Create new meeting
        /// </summary>
        private void CreateMeeting(object o)
        {
            if (this.meeting.ConferenceInformation.Type == ConferenceTypes[0] && string.IsNullOrWhiteSpace(this.meeting.ConferenceInformation.Data))
                this.meeting.ConferenceInformation = null;

            TeamViewer.Meeting meeting = TeamViewer.Meeting.CreateMeeting(TokenViewModel.GetToken(), this.meeting);

            this.OnMeetingCreationFinished.Invoke(new MeetingCreationEventArgs(this, true, meeting));

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

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

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

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

        public delegate void MeetingCreationEventHandler(MeetingCreationEventArgs e);

        public event MeetingCreationEventHandler OnMeetingCreationFinished;
        #endregion

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

        public class MeetingCreationEventArgs : EventArgs
        {
            public bool Result { get; private set; }
            public TeamViewer.Meeting Meeting { get; private set; }
            public CreateMeetingViewModel Sender { get; private set; }

            public MeetingCreationEventArgs(CreateMeetingViewModel sender, bool result, TeamViewer.Meeting meeting = null)
            {
                this.Sender = sender;
                this.Result = result;
                this.Meeting = meeting;
            }
        }
    }
}
