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

    public class CreateGroupViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Group group;
        #endregion

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

            this.group = new TeamViewer.Group();

            this.ConfirmCommand = new Command(this.CreateGroup, (o) => 
            {
                return !string.IsNullOrWhiteSpace(this.group.Name);
            });

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

        #region Methods
        /// <summary>
        /// Create new contact
        /// </summary>
        private void CreateGroup(object o)
        {
            TeamViewer.Group group = TeamViewer.Group.CreateGroup(TokenViewModel.GetToken(), this.group);

            this.OnGroupCreationFinished.Invoke(new GroupCreationEventArgs(this, true, group));

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

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

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

        public delegate void GroupCreationEventHandler(GroupCreationEventArgs e);

        public event GroupCreationEventHandler OnGroupCreationFinished;
        #endregion

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

        public class GroupCreationEventArgs : EventArgs
        {
            public bool Result { get; private set; }
            public TeamViewer.Group Group { get; private set; }
            public CreateGroupViewModel Sender { get; private set; }

            public GroupCreationEventArgs(CreateGroupViewModel sender, bool result, TeamViewer.Group group = null)
            {
                this.Sender = sender;
                this.Result = result;
                this.Group = group;
            }
        }
    }
}
