#include "CKeyBoardHook.h"
#include "CAppInfo.h"
#include "logproc.h"

CKeyBoardHook *CKeyBoardHook::m_pThis;
std::shared_ptr<CKeyBoardHook> g_keyBoardHookPtr = nullptr;

CKeyBoardHook::CKeyBoardHook()
{
    h_module = nullptr;
    g_hProcess = nullptr;
    g_HotKey = nullptr;
    m_pThis = this;
}

CKeyBoardHook::~CKeyBoardHook()
{
    if(g_HotKey != nullptr)
    {
        Hotkey_UnInstall();
    }
}

bool CKeyBoardHook::Hotkey_Install(DWORD ThreadID)
{
    g_HotKey = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)Hotkey_Filter, //安装低级键盘钩子
        GetModuleHandleA("client.exe"), ThreadID);
    if (nullptr == g_HotKey)
    {
        myDebug()<<QString::fromLocal8Bit("安装钩子出错 !");
//		MessageBoxA(NULL, "安装钩子出错 !", "error", MB_ICONSTOP);
        return false;
    }
    return true;
}

bool CKeyBoardHook::Hotkey_UnInstall()
{
    UnhookWindowsHookEx(g_HotKey); //卸载钩子
    g_HotKey = nullptr;

    return true;
}

LRESULT CALLBACK CKeyBoardHook::Hotkey_Filter(int nCode, WPARAM wParam, LPARAM lParam)
{
    KBDLLHOOKSTRUCT *Key_Info = (KBDLLHOOKSTRUCT *) lParam;

    if (HC_ACTION == nCode)
    {
        bool bCtrlKeyDown =
            GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);
        bool bwinKeyDown =
            GetAsyncKeyState(VK_LWIN)>>((sizeof(SHORT) * 8) - 1);

        if (WM_KEYDOWN == wParam || wParam == WM_SYSKEYDOWN)  //如果按键为按下状态
        {
            if(g_appInfoPtr->m_bFullScreenTop)
            {
                if (Key_Info->vkCode == VK_RWIN || Key_Info->vkCode == VK_LWIN) //屏敝 WIN
                {
                    return 1;
                }

                if (Key_Info->vkCode == 0x4D && ((GetKeyState(VK_LWIN) & 0x8000) ||
                    (GetKeyState(VK_RWIN) & 0x8000))) //屏敝 WIN+M 组合键(左右)
                {
                    return 1;
                }
                if (Key_Info->vkCode == 0x44 && ((GetKeyState(VK_RWIN) & 0x8000) ||
                    (GetKeyState(VK_LWIN) & 0x8000)))  //屏敝 WIN+D 组合键(左右)
                {
                    return 1;
                }
                if (Key_Info->vkCode == 0x4C && ((GetKeyState(VK_RWIN) & 0x8000) ||
                    (GetKeyState(VK_LWIN) & 0x8000)))  //屏敝 WIN+L 组合键(左右)
                {
                    return 1;
                }
                if (Key_Info->vkCode==VK_ESCAPE && bCtrlKeyDown)
                //屏敝 CTRL + ESC 组合键
                {
//                    HWND hwnd = FindWindow(L"Shell_traywnd", NULL);
//                    EnableWindow(hwnd, false);
                    return 1;
                }
                if (Key_Info->vkCode == VK_F4 && Key_Info->flags & LLKHF_ALTDOWN)
                //屏敝 ATL + F4 组合键
                {
                    return 1;
                }

                if (Key_Info->vkCode == VK_LMENU || Key_Info->vkCode == VK_RMENU)
                {      //屏敝 ATL
                    return 1;
                }

                if (Key_Info->vkCode == VK_TAB && (Key_Info->flags & LLKHF_ALTDOWN))
                //屏敝 ATL + TAB 组合键
                {
                    return 1;
                }
                if (Key_Info->vkCode == VK_ESCAPE && (Key_Info->flags & LLKHF_ALTDOWN))
                 //屏敝 ATL + ESC 组合键
                {
                    return 1;
                }

                if (Key_Info->vkCode == VK_ESCAPE)
                 //屏敝ESC 键
                {
                    return 1;
                }

                if(bCtrlKeyDown && (Key_Info->vkCode == 0x58 ||
                                    Key_Info->vkCode == 0x56 ||
                                    Key_Info->vkCode == 0x43))
                {
                    return 1;
                }
            }

            if ((Key_Info->vkCode == VK_F11) && bCtrlKeyDown
                /*(GetKeyState(VK_CONTROL) & 0x8000)*/)
            {
//				if(m_hwnd != NULL)
                {

                }
                return true;
            }
        }
    }
    return CallNextHookEx(m_pThis->g_HotKey, nCode, wParam, lParam); //回调
}
