#include <config.h>
#include <stdio.h>
#include <vector>
#include "IIIMP_hotkey_profile.hh"
#include "IIIMP_IMState.hh"

IIIMP_hotkey_profile::IIIMP_hotkey_profile(
)
{
    IMKeySpecList hks;
    u16string label;
    HotkeyList phklist;
    int hkid, sflag, aflag;

    // PROFILE_ID = IIIMP_PROFILE_ID_FIRST

    hkid = HOTKEY_ID_LANG_SWITCH;
    sflag = 0;
    aflag = 1;
    hks.push_back(IMKeySpec(IM_VK_M, 0, IM_CTRL_MASK|IM_SHIFT_MASK, 0));
    hks.push_back(IMKeySpec(IM_VK_SPACE, 0, IM_CTRL_MASK|IM_SHIFT_MASK, 0));
    label = "LE SWITCH";

    phklist.push_back(Hotkey(hkid, sflag, aflag, hks, label));
    hks.clear();

    hkid = HOTKEY_ID_TRIGGER_NOTIFY;
    sflag = 0;
    aflag = 1;
    hks.push_back(IMKeySpec(IM_VK_SPACE, 0, IM_CTRL_MASK, 0));
    hks.push_back(IMKeySpec(IM_VK_SPACE, 0, IM_SHIFT_MASK, 0));
    label = "TRIGGER KEYS";

    phklist.push_back(Hotkey(hkid, sflag, aflag, hks, label));
    hks.clear();

    hkid = HOTKEY_ID_SUPER_HELP;
    sflag = 0;
    aflag = 1;
    hks.push_back(IMKeySpec(IM_VK_BACK_SLASH, 0, IM_CTRL_MASK, 0));
    hks.push_back(IMKeySpec(IM_VK_CLOSE_BRACKET, 0, IM_CTRL_MASK, 0));
    hks.push_back(IMKeySpec(IM_VK_COMMA, 0, IM_ALT_MASK, 0));
    label = "SUPER HELP";

    phklist.push_back(Hotkey(hkid, sflag, aflag, hks, label));
    hks.clear();

    profile_list.clear();
    profile_list.push_back(HotkeyProfile(IIIMP_PROFILE_ID_FIRST, 0, phklist));

    // PROFILE_ID = IIIMP_PROFILE_ID_SECOND

    hkid = HOTKEY_ID_LANG_SWITCH;
    sflag = 0;
    aflag = 1;
    hks.push_back(IMKeySpec(IM_VK_EQUALS, 0, IM_CTRL_MASK, 0));
    label = "LE SWITCH";

    phklist.clear();
    phklist.push_back(Hotkey(hkid, sflag, aflag, hks, label));
    hks.clear();

    hkid = HOTKEY_ID_TRIGGER_NOTIFY;
    sflag = 0;
    aflag = 1;
    hks.push_back(IMKeySpec(IM_VK_MINUS, 0, IM_CTRL_MASK, 0));
    hks.push_back(IMKeySpec(IM_VK_COMMA, 0, IM_CTRL_MASK, 0));
    label = "TRIGGER KEYS";

    phklist.push_back(Hotkey(hkid, sflag, aflag, hks, label));
    hks.clear();

    profile_list.push_back(HotkeyProfile(IIIMP_PROFILE_ID_SECOND, 0, phklist));
}

IIIMP_hotkey_profile::~IIIMP_hotkey_profile()
{
}

const HotkeyProfileList*
IIIMP_hotkey_profile::get_profile_list()
{
    return &profile_list;
}

int
IIIMP_hotkey_profile::register_hotkeys(
    void* register_id,
    HotkeyList *hklist  
)
{
    // TODO ...
    // return profile_id
}

HOTKEY_LIST* 
IIIMP_hotkey_profile::retrive_hotkeys(
    IIIMP_data_s *pdata_s,
    int profile_id
)
{
    HotkeyProfileList::iterator it1;
    HotkeyProfileList *hkplist = const_cast<HotkeyProfileList *>(get_profile_list());
    HotkeyList::iterator it2; 
    IMKeySpecList::iterator it3;
    HOTKEY *hk;
    HOTKEY_LIST *hklist;
    IIIMP_keyevent *keys;
    int i,j;
    u16string label;

    for (it1 = hkplist->begin(); it1 != hkplist->end(); it1++) {
	if (it1->get_profile_id() == profile_id) {
	    HotkeyList *phklist = const_cast<HotkeyList *>(it1->get_hotkey_list());
	    hk = new HOTKEY[phklist->size()];
	    for (i = 0, it2 = phklist->begin(); it2 != phklist->end(), i < phklist->size(); it2++, i++) {
		hk[i].hotkeyctrl.hotkey_id = it2->get_hotkey_id();
		hk[i].hotkeyctrl.state_flag = it2->get_state_flag();
		hk[i].hotkeyctrl.action_flag = it2->get_action_flag();

		IMKeySpecList *keylist = const_cast<IMKeySpecList *>(it2->get_keylist());
		keys = new IIIMP_keyevent[keylist->size()];

		for (j = 0, it3 = keylist->begin(); it3 != keylist->end(), j < keylist->size(); it3++, j++) {
		    keys[j].keycode = it3->get_keycode();
		    keys[j].keychar = it3->get_keychar();
		    keys[j].modifier = it3->get_modifier();
		    keys[j].time_stamp = it3->get_timestamp();
		}
		hk[i].hotkeylist = iiimp_keyevent_list_new(pdata_s, keylist->size(), keys);
		delete[] keys;

		if (!hk[i].hotkeylist) {
		    if (hk[i].hotkeylist) iiimp_keyevent_list_delete(pdata_s, hk[i].hotkeylist);
		    return false;
		}

		label = it2->get_label();
		hk[i].label = iiimp_string_new(pdata_s, label.size(), label.data());
	    }
	    hklist = iiimp_hotkey_list_new(pdata_s, phklist->size(), hk);
	    delete[] hk;
	    return hklist;
	}
    }
}

int 
IIIMP_hotkey_profile::get_num_profiles()
{
    HotkeyProfileList *hkplist = const_cast<HotkeyProfileList *>(get_profile_list());
    return hkplist->size();
}

void* 
IIIMP_hotkey_profile::get_registerer_id_by_profile_id(
    int profile_id
)
{
}

void* 
IIIMP_hotkey_profile::get_profile_id_by_registerer_id(
    void* registerer_id
)
{
}

/* Local Variables: */
/* c-file-style: "iiim-project" */
/* End: */
