using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using ConsoleExtender;


namespace Nastavení_PAO
{

    public enum ConsoleTypeOfMessage
    {

        Info = 1,
        Success = 2,
        Error = 3,
        Profiler = 4,
        Screenshot = 5
    }


   public class InfoConsole
    {

       public bool ConsoleShowed = false;
      private  ConsoleColor[] colors = new ConsoleColor[6];
	  IntPtr consolewindowhandler;
      

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool AllocConsole();

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool FreeConsole();

        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        const int SW_HIDE = 0;
        const int SW_SHOW = 5;

        [DllImport("user32.dll")]
        static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);


        internal const UInt32 SC_CLOSE = 0xF060;
        internal const UInt32 MF_ENABLED = 0x00000000;
        internal const UInt32 MF_GRAYED = 0x00000001;
        internal const UInt32 MF_DISABLED = 0x00000002;
        internal const uint MF_BYCOMMAND = 0x00000000;




        public static void EnableCloseButton(IWin32Window window, bool bEnabled)
        {
            IntPtr hSystemMenu = GetSystemMenu(window.Handle, false);
            EnableMenuItem(hSystemMenu, SC_CLOSE, (uint)(MF_ENABLED | (bEnabled ? MF_ENABLED : MF_GRAYED)));
        }

        public InfoConsole(ConsoleColor c1_success, ConsoleColor c2_error, ConsoleColor c3_info, ConsoleColor c4_datetime, ConsoleColor c5_profiler, ConsoleColor c6_screen)
        {

            colors[0] = c1_success;
            colors[1] = c2_error;
            colors[2] = c3_info;
            colors[3] = c4_datetime;
            colors[4] = c5_profiler;
            colors[5] = c6_screen;
            

            AllocConsole();
            Console.Title = "Datová konzole";
            consolewindowhandler = GetConsoleWindow();
            ShowWindow(consolewindowhandler, SW_HIDE);

            NativeWindow nativeWindow = new NativeWindow();
            nativeWindow.AssignHandle(consolewindowhandler);

            EnableCloseButton(nativeWindow, false);


            //ConsoleHelper.SetConsoleFont(5);
            ConsoleHelper.SetConsoleIcon(SystemIcons.Information);

}

        public void CloseConsoleWindow()
        {


            ShowWindow(consolewindowhandler, SW_HIDE);

        }

        public void FreeConsoleX() {

            FreeConsole();
        
        }

        public void OpenConsoleWindow()
        {

            ShowWindow(consolewindowhandler, SW_SHOW);
        }


        public void AdvancedCharactersInConsole(ConsoleTypeOfMessage Ctom, DateTime time, string text)
        {
        
        ConsoleColor color = ConsoleColor.Black;

        switch(Ctom){
        
            case ConsoleTypeOfMessage.Error:{
            
                color = colors[1];
            
            break;
            }
        
            case ConsoleTypeOfMessage.Info:{

                color = colors[2];
            
            break;
            }
        
            case ConsoleTypeOfMessage.Success:{

                color = colors[0];
            
            break;
            }

            case ConsoleTypeOfMessage.Profiler:
                {

                    color = colors[4];

                    break;
                }

            case ConsoleTypeOfMessage.Screenshot:
                {

                    color = colors[5];

                    break;
                }
        }

            Console.ForegroundColor = colors[3];
            Console.Write(DateTime.Now.ToString("HH:mm:ss"));
            Console.Write(" ");
            Console.ForegroundColor = color;
            Console.Write(text + "\n");

            if (ConsoleShowed == true) { 
            SetForegroundWindow(consolewindowhandler);
            }
        }


    }


}


namespace ConsoleExtender
{
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct ConsoleFont
    {
        public uint Index;
        public short SizeX, SizeY;
    }

    public static class ConsoleHelper
    {
        [DllImport("kernel32")]
        public static extern bool SetConsoleIcon(IntPtr hIcon);

        public static bool SetConsoleIcon(Icon icon)
        {
            return SetConsoleIcon(icon.Handle);
        }

        [DllImport("kernel32")]
        private extern static bool SetConsoleFont(IntPtr hOutput, uint index);

        private enum StdHandle
        {
            OutputHandle = -11
        }

        [DllImport("kernel32")]
        private static extern IntPtr GetStdHandle(StdHandle index);

        public static bool SetConsoleFont(uint index)
        {
            return SetConsoleFont(GetStdHandle(StdHandle.OutputHandle), index);
        }

        [DllImport("kernel32")]
        private static extern bool GetConsoleFontInfo(IntPtr hOutput, [MarshalAs(UnmanagedType.Bool)]bool bMaximize,
            uint count, [MarshalAs(UnmanagedType.LPArray), Out] ConsoleFont[] fonts);

        [DllImport("kernel32")]
        private static extern uint GetNumberOfConsoleFonts();

        public static uint ConsoleFontsCount
        {
            get
            {
                return GetNumberOfConsoleFonts();
            }
        }

        public static ConsoleFont[] ConsoleFonts
        {
            get
            {
                ConsoleFont[] fonts = new ConsoleFont[GetNumberOfConsoleFonts()];
                if (fonts.Length > 0)
                    GetConsoleFontInfo(GetStdHandle(StdHandle.OutputHandle), false, (uint)fonts.Length, fonts);
                return fonts;
            }
        }

    }
}
