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

    public class GroupsViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Groups groups;
        private TeamViewer.Group selectedGroup;
        #endregion

        #region Constructor
        public GroupsViewModel()
        {
            this.Header = "Groups";

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

            this.SaveGroupCommand = new Command(this.SaveGroup, (o) =>
            {
                return this.selectedGroup != null && !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });

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

            this.DeleteGroupCommand = new Command(this.DeleteGroup, (o) =>
            {
                return this.selectedGroup != null && !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });
        }
        #endregion

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

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

        /// <summary>
        /// Save current contact changes
        /// </summary>
        private void SaveGroup(object o)
        {
            TeamViewer.Group.SaveGroup(TokenViewModel.GetToken(), selectedGroup);
        }

        /// <summary>
        /// Create new contact
        /// </summary>
        private void CreateGroup(object o)
        {
            CreateGroupViewModel vm = new CreateGroupViewModel();
            vm.OnGroupCreationFinished += this.OnGroupCreationViewClosed;
            Views.CreateGroupView view = new Views.CreateGroupView(vm);
            MainWindowViewModel.Instance.AddView(view);
        }

        /// <summary>
        /// Delete current contact
        /// </summary>
        private void DeleteGroup(object o)
        {
            TeamViewer.Group.DeleteGroup(TokenViewModel.GetToken(), this.selectedGroup);

            this.GetGroups(o);
        }
        #endregion

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

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

        /// <summary>
        /// Current selected contact
        /// </summary>
        public TeamViewer.Group SelectedGroup
        {
            get { return this.selectedGroup; }
            set
            {
                this.selectedGroup = value;

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

        #region Commands
        public Command GetGroupsCommand { get; private set; }
        public Command SaveGroupCommand { get; private set; }
        public Command CreateGroupCommand { get; private set; }
        public Command DeleteGroupCommand { get; private set; }
        #endregion

        #region Events
        private void OnGroupCreationViewClosed(CreateGroupViewModel.GroupCreationEventArgs e)
        {
            if (e.Result)
            {
                this.Groups = TeamViewer.Groups.GetGroups(TokenViewModel.GetToken());

                if (this.groups != null && this.groups.Items.Length > 0)
                    this.SelectedGroup = this.groups.Items.FirstOrDefault(x => x.GroupId == e.Group.GroupId);
            }

            e.Sender.OnGroupCreationFinished -= this.OnGroupCreationViewClosed;
        }
        #endregion
    }
}
