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

    public class DevicesViewModel : BasicViewModel
    {
        #region Properties
        private TeamViewer.Devices devices;
        private TeamViewer.Device selectedDevice;
        private TeamViewer.Groups groups;
        #endregion

        #region Constructor
        public DevicesViewModel()
        {
            this.Header = "Devices";

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

            this.SaveDeviceCommand = new Command(this.SaveDevice, (o) =>
            {
                return this.selectedDevice != null && !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });

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

            this.DeleteDeviceCommand = new Command(this.DeleteDevice, (o) =>
            {
                return this.selectedDevice != null && !string.IsNullOrEmpty(TokenViewModel.GetToken());
            });
        }
        #endregion

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

            this.Devices = TeamViewer.Devices.GetDevices(TokenViewModel.GetToken());

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

        /// <summary>
        /// Save device changes
        /// </summary>
        private void SaveDevice(object o)
        {
            TeamViewer.Device.SaveDevice(TokenViewModel.GetToken(), this.selectedDevice);
        }

        /// <summary>
        /// Create new device
        /// </summary>
        private void CreateDevice(object o)
        {
            CreateDeviceViewModel vm = new CreateDeviceViewModel();
            vm.OnDeviceCreationFinished += this.OnDeviceCreationViewClosed;
            Views.CreateDeviceView view = new Views.CreateDeviceView(vm);
            MainWindowViewModel.Instance.AddView(view);
        }

        /// <summary>
        /// Delete current device
        /// </summary>
        private void DeleteDevice(object o)
        {
            TeamViewer.Device.DeleteDevice(TokenViewModel.GetToken(), selectedDevice);

            this.GetDevices(o);
        }
        #endregion

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

                this.RaisePropertyChanged(() => this.Devices);
            }
        }
        
        /// <summary>
        /// Current selected device
        /// </summary>
        public TeamViewer.Device SelectedDevice
        {
            get { return this.selectedDevice; }
            set
            {
                this.selectedDevice = value;

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

        /// <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 GetDevicesCommand { get; private set; }
        public Command SaveDeviceCommand { get; private set; }
        public Command CreateDeviceCommand { get; private set; }
        public Command DeleteDeviceCommand { get; private set; }
        #endregion

        #region Events
        private void OnDeviceCreationViewClosed(CreateDeviceViewModel.DeviceCreationEventArgs e)
        {
            if (e.Result)
            {
                this.Devices = TeamViewer.Devices.GetDevices(TokenViewModel.GetToken());

                if (this.devices != null && this.devices.Items.Length > 0)
                    this.SelectedDevice = this.devices.Items.FirstOrDefault(x => x.DeviceId == e.Device.DeviceId);
            }

            e.Sender.OnDeviceCreationFinished -= this.OnDeviceCreationViewClosed;
        }
        #endregion
    }
}
