#include "QuestNPCForm.h"

using namespace ObjDataEdit;

//######################################################################################################
//主要需要繼承的涵式
ReaderClass< DB_GameObjDbStruct >* QuestNPCForm::ObjReader( )
{
	return  GameObjDbStructEx::ObjReader_QuestNPC( );
}
void    QuestNPCForm::OnActive()
{
    static bool FirstTime = true;

    //設定種族職業資訊
    if( FirstTime  == false )
        return;

    FirstTime = false;

	BaseForm::OnActive();

    SqlDataReader* myReader;

    sqlCommand_Base->CommandText = "Select GUID , Name from Table_ZoneDB";
    myReader = sqlCommand_Base->ExecuteReader();
    CB_Zone1->Items->Clear();
    CB_Zone2->Items->Clear();
    CB_Zone3->Items->Clear();

    System::Object* AddItem[] = new System::Object*[1];
    String* Str = "0 無";
    AddItem[0] = Str;
    CB_Zone1->Items->AddRange(AddItem);
    CB_Zone2->Items->AddRange(AddItem);
    CB_Zone3->Items->AddRange(AddItem);

    while( myReader->Read())
    {                
        int     Value =  myReader->GetInt32( 0 );
        String* Str   =  myReader->GetString( 1 );

        Str = String::Format( "{0} {1}", __box( Value ) , Str );
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CB_Zone1->Items->AddRange(AddItem);
        CB_Zone2->Items->AddRange(AddItem);
        CB_Zone3->Items->AddRange(AddItem);
    }
    myReader->Close();

	// 需求制造工具
	sqlCommand_Base->CommandText = "Select content from StringDB Where KeyStr LIKE 'CRAFT_TOOLS_%' ORDER BY KeyStr";
	myReader = sqlCommand_Base->ExecuteReader();
	comboBox2->Items->Clear();
	while( myReader->Read() )
	{                
		//int     Value =  myReader->GetInt32( 0 );
		String* Str   =  myReader->GetString( 0 );

		comboBox2->Items->Add( Str );
	}
	myReader->Close();

	CB_SearchGroupID->Items->Clear();
	sqlCommand_Base->CommandText = "Select ID , Name from Table_String Where Type = '企劃搜尋群組' ORDER BY  ID";
	myReader = sqlCommand_Base->ExecuteReader();
	while( myReader->Read() )
	{                
		int     Value =  myReader->GetInt32( 0 );
		String* Str   =  myReader->GetString( 1 );

		CB_SearchGroupID->Items->Add( Str );
	}
	myReader->Close();

}
void    QuestNPCForm::OnSelectObj( bool LockOk )
{
//    panel1->Enabled = LockOk;
    _ObjDB.GameData.Classification = EM_ObjectClass_QuestNPC;

	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
	//ZeroMemory( &( TempObj->GameData.NPC ), sizeof( TempObj->GameData.NPC ) );

	TempObj->GameData.Classification	= EM_ObjectClass_QuestNPC; 
	TempObj->GameData.NPC.TitleType		= EM_NpcTitleTypeENUM_QuestNPC;
	
	TempObj->GameData.NPC.Race				= 0;
	TempObj->GameData.NPC.Vocation			= 0;
	TempObj->GameData.NPC.Sex				= 0;
	TempObj->GameData.NPC.ExpRate			= 0;
	TempObj->GameData.NPC.Level				= 1;
	TempObj->GameData.NPC.LevelRange		= 1;
	TempObj->GameData.NPC.STR				= 10;
	TempObj->GameData.NPC.STA				= 10;
	TempObj->GameData.NPC.INT				= 10;
	TempObj->GameData.NPC.MND				= 10;
	TempObj->GameData.NPC.AGI				= 10;
	TempObj->GameData.NPC.MoveSpeed			= 80;
	TempObj->GameData.NPC.SearchRange		= 15;
	TempObj->GameData.NPC.DropMoney_Base	= 0;
	TempObj->GameData.NPC.DropMoney_Rand	= 0;
	TempObj->GameData.NPC.Brave				= 0;
	TempObj->GameData.NPC.LinkType			= 0;
	TempObj->GameData.NPC.EscapeType		= 0;

	// 檢查設定若有設定重生時間話, 表該 Obj 為 QuestObj
	switch( _ObjDB.GameData.NPC.iQuestMode )
	{
	case 0:
		{
			tab_QuestObjType->SelectedIndex = 0;
		} break;
	case 1:
		{
			tab_QuestObjType->SelectedIndex = 1;
			CB_ReviveTime->Text = String::Format("{0}" , __box( _ObjDB.GameData.ReviveTime ) );	
		} break;
	case 2:
		{
			tab_QuestObjType->SelectedIndex = 2;
			CB_ReviveTime->Text = String::Format("{0}" , __box( _ObjDB.GameData.ReviveTime ) );	
		} break;
	}
}
//輸出時額外設定
void    QuestNPCForm::OnOutputObjSetting( DB_GameObjDbStruct* TempObj )
{
	int iVal = 0;

    TempObj->GameData.Classification	= EM_ObjectClass_QuestNPC; 
	TempObj->GameData.NPC.TitleType		= EM_NpcTitleTypeENUM_QuestNPC;
	TempObj->GameData.NPC.KeepDistance	= 0;
	TempObj->GameData.NPC.KillGroupID	= -1;

	if( TempObj->GameData.NPC.ZoneID_Bk != -1 )
		TempObj->GameData.NPC.ZoneID = TempObj->GameData.NPC.ZoneID_Bk;

	// 將主副手設成相同機率
	
	// 主手
		if( TempObj->GameData.NPC.MainHandEQ[0] != 0 )
		{
			iVal = TempObj->GameData.NPC.MainHandEQ[0];
		}
		else
		{
			iVal = 0;
		}

		for( int i = 0; i < _MAX_NPC_EQSEL_COUNT_; i++  )
		{
			TempObj->GameData.NPC.MainHandEQ[i] = iVal;
		}

	// 副手
		if( TempObj->GameData.NPC.SecHandEQ[0] != 0 )
		{
			iVal = TempObj->GameData.NPC.SecHandEQ[0];
		}
		else
		{
			iVal = 0;
		}

		for( int i = 0; i < _MAX_NPC_EQSEL_COUNT_; i++  )
		{
			TempObj->GameData.NPC.SecHandEQ[i] = iVal;
		}

	// 如果是任務物件, 將顯示頭像框隱藏
//	ZeroMemory( TempObj->GameData.NPC.pQuestLink, sizeof( TempObj->GameData.NPC.pQuestLink ) );

	if( TempObj->GameData.NPC.IsShowNpcPos == false )
	{
		TempObj->GameData.NPC.ZoneID = -1;
		TempObj->GameData.NPC.X = 0;
		TempObj->GameData.NPC.Y = 0;
		TempObj->GameData.NPC.Z = 0;
	}
	TempObj->GameData.NPC.LuaEvent_OnAttackMagic[0] = 0;
	TempObj->GameData.NPC.LuaEvent_OnAssistMagic[0] = 0;
	TempObj->GameData.NPC.LuaEvent_OnDead[0] = 0;
	TempObj->GameData.NPC.LuaEvent_OnKill[0] = 0;

	TempObj->GameData.NPC.iTagID	= TempObj->TagID;

	TempObj->GameData.NPC.CarryType	= EM_NPCCarryType_None;
	TempObj->GameData.NPC.CarryCount= 0;


	// 設定連結關希
	map< int, set< int > >::iterator it = _mapQuestLink->find( TempObj->GameData.GUID );
	if( it != _mapQuestLink->end() )
	{
		int iCount = 0;
		for( set< int >::iterator itQuest = it->second.begin(); itQuest != it->second.end(); itQuest++ )
		{
			if( iCount < _MAX_QUESTLINK_COUNT )
			{
				TempObj->GameData.NPC.iQuestLinkObj[ iCount ] = *itQuest;
				iCount++;
			}
		}
	}

}
void    QuestNPCForm::OnTimeProc()
{

}
int     QuestNPCForm::ObjSize(  )
{
    GameObjDbStruct *P = NULL;
    int Size = sizeof( GameObjDbStruct ) - sizeof( P->Temp ) +  sizeof( P->NPC ) + 3;
    Size = Size & 0xfffffC;
    return Size;
}
//-----------------------------------------------------------------------------------------------
Void QuestNPCForm::OnText_ValueChanged					(System::Object *  sender, System::EventArgs *  e)
{
	// Save it
	GetFloatValue_TextChanged( sender, e );

	TextBox* Edit	= static_cast<TextBox*>(sender);

	if( Edit->Text->Length != 0 ) 
	{
		char				utf8_1 __gc[]	= Encoding::Default->GetBytes( Edit->Text );
		char __pin			*Str			= &utf8_1[0];
		float Value							= (float)atof( Str );

		if( Value != 0 )
		{
			Edit->BackColor = System::Drawing::SystemColors::Window;
			//Reader->Write( Data , Name , Value );
		}
		else
		{
			Edit->BackColor = System::Drawing::Color::Gainsboro;
			Edit->Text = "";

		}
	}
	else
	{
		Edit->BackColor = System::Drawing::Color::Gainsboro;
		Edit->Text = "";
		//Reader->Write( Data , Name , 0 );
	}
}
//-----------------------------------------------------------------------------------------------
Void QuestNPCForm::OnCombox_SelIndexChanged				(System::Object *  sender, System::EventArgs *  e)
{
	GetIndex_SelectedIndexChanged( sender, e );

	ComboBox* Box = static_cast<ComboBox*>(sender);

	if( Box->SelectedIndex == 0 )
	{
		Box->BackColor = System::Drawing::Color::Gainsboro;
	}
	else
	{
		Box->BackColor = System::Drawing::SystemColors::Window;
	}
}
//-----------------------------------------------------------------------------------------------
Void QuestNPCForm::OnText_TextChanged				(System::Object *  sender, System::EventArgs *  e)
{
	GetStr_TextChanged( sender, e );

	TextBox* Edit = static_cast<TextBox*>(sender);

	if( Edit->Text->Length != 0 ) 
	{
		char utf8_1 __gc[] = Encoding::Default->GetBytes( Edit->Text );
		char __pin *Str = &utf8_1[0];
		Edit->BackColor = System::Drawing::SystemColors::Window;
	}
	else
	{
		Edit->BackColor = System::Drawing::Color::Gainsboro;
		Edit->Text = "";
	}
}
//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------
Void QuestNPCForm::OnTab_EnterQuestObj					(System::Object *  sender, System::EventArgs *  e)
{
	int iIndex	= tab_QuestObjType->SelectedIndex;
	//int iSel	= tab_QuestObjType->SelectedTab;
	combox_QuestMode->SelectedIndex = iIndex;

	if( iIndex == 0 )
	{
		// Quest Npc
		_ObjDB.GameData.NPC.iQuestMode = 0;
	}
	else
	{
		if( _ObjDB.GameData.ReviveTime == 0 )
		{
			CB_ReviveTime->SelectedIndex	= 0;

			DB_GameObjDbStruct __pin *Data = &_ObjDB;
			ReaderClass< DB_GameObjDbStruct >*      Reader = ObjReader( );

			{
				String* Tag			= static_cast<String*>(CB_ReviveTime->Tag);
				char utf8 __gc[]	= Encoding::Default->GetBytes( Tag );
				char __pin *Name	= &utf8[0];
				Reader->Write( Data , Name , CB_ReviveTime->SelectedIndex );
			}
			
			


			{
				String* Tag			= static_cast<String*>(combox_QuestMode->Tag);
				char utf8 __gc[]	= Encoding::Default->GetBytes( Tag );
				char __pin *Name	= &utf8[0];
				Reader->Write( Data , Name , combox_QuestMode->SelectedIndex );		
			}
			
		}
	}
}

Void QuestNPCForm::OnCombox_QuestModeChanged				(System::Object *  sender, System::EventArgs *  e)
{
	GetIndex_SelectedIndexChanged( sender, e );

	tab_QuestObjType->SelectedIndex = combox_QuestMode->SelectedIndex;

	if( _ObjDB.GameData.ReviveTime == 0 )
	{
		CB_ReviveTime->SelectedIndex	= 0;
		CB_ReviveTime->Text					= "10";
		//_ObjDB.GameData.NPC.iQuestMode	= 1;
		DB_GameObjDbStruct __pin *Data = &_ObjDB;
		ReaderClass< DB_GameObjDbStruct >*      Reader = ObjReader( );

		{
			String* Tag			= static_cast<String*>(CB_ReviveTime->Tag);
			char utf8 __gc[]	= Encoding::Default->GetBytes( Tag );
			char __pin *Name	= &utf8[0];
			Reader->Write( Data , Name , CB_ReviveTime->SelectedIndex );
		}


	}
}

Void QuestNPCForm::OnCombox_ValueChanged(System::Object *  sender, System::EventArgs *  e)
{
	if( CheckModifyAndCheckOut() == false )
		return;
	/*
	if( _OnUpdate == false && CB_Lock->Checked == false )
	{
	MessageBox::Show( "尚未鎖定，不可修改" , "錯誤" );
	UpDate();
	return;
	}
	*/

	if( CB_Lock->Checked == true )
		_ObjDB.IsModify = true;

	ComboBox*				pBox	= static_cast<ComboBox*>(sender);
	DB_GameObjDbStruct __pin *Data	= &_ObjDB;

	ReaderClass< DB_GameObjDbStruct >*      Reader = ObjReader( );
	String* Tag = static_cast<String*>(pBox->Tag);
	char utf8 __gc[] = Encoding::Default->GetBytes( Tag );
	char __pin *Name = &utf8[0];

	if( pBox->Text->Length != 0 ) 
	{
		char utf8_1 __gc[] = Encoding::Default->GetBytes( pBox->Text );
		char __pin *Str = &utf8_1[0];
		float Value = (float)atof( Str );

		Reader->Write( Data , Name , Value );
	}
	else
		Reader->Write( Data , Name , 0 );

	_OnValueChange = true;
}

void    QuestNPCForm::OnValueChange()
{
	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
	MainHandEq1->Text	= SearchObjName( TempObj->GameData.NPC.MainHandEQ[0] );
	SecHandEq1->Text	= SearchObjName( TempObj->GameData.NPC.SecHandEQ[0] );
	lableRefNPC->Text	= SearchObjName( TempObj->GameData.NPC.iQuestNPCRefID );

	//BowEQ->Text = SearchObjName( TempObj->GameData.NPC.BowEQ );
	LB_OrnamentEQ->Text = SearchObjName( TempObj->GameData.NPC.OrnamentEQ );
}

System::Void QuestNPCForm::FixAllNPCPosClick						(System::Object*  sender, System::EventArgs*  e)
{
	SqlDataReader*	myReader;

	int X , Y , Z , ZoneID;
	for( int i = 0 ; i < (int)_ObjList->size() ; i++ )
	{
		int GUID = (*_ObjList)[i].GUID;
		sqlCommandGlobalDB->CommandText = String::Format( "SELECT TOP (1) ZoneID, X, Y, Z FROM NPCData WHERE (OrgObjID = {0} AND IsDelflag = 0 )" , __box( GUID ) );
		myReader = sqlCommandGlobalDB->ExecuteReader();
		if( myReader->Read() )
		{
			ZoneID = myReader->GetInt32( 0 );
			X = int( myReader->GetFloat( 1 ) );
			Y = int( myReader->GetFloat( 2 ) );
			Z = int( myReader->GetFloat( 3 ) );					
		}
		else
		{
			X = 0;
			Y = 0;
			Z = 0;
			ZoneID = -1;
		}
		myReader->Close();

		sqlCommand_Base->CommandText = String::Concat( String::Format( "UPDATE QuestNPCObjectDB SET ZoneID = {0},X={1},Y={2}", __box(ZoneID) , __box(X) , __box(Y) )
			, String::Format( ",Z={0} WHERE GUID={1}", __box(Z) , __box(GUID) ) );
		sqlCommand_Base->ExecuteReader()->Close();

	}

}