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

    public class UsersViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Users users;
        private TeamViewer.User selectedUser;
        #endregion

        #region Constructor
        public UsersViewModel()
        {
            this.Header = "Users";

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

            this.SaveUserCommand = new Command(this.SaveUser, (o) =>
            {
                return this.selectedUser != null && !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });
        }
        #endregion

        #region Methods
        /// <summary>
        /// Get users for account
        /// </summary>
        private void GetUsers(object o)
        {
            this.Users = TeamViewer.Users.GetUsers(TokenViewModel.GetToken(), fullList: this.FullListOption);

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

        /// <summary>
        /// Save current user changes
        /// </summary>
        private void SaveUser(object o)
        {
            //TeamViewer.Account.SaveAccount(TokenViewModel.GetToken(), account);
        }
        #endregion

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

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

        /// <summary>
        /// Current selected user
        /// </summary>
        public TeamViewer.User SelectedUser
        {
            get { return this.selectedUser; }
            set
            {
                this.selectedUser = value;

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

        /// <summary>
        /// Retrieve full user data
        /// </summary>
        public bool FullListOption { get; set; }
        #endregion

        #region Commands
        public Command GetUsersCommand { get; private set; }
        public Command SaveUserCommand { get; private set; }
        #endregion
    }
}
