using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Media;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace Nastavení_PAO
{
    public partial class MsgBoxMod : Form
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

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


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

        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);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        static uint WM_CLOSE = 0x10;
        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;

        IntPtr msgwindowhandler;
        Timer timer,shower,starter;
        string fid;


        private string zprava, title;
        private Icon mbij;
        private int ih, iw, lh, lw;


        public string Zprava {

            set {

                this.zprava = value;
            }

            get {

                return zprava;
            
            }
        
        }

        public string Title
        {

            set
            {

                this.title = value;

            }

            get
            {

                return title;

            }

        }

        public Icon Mbi
        {

            set
            {

                this.mbij = value;

            }

            get
            {

                return mbij;

            }

        }

        public delegate void FCompl(object sender, EventArgs e);
        public event FCompl FComplStatus;

        public void Bridge____SetForegroundWindow(string caption) { 
        
        SetForegroundWindow(FindWindowByCaption(IntPtr.Zero, caption));
        
        }

        protected void FComplStatusMake() {

            EventArgs e = new EventArgs();

            if (FComplStatus != null) {

                FComplStatus(this,e);
            
            }
        
        }

        public MsgBoxMod()
        {

           

            timer = new Timer();
            timer.Interval = 2000;
            timer.Stop();
            timer.Enabled = false;
            timer.Tick +=new EventHandler(timer_Tick);

            starter = new Timer();
            starter.Interval = 1;
            starter.Stop();
            starter.Enabled = false;
            starter.Tick += new EventHandler(starter_Tick);

            shower = new Timer();
            shower.Interval = 100;
            shower.Stop();
            shower.Enabled = false;
            shower.Tick +=new EventHandler(shower_Tick);

            InitializeComponent();

            Guid g = Guid.NewGuid();
            fid = Convert.ToBase64String(g.ToByteArray());
            fid = fid.Replace("=", "");
            fid = fid.Replace("+", "");
            this.Text = fid;

            label1.Text = fid;
            label1.Location = new Point(24, (this.ClientSize.Height /2));
            this.ClientSize = new Size(label1.Size.Width + (label1.Size.Width / 2) + 3, this.ClientSize.Height);

            this.Location = new Point((Screen.FromControl(this).Bounds.Width / 2) - (this.ClientSize.Width / 2), (Screen.FromControl(this).Bounds.Height / 2) - (this.ClientSize.Height / 2));

            starter.Enabled = true;
            starter.Start();
        }

        private void starter_Tick(object sender, EventArgs w) {

            this.Show();

            starter.Stop();
            starter.Enabled = false;
        
        }

        private void timer_Tick(object sender, EventArgs e) {

            //SendMessage(msgwindowhandler, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            ShowWindow(msgwindowhandler, SW_HIDE);
            timer.Stop();
            timer.Enabled = false;
        
        }

        private void shower_Tick(object sender, EventArgs e) {

            ShowWindow(msgwindowhandler, SW_SHOW);

            shower.Stop();
            shower.Enabled = false;

            if (Mbi == SystemIcons.Asterisk)
            {

                SystemSounds.Beep.Play();

            }

            if (Mbi == SystemIcons.Exclamation)
            {

                SystemSounds.Exclamation.Play();

            }

            if (Mbi == SystemIcons.Error)
            {


                SystemSounds.Hand.Play();

            }

            if (Mbi == SystemIcons.Question)
            {

                SystemSounds.Asterisk.Play();


            }

            timer.Enabled = true;
            timer.Start();
        
        }

        public void MakeMessage(String zprava, String title, Icon mbi)
        {

            label1.Font = new Font("Microsoft Sans Serif", (float)8.25);
            label1.ForeColor = Color.Black;

            Zprava = zprava;
            Title = title;
            Mbi = mbi;

            this.Text = Title;
            label1.Text = Zprava;

            iw = 24;
            ih = (ClientRectangle.Height / 2) - (Mbi.Height / 2);
            lh = ih + (label1.Size.Height / 2) + 4;
            lw = iw + Mbi.Size.Width + iw / 3;
            this.ClientSize = new Size(label1.Size.Width + (label1.Size.Width / 2) + 3, this.ClientSize.Height);

            label1.Location = new Point(lw, lh);

            this.Location = new Point((Screen.FromControl(this).Bounds.Width / 2) - (this.ClientSize.Width / 2), (Screen.FromControl(this).Bounds.Height/ 2) - (this.ClientSize.Height / 2));
            
            shower.Enabled = true;
            shower.Start();

        
        }

        private void MsgBoxMod_Shown(object sender, EventArgs e)
        {

            msgwindowhandler = FindWindowByCaption(IntPtr.Zero, fid.ToString());
            SetForegroundWindow(msgwindowhandler);
            IntPtr hSystemMenu = GetSystemMenu(msgwindowhandler, false);
            EnableMenuItem(hSystemMenu, SC_CLOSE, (uint)(MF_ENABLED | MF_GRAYED));

            ShowWindow(msgwindowhandler, SW_HIDE);
            FComplStatusMake();
        }

        private void MsgBoxMod_Paint(object sender, PaintEventArgs g)
        {
            if (Mbi != null)
            {
                g.Graphics.DrawIcon(Mbi, new Rectangle(iw, ih, Mbi.Width, Mbi.Height));
            }
        }
    }
}
