#include "../../netwaker_member/NetWakerClientInc.h"
#include "../skillbook/UI_SkillBook.h"
#include "ItemClipboard.h"
#include "BagFrame.h"
#include "MacroFrame.h"
#include "ActionFrame.h"

int LuaFunc_PickupActionButton(lua_State* L);
int LuaFunc_UseActionButton(lua_State* L);
int LuaFunc_GetActionItemInfo(lua_State* L);
int LuaFunc_GetActionButtonHotkey(lua_State* L);

CActionFrame*				g_pActionFrame = NULL;

// ----------------------------------------------------------------------------
CActionFrame::CActionFrame(CInterface* object)
	: CInterfaceBase(object)
{
	g_pActionFrame = this;

	for ( int i = 0; i < ACTION_FRAME_MAX_ITEM; i++ )
		ClearItem(i);
}

// ----------------------------------------------------------------------------
CActionFrame::~CActionFrame()
{
	Save();

	g_pActionFrame = NULL;
}

// ----------------------------------------------------------------------------
void CActionFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register(L, "PickupActionButton",		LuaFunc_PickupActionButton);
		lua_register(L, "UseActionButton",			LuaFunc_UseActionButton);
		lua_register(L, "GetActionItemInfo",		LuaFunc_GetActionItemInfo);
		lua_register(L, "GetActionButtonHotkey",	LuaFunc_GetActionButtonHotkey);
	}
}

// ----------------------------------------------------------------------------
void CActionFrame::Cut(int index)
{
	ActionItem* item = GetItem(index);
	if ( item )
	{
		switch( item->type )
		{
		case eActionItemType_None:
			break;

		case eActionItemType_Bag:
		case eActionItemType_Equipment:
			{
				GameObjDbStruct* itemDB = CNetGlobal::GetObj( item->objectID );
				if ( itemDB )
				{
					g_pItemClipboard->SetFile( itemDB->ImageObj->ACTField );
					g_pItemClipboard->PickupAction( this, item->type, item->objectID, 0 );
				}
			}
			break;

		case eActionItemType_Skill:
			{
/*				GameObjDbStruct* itemDB = CNetGlobal::GetObj( g_pUi_SkillBook->GetSkillGUID(item->skillID, item->level) );
				if ( itemDB )
				{
					g_pItemClipboard->SetFile( itemDB->ImageObj->ACTField );
					g_pItemClipboard->PickupAction( this, item->type, item->skillID, item->level );
				}*/
			}
			break;

		case eActionItemType_Macro:
			{
				MacroItem* macroItem = g_pMacroFrame->GetMacroItemById( item->macroID );
				if ( macroItem )
				{
					g_pItemClipboard->SetFile( g_pMacroFrame->GetMacroIcon(macroItem->icon) );
					g_pItemClipboard->PickupAction( this, item->type, item->macroID, 0 );
				}
			}
			break;
		}
	}
	ClearItem(index);
}

// ----------------------------------------------------------------------------
void CActionFrame::Paste(int index)
{
	ActionItem* action = GetItem(index);
	if ( action )
	{
		RoleDataEx* role = CNetGlobal::RoleData();

		switch(g_pItemClipboard->GetType())
		{	
		case CItemClipboard::eItem_Type_None:			// 拿取物品
			Cut(index);
			return;

		case CItemClipboard::eItem_Type_Bag:			// 背包 -> 快捷
			{				
				ClipboardBagItem bagItem = g_pItemClipboard->GetBagItem();
				ItemFieldStruct* item = role->GetBodyItem( bagItem.pos );
				if ( item )
				{
					if ( action->type != eActionItemType_None )
						Cut(index);
					else
						g_pItemClipboard->Clear();

					action->type		= eActionItemType_Bag;
					action->objectID	= item->OrgObjID;
				}
			}
			break;

		case CItemClipboard::eItem_Type_Equipment:		// 裝備 -> 快捷
			{				
				ClipboardEquipmentItem equipmentItem = g_pItemClipboard->GetEquipmentItem();
				ItemFieldStruct* item = role->GetEqItem( equipmentItem.pos );
				if ( item )
				{
					if ( action->type != eActionItemType_None )
						Cut(index);
					else
						g_pItemClipboard->Clear();

					action->type		= eActionItemType_Equipment;
					action->objectID	= item->OrgObjID;
				}
			}
			break;

		case CItemClipboard::eItem_Type_Action:			// 快捷 -> 快捷
			{
				ClipboardActionItem actionItem = g_pItemClipboard->GetActionItem();
				if ( action->type != eActionItemType_None )
					Cut(index);
				else
					g_pItemClipboard->Clear();

				action->type		= actionItem.type;
				switch(action->type)
				{
				case eActionItemType_None:
					break;

				case eActionItemType_Bag:
				case eActionItemType_Equipment:
					action->objectID = actionItem.id;
					break;

				case eActionItemType_Skill:
					action->skillID = actionItem.id;
					action->level	= actionItem.level;
					break;

				case eActionItemType_Macro:
					action->macroID	= actionItem.id;
					break;
				}
			}
			break;

		case CItemClipboard::eItem_Type_Bank:			// 銀行 -> 快捷
			g_pItemClipboard->Clear();
			break;		

		case CItemClipboard::eItem_Type_Shop:			// 商店 -> 快捷
			break;

		case CItemClipboard::eItem_Type_Trade:			// 交易 -> 快捷
			break;

		case CItemClipboard::eItem_Type_Skill:			// 技能 -> 快捷
			{
				ClipboardSkillItem skillItem = g_pItemClipboard->GetSkillItem();
				if ( action->type != eActionItemType_None )
					Cut(index);
				else
					g_pItemClipboard->Clear();

				action->type	= eActionItemType_Skill;
				action->skillID	= skillItem.id;
				action->level	= skillItem.level;
			}
			break;

		case CItemClipboard::eItem_Type_Macro:			// 巨集 -> 快捷
			{
				ClipboardMacroItem macroItem = g_pItemClipboard->GetMacroItem();
				if ( action->type != eActionItemType_None )
					Cut(index);
				else
					g_pItemClipboard->Clear();

				action->type	= eActionItemType_Macro;
				action->macroID	= macroItem.id;
			}
			break;
		}
	}
}

// ----------------------------------------------------------------------------
void CActionFrame::Use(int index)
{
	ActionItem* item = GetItem(index);
	if ( item )
	{
		if ( !g_pItemClipboard->IsEmpty() )
			Paste(index);
		else
		{
			switch(item->type)
			{
			case eActionItemType_None:


			case eActionItemType_Bag:
				g_pBagFrame->Use( g_pBagFrame->GetIndex(item->objectID) );
				break;

			case eActionItemType_Equipment:			
				break;

			case eActionItemType_Skill:
//				g_pUi_SkillBook->Use( item->skillID, item->level );
				break;

			case eActionItemType_Macro:
				g_pMacroFrame->UseMacro( item->macroID );
				break;
			}
		}
	}
}

// ----------------------------------------------------------------------------
ActionItem* CActionFrame::GetItem(int index)
{
	if (index < 0 || index >= ACTION_FRAME_MAX_ITEM)
		return NULL;
	return &m_items[index];
}

// ----------------------------------------------------------------------------
void CActionFrame::ClearItem(int index)
{
	ActionItem* item = GetItem(index);
	if ( item )
	{
		memset(item, 0, sizeof(ActionItem));
	}
}

// ----------------------------------------------------------------------------
void CActionFrame::Save()
{	
	char globalName[MAX_PATH];
	char localName[MAX_PATH];
	FILE* fptr = NULL;

	GetGlobalPath("Action", globalName, MAX_PATH);
	GetLocalPath("Action", localName, MAX_PATH);

	// 儲存共用資料			

	// 記錄資料	

	// 關閉檔案	

	// 儲存本地資料
	if ( (fptr = fopen(localName, "wb")) != NULL )
	{
		// 記錄資料
		fwrite( m_items, sizeof(m_items), 1, fptr );

		// 關閉檔案
		fclose(fptr);
	}
}

// ----------------------------------------------------------------------------
void CActionFrame::Load()
{
	char globalName[MAX_PATH];
	char localName[MAX_PATH];
	FILE* fptr = NULL;

	GetGlobalPath("Action", globalName, MAX_PATH);
	GetLocalPath("ACtion", localName, MAX_PATH);

	// 讀取共用資料

	// 記錄資料	

	// 關閉檔案

	// 讀取本地資料
	if ( (fptr = fopen(localName, "rb")) != NULL )
	{
		// 記錄資料
		fread( m_items, sizeof(m_items), 1, fptr );

		// 關閉檔案
		fclose(fptr);
	}
}

// ----------------------------------------------------------------------------
int LuaFunc_PickupActionButton(lua_State* L)
{
	g_pActionFrame->Paste(luaL_checkint(L, 1) - 1);		// 拿取物品
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_UseActionButton(lua_State* L)
{
	g_pActionFrame->Use( luaL_checkint(L, 1) - 1 );
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetActionItemInfo(lua_State* L)
{
	ActionItem* item = g_pActionFrame->GetItem(luaL_checkint(L, 1) - 1);
	if ( item )
	{
		switch (item->type)
		{
		case eActionItemType_None:
			lua_pushstring(L, "Interface\\Buttons\\emptySlot");				// push item texture file
			return 1;
		
		case eActionItemType_Bag:
		case eActionItemType_Equipment:
			{
				GameObjDbStruct* itemDB = CNetGlobal::GetObj(item->objectID);
				if (itemDB)
				{
					lua_pushstring(L, itemDB->ImageObj->ACTField);
					lua_pushstring(L, "");
					lua_pushnumber(L, 1);
					lua_pushboolean(L, false);
					return 4;
				}
				else
				{
					lua_pushstring(L, "Interface\\Buttons\\emptySlot");		// push item texture file
					return 1;
				}
			}
			break;

		case eActionItemType_Skill:
			{
/*				GameObjDbStruct* itemDB = CNetGlobal::GetObj( g_pUi_SkillBook->GetSkillGUID(item->skillID, item->level) );
				if (itemDB)
				{
					lua_pushstring(L, itemDB->ImageObj->ACTField);
					lua_pushstring(L, "");
					lua_pushnumber(L, 1);
					lua_pushboolean(L, false);
					return 4;
				}
				else
				{
					lua_pushstring(L, "Interface\\Buttons\\emptySlot");		// push item texture file
					return 1;
				}*/
			}
			break;

		case eActionItemType_Macro:
			{
				MacroItem* macroItem = g_pMacroFrame->GetMacroItemById( item->macroID );
				if ( macroItem )
				{
					lua_pushstring(L, g_pMacroFrame->GetMacroIcon(macroItem->icon));
					lua_pushstring(L, macroItem->name);
					lua_pushnumber(L, 1);
					lua_pushboolean(L, false);
					return 4;
				}
			}
			break;
		}
	}

	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetActionButtonHotkey(lua_State* L)
{	
	switch ( luaL_checkint(L, 1) )
	{
	case 1:
		lua_pushstring(L, "1");
		break;

	case 2:
		lua_pushstring(L, "2");
		break;

	case 3:
		lua_pushstring(L, "3");
		break;

	case 4:
		lua_pushstring(L, "4");
		break;

	case 5:
		lua_pushstring(L, "5");
		break;

	case 6:
		lua_pushstring(L, "6");
		break;

	case 7:
		lua_pushstring(L, "7");
		break;

	case 8:
		lua_pushstring(L, "8");
		break;

	case 9:
		lua_pushstring(L, "9");
		break;

	case 10:
		lua_pushstring(L, "0");
		break;

	case 11:
		lua_pushstring(L, "-");
		break;

	case 12:
		lua_pushstring(L, "+");
		break;

	default:
		lua_pushnil(L);
	}
	return 1;
}