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

    public class CreateDeviceViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Device device;
        private TeamViewer.Groups groups;
        #endregion

        #region Constructor
        public CreateDeviceViewModel()
        {
            this.Header = "Create device";

            this.device = new TeamViewer.Device();
            this.groups = TeamViewer.Groups.GetGroups(TokenViewModel.GetToken());

            if (this.groups != null)
                this.device.GroupId = this.groups.Items[0].GroupId;

            this.ConfirmCommand = new Command(this.CreateDevice, (o) => 
            {
                return !string.IsNullOrWhiteSpace(this.device.RemoteControlId) && !string.IsNullOrWhiteSpace(this.device.GroupId);
            });

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

        #region Methods
        /// <summary>
        /// Create new device
        /// </summary>
        private void CreateDevice(object o)
        {
            TeamViewer.Device device = TeamViewer.Device.CreateDevice(TokenViewModel.GetToken(), this.device);

            this.OnDeviceCreationFinished.Invoke(new DeviceCreationEventArgs(this, true, device));

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

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

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

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

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

        public delegate void DeviceCreationEventHandler(DeviceCreationEventArgs e);

        public event DeviceCreationEventHandler OnDeviceCreationFinished;
        #endregion

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

        public class DeviceCreationEventArgs : EventArgs
        {
            public bool Result { get; private set; }
            public TeamViewer.Device Device { get; private set; }
            public CreateDeviceViewModel Sender { get; private set; }

            public DeviceCreationEventArgs(CreateDeviceViewModel sender, bool result, TeamViewer.Device device = null)
            {
                this.Sender = sender;
                this.Result = result;
                this.Device = device;
            }
        }
    }
}
