using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

using Foo.Platform;
using Foo.Platform.Interacters;

namespace Foo.Hooks.CaptionButton
{
    public partial class CaptionButton : Form
    {
        // Declare the Log4net Variable
        private static log4net.ILog Log = Logger.GetLog4NetInterface(MethodBase.GetCurrentMethod().DeclaringType);

        [DllImport("user32.dll")]
        public static extern Int32 GetWindowLong(IntPtr hwnd, int nIndex);

        [DllImport("user32.dll")]
        public static extern Int32 SetWindowLong(IntPtr hwnd, int nIndex, Int32 dwNewLong);

        [DllImport("user32.dll")]
        public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, Int32 crKey, int bAlpha, uint dwFlags);

        public const int GWL_EXSTYLE = -20;
        public const int LWA_COLORKEY = 0x00000001;
        public const int WS_EX_LAYERED = 0x00080000;

        [StructLayout(LayoutKind.Sequential)]
        struct COLORREF
        {
            public byte R;
            public byte G;
            public byte B;
        }

        public CaptionButton()
        {
            InitializeComponent();            
        }

        ~CaptionButton()
        {
        }

        protected override void WndProc(ref Message m)
        {
            // Debug Only *Allows us to step right into buttonhook starting *
            if (Program.DEBUG_ONLY_SETTING_STARTBUTTONHOOK &&
                !Program.DEBUG_ONLY_SETTING_STARTBUTTONHOOK_IS_STARTED)
            {
                Log.Info(string.Format("{0}() - * Debug Only Option * Starting ButtonHook", MethodBase.GetCurrentMethod().Name));
                BHInteracter.StartButtonHookDLLEntryW();
                Program.DEBUG_ONLY_SETTING_STARTBUTTONHOOK_IS_STARTED = true;
            }

            if (m.Msg == BHInteracter.WM_CAPTIONBUTTON_START)
            {
                Log.Info(string.Format("{0}() - Received CAPTIONBUTTON_START Message", MethodBase.GetCurrentMethod().Name));
                BHInteracter.StartButtonHookDLLEntryW();
            }
            else if (m.Msg == BHInteracter.WM_CAPTIONBUTTON_STOP)
            {
                Log.Info(string.Format("{0}() - Received CAPTIONBUTTON_STOP Message", MethodBase.GetCurrentMethod().Name));
                BHInteracter.StopButtonHookDLLEntryW();
            }

            // force opacity at all times
            if (Opacity == 1.0)
                Opacity = 0.0;

            // force minimized at all times
            if (WindowState != FormWindowState.Minimized)
                WindowState = FormWindowState.Minimized;

            // force Not to be seen in taskbar
            if (ShowInTaskbar == true)
                ShowInTaskbar = false;
            
            base.WndProc(ref m);
        }

        void CaptionButton_Shown(object sender, System.EventArgs e)
        {
            Hide();
        }

        private void CaptionButton_HandleCreated(object sender, EventArgs e)
        {
            IntPtr hWnd = this.Handle;
            SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
            SetLayeredWindowAttributes(hWnd, 0x00000000, 0, LWA_COLORKEY);
        }

        void CaptionButton_Load(object sender, System.EventArgs e)
        {
            Log.Info(string.Format("{0}() - CaptionButton Load Called! - passing out hWnd Handle {1}", MethodBase.GetCurrentMethod().Name,Handle.ToInt32()));
            TemporaryVal.SetValI(Handle.ToInt32());
        }
    }
}
