using System;
using System.Diagnostics;
using System.Net;
using System.Windows.Media.Imaging;
using Caliburn.Micro;

namespace TimedShutdown.ViewModels
{
    public class ShellRootViewModel : Screen
    {
        private readonly ShutdownManager _shutdownManager = new ShutdownManager();
        private readonly PingWidget pingWidget = new PingWidget();

        #region Values

        private int _hours;
        public int Hours
        {
            get { return _hours; }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }
                else if (value > 23)
                {
                    value = 23;
                }

                _hours = value;
                NotifyOfPropertyChange(() => Hours);
                NotifyOfPropertyChange(() => ShutdownTime);
                NotifyOfPropertyChange(() => ShutdownTimeSpan);
            }
        }

        private int _minutes;
        public int Minutes
        {
            get { return _minutes; }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }
                else if (value > 59)
                {
                    value = 59;
                }

                _minutes = value;
                NotifyOfPropertyChange(() => Minutes);
                NotifyOfPropertyChange(() => ShutdownTime);
                NotifyOfPropertyChange(() => ShutdownTimeSpan);
            }
        }

        public int ShutdownTime => Minutes * 60 + Hours * 60 * 60;

        public TimeSpan ShutdownTimeSpan => TimeSpan.FromSeconds(ShutdownTime);

        public bool ShutdownOptionsForce { get; set; }

        private string _remoteShutdown;
        public string RemoteShutdown
        {
            get { return _remoteShutdown; }
            set
            {
                // Show remote shutdown alert upon checking for a non-loopback
                // address.
                if (!string.IsNullOrEmpty(value)
                    && value != "localhost"
                    && value != "127.0.0.1")
                {
                    Messaging alert = new Messaging();
                    alert.Show(Messaging.Message.RemoteShutdown, value);
                }
                _remoteShutdown = value;
            }
        }

        private bool _pingRemoteHost;
        public bool PingRemoteHost
        {
            get { return _pingRemoteHost; }
            set
            {
                _pingRemoteHost = value;

                if (value == true)
                {

                    pingWidget.InitatePingRequest(IPAddress.Parse(RemoteShutdown));
                }
                else
                {
                    if (pingWidget.WidgetHasInitialized == true)
                    {
                        pingWidget.StopCurrentPingRequest();
                    }
                }
            }
        }

        public string PingRemoteHostStatus { get; set; }

        #region Remote Expander

        public int WindowHeight { get; set; } = 335; // Default 335

        private bool _remoteExpander;
        public bool RemoteExpander
        {
            get { return _remoteExpander; }
            set
            {
                WindowHeight = value ? 460 : 335;
                NotifyOfPropertyChange(() => WindowHeight);

                _remoteExpander = value;
            }
        }

        #endregion

        #endregion

        #region Buttons

        public bool CanShutdown { get; set; } = true;
        public void Shutdown()
        {
            _shutdownManager.ExecuteOperation(
                ShutdownManager.Operation.Shutdown,
                ShutdownTime,
                ShutdownOptionsForce,
                RemoteShutdown);

                CanReboot = false;
                CanShutdown = false;

                NotifyOfPropertyChange(() => CanReboot);
                NotifyOfPropertyChange(() => CanShutdown);
        }

        public bool CanReboot { get; set; } = true;
        public void Reboot()
        {
            _shutdownManager.ExecuteOperation(
                ShutdownManager.Operation.Reboot,
                ShutdownTime,
                ShutdownOptionsForce,
                RemoteShutdown);

                CanReboot = false;
                CanShutdown = false;

                NotifyOfPropertyChange(() => CanReboot);
                NotifyOfPropertyChange(() => CanShutdown);
        }

        public void AbortShutdown()
        {
            // Reset values to defaults.
            Minutes = 0;
            Hours = 0;

            _shutdownManager.AbortOperation(RemoteShutdown);
            CanReboot = true;
            CanShutdown = true;

            NotifyOfPropertyChange(() => CanReboot);
            NotifyOfPropertyChange(() => CanShutdown);
            NotifyOfPropertyChange(() => RemoteShutdown);
        }

        #endregion
    }
}
