using Microsoft.Win32;
using System;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using System.Drawing;

namespace GFATask
{
    public class SystemChanges
    {
        public SystemChanges() { }
        public void Load()
        {
            AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
            SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
            SystemEvents.EventsThreadShutdown += SystemEvents_EventsThreadShutdown;
            SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
            SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanging;
            SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
            SystemEvents.SessionEnding += SystemEvents_SessionEnding;
            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
        }

        private void CurrentDomain_ProcessExit(object sender, EventArgs e)
        {
            XObjects.Delay.Break(Delay.Prevent.Decrease);
            SystemApps.Kill(out _);
        }

        private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            //XObjects.MessageBox = Feed.Show("Netzwerkverbindung " + (e.IsAvailable ? "hergestellt" : "getrennt"), NotifyButtons.OK, NotifyType.Message, Designer.Feed(150, 150), null, "Netzwerk");
            if (e.IsAvailable)
            {
                AD.GetGroups();
                Forms.Main.CreateAppListPanels();
            }
        }

        private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
        {
            //if (e.Category == UserPreferenceCategory.General)
            //{
            //    MessageBox.Show(e.Category.ToString());
            //    Forms.Main.BackColor = WindowsColors.Accent.StartMenuBackColor;
            //    Forms.Main.BorderColor = WindowsColors.Accent.BackColor; 
            //    Forms.Main.TitlebarForeColor = Coloring.GetReadableForeColor(Forms.Main.BorderColor);
            //}
        }

        private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
        {
            Forms.Main.SetMaximumSize();
            //Forms.Main.DesktopAlign();
        }

        private void SystemEvents_DisplaySettingsChanging(object sender, EventArgs e)
        {
            Forms.Settings.LoadDisplays();
            Win32API.SendMessage(Forms.Main.Handle, (int)WM.ENTERSIZEMOVE, 0, 0);
            Forms.Main.DesktopAlign();
        }

        private void SystemEvents_EventsThreadShutdown(object sender, EventArgs e)
        {
            SystemApps.Kill(out _);
        }

        private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
        {
            SystemApps.Kill(out _);
        }

        private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
        {
            if(e.Mode == PowerModes.Resume)
            {
                SystemApps.Restart(out _);
            }
        }
    }
}
