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

    public class CreateContactViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Contact contact;
        private TeamViewer.Groups groups;
        private string customGroupName;
        #endregion

        #region Constructor
        public CreateContactViewModel()
        {
            this.Header = "Create contact";

            this.contact = new TeamViewer.Contact();
            this.groups = TeamViewer.Groups.GetGroups(TokenViewModel.GetToken());

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

            this.ConfirmCommand = new Command(this.CreateContact, (o) => 
            {
                return !string.IsNullOrWhiteSpace(this.contact.Email) && !string.IsNullOrWhiteSpace(this.contact.GroupName);
            });

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

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

            TeamViewer.Contact contact = TeamViewer.Contact.CreateContact(TokenViewModel.GetToken(), this.contact);

            this.OnContactCreationFinished.Invoke(new ContactCreationEventArgs(this, true, contact));

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

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

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

        /// <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 contact
        /// </summary>
        public string CustomGroupName
        {
            get { return this.customGroupName; }
            set
            {
                if (this.customGroupName != value)
                {
                    this.customGroupName = value;

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

        public delegate void ContactCreationEventHandler(ContactCreationEventArgs e);

        public event ContactCreationEventHandler OnContactCreationFinished;
        #endregion

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

        public class ContactCreationEventArgs : EventArgs
        {
            public bool Result { get; private set; }
            public TeamViewer.Contact Contact { get; private set; }
            public CreateContactViewModel Sender { get; private set; }

            public ContactCreationEventArgs(CreateContactViewModel sender, bool result, TeamViewer.Contact contact = null)
            {
                this.Sender = sender;
                this.Result = result;
                this.Contact = contact;
            }
        }
    }
}
