#include "TitleForm.h"

using namespace ObjDataEdit;

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

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

	FirstTime = false;

	BaseForm::OnActive();

    CB_MainJob->Items->Clear();
    CB_SubJob->Items->Clear();
	CB_MainJob->Items->Add(S"無");
	CB_SubJob->Items->Add(S"無");
    for( unsigned int i = 0 ; i < Global::JobName.size() ; i++ )
    {
        String* Str =  Global::JobName[i].c_str();
        CB_MainJob->Items->Add(Str);
        CB_SubJob->Items->Add(Str);

    }

	sqlCommand_Base->CommandText = "Select ID , Name from Table_String Where Type = '殺死計數' ORDER BY  ID";
	SqlDataReader*  myReader = sqlCommand_Base->ExecuteReader();
	CB_MonsterHountType->Items->Clear();
	CB_MonsterHountType->Items->Add( S"----" );
	while( myReader->Read() )
	{                
		int     Value =  myReader->GetInt32( 0 );
		String* Str   =  myReader->GetString( 1 );

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


	CB_AbilityList1->Items->Clear();
	CB_AbilityList2->Items->Clear();
	CB_AbilityList3->Items->Clear();
	CB_AbilityList4->Items->Clear();
	CB_AbilityList5->Items->Clear();

	CB_AbilityListEx1->Items->Clear();
	CB_AbilityListEx2->Items->Clear();
	CB_AbilityListEx3->Items->Clear();
	CB_AbilityListEx4->Items->Clear();
	CB_AbilityListEx5->Items->Clear();

	for( int i = 0 ; i < EM_WearEqType_MaxCount ; i++ )
	{
		String* Str = GameObjDbStructEx::GetWearEqTypeENUMString( (WearEqTypeENUM)i );
		CB_AbilityList1->Items->Add( Str );
		CB_AbilityList2->Items->Add( Str );
		CB_AbilityList3->Items->Add( Str );
		CB_AbilityList4->Items->Add( Str );
		CB_AbilityList5->Items->Add( Str );

		CB_AbilityListEx1->Items->Add( Str );
		CB_AbilityListEx2->Items->Add( Str );
		CB_AbilityListEx3->Items->Add( Str );
		CB_AbilityListEx4->Items->Add( Str );
		CB_AbilityListEx5->Items->Add( Str );
	}  
}
void    TitleForm::OnSelectObj( bool LockOk )
{
    _ObjDB.GameData.Classification = EM_ObjectClass_Title;
	ObjectModeStruct Mode;
	int TempMode[20];

	Mode._Mode = 0;
	Mode.PreViewTitle = 1;
	TempMode[0] = Mode._Mode;

	for( int i = 0 ; i < CLBModeFlag1->Items->Count ; i ++ )
	{
		if( TempMode[i] & _ObjDB.GameData.Mode._Mode )
			CLBModeFlag1->SetItemChecked( i , true );
		else
			CLBModeFlag1->SetItemChecked( i , false );
	}
}
//輸出時額外設定
void    TitleForm::OnOutputObjSetting( DB_GameObjDbStruct* TempObj )
{
    TempObj->GameData.Classification = EM_ObjectClass_Title;
	TempObj->GameData.Title.MainJob = (Voc_ENUM)(TempObj->GameData.Title.MainJob - 1);
	TempObj->GameData.Title.SubJob = (Voc_ENUM)(TempObj->GameData.Title.SubJob - 1);
	TempObj->GameData.Title.KillCount.KillGroupID--;

	for( int i = 5; i < 10 ; i++ )
	{
		TempObj->GameData.Title.Ability.EqType[ i ] = EM_WearEqType_None;
		TempObj->GameData.Title.Ability.EqTypeValue[ i ] = 0;
	}

	TempObj->GameData.Title.AbilityEx.OnAttackMagicRate = 0;
	TempObj->GameData.Title.AbilityEx.OnAttackMagicRank = 0;
	TempObj->GameData.Title.AbilityEx.OnAttackMagicID = 0;

	for( int i = 5; i < 10 ; i++ )
	{
		TempObj->GameData.Title.AbilityEx.EqType[ i ] = EM_WearEqType_None;
		TempObj->GameData.Title.AbilityEx.EqTypeValue[ i ] = 0;
	}
}
void    TitleForm::OnTimeProc()
{

}

int     TitleForm::ObjSize(  )
{
//    int Size = sizeof( GameObjDbStruct ) ;
//    Size = Size & 0xfffffC;
	GameObjDbStruct *P = NULL;
	int Size = sizeof( GameObjDbStruct ) - sizeof( P->Temp ) +  sizeof( P->Title ) + 3;
	Size = Size & 0xfffffC;

    return Size;
}


void    TitleForm::OnValueChange( )
{
	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
	LB_EqTypeOnAttackMagicID->Text = SearchObjName( TempObj->GameData.Title.Ability.OnAttackMagicID );  
	LB_SkillName->Text  = SearchObjName( TempObj->GameData.Title.SkillID );  
	TabControl_Type->SelectedIndex = TempObj->GameData.Title.Type;


//	char	Key1[512];
//	char	Key2[512] = { 0 };
	String*	Key1 = "";
	String*	Key2 = "";

	//sprintf( Key1 , "UI_TITLE_TYPE_%d" , TempObj->GameData.Title.Classify[0] );
	Key1 = String::Format( "UI_TITLE_TYPE_{0}" , __box( TempObj->GameData.Title.Classify[0] ) );
	if( TempObj->GameData.Title.Classify[1] >= 0 )
		Key2 = String::Format( "UI_TITLE_TYPE_{0}_{1}" , __box( TempObj->GameData.Title.Classify[0] ) , __box( TempObj->GameData.Title.Classify[1] ) );

	//找出Key的內容
	sqlCommand_Base1->CommandText = String::Format( "Select [Content] from StringDB Where KeyStr = '{0}'" , Key1 );
	SqlDataReader*  myReader = sqlCommand_Base1->ExecuteReader();
	if( myReader->Read() )
		Key1 =  myReader->GetString( 0 );
	myReader->Close();

	if( Key2->Length != 0 )
	{
		sqlCommand_Base1->CommandText = String::Format( "Select [Content] from StringDB Where KeyStr = '{0}'" , Key2 );
		myReader = sqlCommand_Base1->ExecuteReader();
		if( myReader->Read() )
			Key2 =  myReader->GetString( 0 );
		myReader->Close();

		LB_Classify->Text = String::Format( "{0} - {1}" , Key1 , Key2 );
	}
	else
		LB_Classify->Text = Key1;
}

System::Void TitleForm::TitleType_SelectedIndexChange(System::Object*  sender, System::EventArgs*  e)
{

	TabControl* Box = static_cast<TabControl*>(sender);
	DB_GameObjDbStruct __pin *Data = &_ObjDB;

	if( Box->SelectedIndex ==  Data->GameData.Title.Type )
		return;

	if( CheckModifyAndCheckOut() == false )
		return;

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

	Data->GameData.Title.Type = (TitleNameTypeENUM)Box->SelectedIndex;

	_OnValueChange = true;	

}
//######################################################################################################
System::Void TitleForm::ChangeMode(System::Object *  sender, System::EventArgs *  e)
{
	CheckedListBox* CBox = static_cast<CheckedListBox*>(sender);

	ObjectModeStruct Mode;
	int TempMode[20];

	Mode._Mode = 0;
	Mode.PreViewTitle = 1;
	TempMode[0] = Mode._Mode;

	int Flag = 0;
	for( int i = CBox->Items->Count - 1 ; i >= 0 ; i -- )
	{

		if( CBox->GetItemChecked( i ) == true )
			Flag = Flag + TempMode[ i ];
	}

	if( _ObjDB.GameData.Mode._Mode == Flag )
		return;

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

	_ObjDB.GameData.Mode._Mode = Flag;
}
