// The CMLeonOS Project (https://github.com/Leonmmcoset/CMLeonOS)
// Copyright (C) 2025-present LeonOS 2 Developer Team
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

using Cosmos.System.Graphics;
using CMLeonOS.Gui.Apps;
using CMLeonOS.Logger;
using CMLeonOS.Utils;
using System.Collections.Generic;
using System.Drawing;

namespace CMLeonOS.Gui
{
    internal static class AppManager
    {
        internal static List<AppMetadata> AppMetadatas { get; private set; } = new List<AppMetadata>();

        private static bool appsLoaded = false;

        internal static Bitmap DefaultAppIcon
        {
            get
            {
                return Icons.Icon_Default;
            }
        }

        private static class Icons
        {
            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Default.bmp")]
            private static byte[] _iconBytes_Default;
            internal static Bitmap Icon_Default = new Bitmap(_iconBytes_Default);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Info.bmp")]
            private static byte[] _iconBytes_Info;
            internal static Bitmap Icon_Info = new Bitmap(_iconBytes_Info);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Settings.bmp")]
            private static byte[] _iconBytes_Settings;
            internal static Bitmap Icon_Settings = new Bitmap(_iconBytes_Settings);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Clock.bmp")]
            private static byte[] _iconBytes_Clock;
            internal static Bitmap Icon_Clock = new Bitmap(_iconBytes_Clock);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Tasks.bmp")]
            private static byte[] _iconBytes_Tasks;
            internal static Bitmap Icon_Tasks = new Bitmap(_iconBytes_Tasks);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Calculator.bmp")]
            private static byte[] _iconBytes_Calculator;
            internal static Bitmap Icon_Calculator = new Bitmap(_iconBytes_Calculator);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.CodeStudio.bmp")]
            private static byte[] _iconBytes_CodeStudio;
            internal static Bitmap Icon_CodeStudio = new Bitmap(_iconBytes_CodeStudio);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Notepad.bmp")]
            private static byte[] _iconBytes_Notepad;
            internal static Bitmap Icon_Notepad = new Bitmap(_iconBytes_Notepad);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Calendar.bmp")]
            private static byte[] _iconBytes_Calendar;
            internal static Bitmap Icon_Calendar = new Bitmap(_iconBytes_Calendar);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Files.bmp")]
            private static byte[] _iconBytes_Files;
            internal static Bitmap Icon_Files = new Bitmap(_iconBytes_Files);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Logs.bmp")]
            private static byte[] _iconBytes_Logs;
            internal static Bitmap Icon_Logs = new Bitmap(_iconBytes_Logs);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.DemoLauncher.bmp")]
            private static byte[] _iconBytes_DemoLauncher;
            internal static Bitmap Icon_DemoLauncher = new Bitmap(_iconBytes_DemoLauncher);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Stopwatch.bmp")]
            private static byte[] _iconBytes_Stopwatch;
            internal static Bitmap Icon_Stopwatch = new Bitmap(_iconBytes_Stopwatch);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.Paint.bmp")]
            private static byte[] _iconBytes_Paint;
            internal static Bitmap Icon_Paint = new Bitmap(_iconBytes_Paint);

            [IL2CPU.API.Attribs.ManifestResourceStream(ResourceName = "CMLeonOS.Gui.Resources.AppIcons.MemoryStatistics.bmp")]
            private static byte[] _iconBytes_MemoryStatistics;
            internal static Bitmap Icon_MemoryStatistics = new Bitmap(_iconBytes_MemoryStatistics);
        }

        internal static void RegisterApp(AppMetadata app)
        {
            AppMetadatas.Add(app);
        }

        internal static AppMetadata GetAppMetadata(string name)
        {
            foreach (AppMetadata app in AppMetadatas)
            {
                if (app.Name == name)
                {
                    return app;
                }
            }
            return null;
        }

        internal static void LoadAllApps()
        {
            if (appsLoaded)
            {
                return;
            }

            RegisterApp(new AppMetadata("Files", () => { return new Files(); }, Icons.Icon_Files, Color.FromArgb(25, 84, 97)));
            RegisterApp(new AppMetadata("Clock", () => { return new Clock(); }, Icons.Icon_Clock, Color.FromArgb(168, 55, 47)));
            RegisterApp(new AppMetadata("Notepad", () => { return new Notepad(); }, Icons.Icon_Notepad, Color.FromArgb(14, 59, 76)));
            RegisterApp(new AppMetadata("Settings", () => { return new Apps.Settings(); }, Icons.Icon_Settings, Color.FromArgb(0, 115, 186)));
            RegisterApp(new AppMetadata("Tasks", () => { return new Tasks(); }, Icons.Icon_Tasks, Color.FromArgb(204, 241, 255)));
            RegisterApp(new AppMetadata("Calculator", () => { return new Calculator(); }, Icons.Icon_Calculator, Color.FromArgb(0, 115, 186)));
            RegisterApp(new AppMetadata("Event Log", () => { return new Logs(); }, Icons.Icon_Logs, Color.FromArgb(14, 59, 76)));
            RegisterApp(new AppMetadata("Demos", () => { return new DemoLauncher(); }, Icons.Icon_DemoLauncher, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("Info", () => { return new Info(); }, Icons.Icon_Info, Color.FromArgb(0, 115, 186)));
            RegisterApp(new AppMetadata("Stopwatch", () => { return new Stopwatch(); }, Icons.Icon_Stopwatch, Color.FromArgb(168, 55, 47)));
            RegisterApp(new AppMetadata("Paint", () => { return new Apps.Paint.Paint(); }, Icons.Icon_Paint, Color.FromArgb(0, 115, 186)));
            RegisterApp(new AppMetadata("Memory Statistics", () => { return new Apps.MemoryStatistics(); }, Icons.Icon_MemoryStatistics, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("CodeStudio", () => { return new Apps.CodeStudio.CodeStudio(); }, Icons.Icon_CodeStudio, Color.FromArgb(14, 59, 76)));
            RegisterApp(new AppMetadata("Image Viewer", () => { return new ImageViewer(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("MarkIt Viewer", () => { return new MarkItViewer(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("Environment Variables", () => { return new EnvironmentVariables(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("UILib Gallery", () => { return new UILibGallery(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("Music Editor", () => { return new MusicEditor(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("Function Grapher", () => { return new FunctionGrapher(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("CodeBlocks", () => { return new CodeBlocks(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("Contacts", () => { return new Contacts(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("Sheet Editor", () => { return new SheetEditor(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));
            RegisterApp(new AppMetadata("Tetris", () => { return new Tetris(); }, Icons.Icon_Default, Color.FromArgb(25, 25, 25)));

            Logger.Logger.Instance.Info("AppManager", $"{AppMetadatas.Count} apps were registered.");

            appsLoaded = true;
        }
    }
}
