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

    public class ContactsViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Contacts contacts;
        private TeamViewer.Contact selectedContact;
        private TeamViewer.Groups groups;
        #endregion

        #region Constructor
        public ContactsViewModel()
        {
            this.Header = "Contacts";

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

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

            this.DeleteContactCommand = new Command(this.DeleteContact, (o) =>
            {
                return this.selectedContact != null && !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });

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

        #region Methods
        /// <summary>
        /// Get contacts for account
        /// </summary>
        private void GetContacts(object o)
        {
            this.Groups = TeamViewer.Groups.GetGroups(TokenViewModel.GetToken());
            this.Contacts = TeamViewer.Contacts.GetContacts(TokenViewModel.GetToken());

            if (this.contacts.Items.Length > 0)
                this.SelectedContact = this.contacts.Items[0];
        }

        /// <summary>
        /// Create new contact
        /// </summary>
        private void CreateContact(object o)
        {
            CreateContactViewModel vm = new CreateContactViewModel();
            vm.OnContactCreationFinished += this.OnContactCreationViewClosed;
            Views.CreateContactView view = new Views.CreateContactView(vm);
            MainWindowViewModel.Instance.AddView(view);
        }

        /// <summary>
        /// Delete current contact
        /// </summary>
        private void DeleteContact(object o)
        {
            TeamViewer.Contact.DeleteContact(TokenViewModel.GetToken(), this.selectedContact);

            this.GetContacts(o);
        }
        #endregion

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

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

        /// <summary>
        /// Current selected contact
        /// </summary>
        public TeamViewer.Contact SelectedContact
        {
            get { return this.selectedContact; }
            set
            {
                this.selectedContact = value;

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

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

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

        #region Commands
        public Command GetContactsCommand { get; private set; }
        public Command CreateContactCommand { get; private set; }
        public Command DeleteContactCommand { get; private set; }
        public Command OpenImageUrlCommand { get; private set; }
        #endregion

        #region Events
        private void OnContactCreationViewClosed(CreateContactViewModel.ContactCreationEventArgs e)
        {
            if (e.Result)
            {
                this.Contacts = TeamViewer.Contacts.GetContacts(TokenViewModel.GetToken());

                if (this.contacts != null && this.contacts.Items.Length > 0)
                    this.SelectedContact = this.contacts.Items.FirstOrDefault(x => x.ContactId == e.Contact.ContactId);
            }

            e.Sender.OnContactCreationFinished -= this.OnContactCreationViewClosed;
        }
        #endregion
    }
}
