#include "SuitForm.h"


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

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

	FirstTime = false;

	BaseForm::OnActive();

	CB_AbilityList->Add( CB_AbilityList1);
	CB_AbilityList->Add( CB_AbilityList2);
	CB_AbilityList->Add( CB_AbilityList3);
	CB_AbilityList->Add( CB_AbilityList4);
	CB_AbilityList->Add( CB_AbilityList5);
	CB_AbilityList->Add( CB_AbilityList6);
	CB_AbilityList->Add( CB_AbilityList7);
	CB_AbilityList->Add( CB_AbilityList8);
	CB_AbilityList->Add( CB_AbilityList9);
	CB_AbilityList->Add( CB_AbilityList10);
	CB_AbilityList->Add( CB_AbilityList11);
	CB_AbilityList->Add( CB_AbilityList12);
	CB_AbilityList->Add( CB_AbilityList13);
	CB_AbilityList->Add( CB_AbilityList14);
	CB_AbilityList->Add( CB_AbilityList15);
	CB_AbilityList->Add( CB_AbilityList16);
	CB_AbilityList->Add( CB_AbilityList17);
	CB_AbilityList->Add( CB_AbilityList18);
	CB_AbilityList->Add( CB_AbilityList19);
	CB_AbilityList->Add( CB_AbilityList20);
	CB_AbilityList->Add( CB_AbilityList21);
	CB_AbilityList->Add( CB_AbilityList22);
	CB_AbilityList->Add( CB_AbilityList23);
	CB_AbilityList->Add( CB_AbilityList24);
	CB_AbilityList->Add( CB_AbilityList25);
	CB_AbilityList->Add( CB_AbilityList26);
	CB_AbilityList->Add( CB_AbilityList27);
	CB_AbilityList->Add( CB_AbilityList28);
	CB_AbilityList->Add( CB_AbilityList29);
	CB_AbilityList->Add( CB_AbilityList30);
	CB_AbilityList->Add( CB_AbilityList31);
	CB_AbilityList->Add( CB_AbilityList32);
	CB_AbilityList->Add( CB_AbilityList33);
	CB_AbilityList->Add( CB_AbilityList34);
	CB_AbilityList->Add( CB_AbilityList35);
	CB_AbilityList->Add( CB_AbilityList36);
	CB_AbilityList->Add( CB_AbilityList37);

	NUD_Value->Add( NUD_Value1 );
	NUD_Value->Add( NUD_Value2 );
	NUD_Value->Add( NUD_Value3 );
	NUD_Value->Add( NUD_Value4 );
	NUD_Value->Add( NUD_Value5 );
	NUD_Value->Add( NUD_Value6 );
	NUD_Value->Add( NUD_Value7 );
	NUD_Value->Add( NUD_Value8 );
	NUD_Value->Add( NUD_Value9 );
	NUD_Value->Add( NUD_Value10 );
	NUD_Value->Add( NUD_Value11 );
	NUD_Value->Add( NUD_Value12 );
	NUD_Value->Add( NUD_Value13 );
	NUD_Value->Add( NUD_Value14 );
	NUD_Value->Add( NUD_Value15 );
	NUD_Value->Add( NUD_Value16 );
	NUD_Value->Add( NUD_Value17 );
	NUD_Value->Add( NUD_Value18 );
	NUD_Value->Add( NUD_Value19 );
	NUD_Value->Add( NUD_Value20 );
	NUD_Value->Add( NUD_Value21 );
	NUD_Value->Add( NUD_Value22 );
	NUD_Value->Add( NUD_Value23 );
	NUD_Value->Add( NUD_Value24 );
	NUD_Value->Add( NUD_Value25 );
	NUD_Value->Add( NUD_Value26 );
	NUD_Value->Add( NUD_Value27 );
	NUD_Value->Add( NUD_Value28 );
	NUD_Value->Add( NUD_Value29 );
	NUD_Value->Add( NUD_Value30 );
	NUD_Value->Add( NUD_Value31 );
	NUD_Value->Add( NUD_Value32 );
	NUD_Value->Add( NUD_Value33 );
	NUD_Value->Add( NUD_Value34 );
	NUD_Value->Add( NUD_Value35 );
	NUD_Value->Add( NUD_Value36 );
	NUD_Value->Add( NUD_Value37 );


	for( int i = 0 ; i < 37 ; i++ )
	{
		ComboBox* CB_Temp = static_cast<ComboBox*>( CB_AbilityList->Item[i] );

		CB_Temp->Items->Clear();
		for( int j = 0 ; j < EM_WearEqType_MaxCount ; j++ )
		{
			String* Str = GameObjDbStructEx::GetWearEqTypeENUMString( (WearEqTypeENUM)j );
			CB_Temp->Items->Add( Str );
		}
	}


	CB_PreviewTotal->SelectedIndex = 0;
/*
	CB_AbilityList1 ->Items->Clear();
	CB_AbilityList2 ->Items->Clear();
	CB_AbilityList3 ->Items->Clear();
	CB_AbilityList4 ->Items->Clear();
	CB_AbilityList5 ->Items->Clear();
	CB_AbilityList6 ->Items->Clear();
	CB_AbilityList7 ->Items->Clear();
	CB_AbilityList8 ->Items->Clear();
	CB_AbilityList9 ->Items->Clear();
	CB_AbilityList10->Items->Clear();
	CB_AbilityList11->Items->Clear();
	CB_AbilityList12->Items->Clear();
	CB_AbilityList13->Items->Clear();
	CB_AbilityList14->Items->Clear();
	CB_AbilityList15->Items->Clear();
	CB_AbilityList16->Items->Clear();
	CB_AbilityList17->Items->Clear();
	CB_AbilityList18->Items->Clear();
	CB_AbilityList19->Items->Clear();
	CB_AbilityList20->Items->Clear();
	CB_AbilityList21->Items->Clear();
	CB_AbilityList22->Items->Clear();
	CB_AbilityList23->Items->Clear();
	CB_AbilityList24->Items->Clear();
	CB_AbilityList25->Items->Clear();
	CB_AbilityList26->Items->Clear();
	CB_AbilityList27->Items->Clear();
	CB_AbilityList28->Items->Clear();
	CB_AbilityList29->Items->Clear();
	CB_AbilityList30->Items->Clear();
	CB_AbilityList31->Items->Clear();
	CB_AbilityList32->Items->Clear();
	CB_AbilityList33->Items->Clear();
	CB_AbilityList34->Items->Clear();
	CB_AbilityList35->Items->Clear();
	CB_AbilityList36->Items->Clear();
	CB_AbilityList37->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_AbilityList6 ->Items->Add( Str );
		CB_AbilityList7 ->Items->Add( Str );
		CB_AbilityList8 ->Items->Add( Str );
		CB_AbilityList9 ->Items->Add( Str );
		CB_AbilityList10->Items->Add( Str );
		CB_AbilityList11->Items->Add( Str );
		CB_AbilityList12->Items->Add( Str );
		CB_AbilityList13->Items->Add( Str );
		CB_AbilityList14->Items->Add( Str );
		CB_AbilityList15->Items->Add( Str );
		CB_AbilityList16->Items->Add( Str );
		CB_AbilityList17->Items->Add( Str );
		CB_AbilityList18->Items->Add( Str );
		CB_AbilityList19->Items->Add( Str );
		CB_AbilityList20->Items->Add( Str );
		CB_AbilityList21->Items->Add( Str );
		CB_AbilityList22->Items->Add( Str );
		CB_AbilityList23->Items->Add( Str );
		CB_AbilityList24->Items->Add( Str );
		CB_AbilityList25->Items->Add( Str );
		CB_AbilityList26->Items->Add( Str );
		CB_AbilityList27->Items->Add( Str );
		CB_AbilityList28->Items->Add( Str );
		CB_AbilityList29->Items->Add( Str );
		CB_AbilityList30->Items->Add( Str );
		CB_AbilityList31->Items->Add( Str );
		CB_AbilityList32->Items->Add( Str );
		CB_AbilityList33->Items->Add( Str );
		CB_AbilityList34->Items->Add( Str );
		CB_AbilityList35->Items->Add( Str );
		CB_AbilityList36->Items->Add( Str );
		CB_AbilityList37->Items->Add( Str );
	}    
*/
}
void    SuitForm::OnSelectObj( bool LockOk )
{
	int ID = 0;
    _ObjDB.GameData.Classification = EM_ObjectClass_Suit;
	
	sqlCommand_Base->CommandText = String::Format( "Select GUID from WeaponObjectDB Where SuitID = {0}" , __box( _ObjGUID ) );
	SqlDataReader* myReader = sqlCommand_Base->ExecuteReader();
	//memset( (void*)(&_ObjDB.GameData.Suit.SuitList) , 0 , sizeof(_ObjDB.GameData.Suit.SuitList) );
	for( int i = 0 ; i < 10 ; i++ )
		_ObjDB.GameData.Suit.SuitList[i] = 0;

	while( myReader->Read())
	{
		if( ID >= 10 )
			break;
		_ObjDB.GameData.Suit.SuitList[ ID++ ] = myReader->GetInt32( 0 );
		
	}
	myReader->Close();

	sqlCommand_Base->CommandText = String::Format( "Select GUID from ArmorObjectDB Where SuitID = {0}" , __box( _ObjGUID ) );
	myReader = sqlCommand_Base->ExecuteReader();

	while( myReader->Read())
	{
		if( ID >= 10 )
			break;
		_ObjDB.GameData.Suit.SuitList[ ID++ ] = myReader->GetInt32( 0 );

	}
	myReader->Close();

	Fix_SuitList();
}
//輸出時額外設定
void    SuitForm::OnOutputObjSetting( DB_GameObjDbStruct* TempObj )
{
    TempObj->GameData.Classification = EM_ObjectClass_Suit;
	ReCalSuitValue( TempObj->GameData.Suit );


	int ID = 0;
	sqlCommand_Base1->CommandText = String::Format( "Select GUID from WeaponObjectDB Where SuitID = {0}" , __box( TempObj->GameData.GUID ) );
	SqlDataReader* myReader = sqlCommand_Base1->ExecuteReader();

	while( myReader->Read())
	{
		if( ID >= 10 )
			break;
		TempObj->GameData.Suit.SuitList[ ID++ ] = myReader->GetInt32( 0 );

	}
	myReader->Close();

	sqlCommand_Base1->CommandText = String::Format( "Select GUID from ArmorObjectDB Where SuitID = {0}" , __box( TempObj->GameData.GUID ) );
	myReader = sqlCommand_Base1->ExecuteReader();

	while( myReader->Read())
	{
		if( ID >= 10 )
			break;
		TempObj->GameData.Suit.SuitList[ ID++ ] = myReader->GetInt32( 0 );

	}
	myReader->Close();
}

void    SuitForm::OnTimeProc()
{
    
}
int     SuitForm::ObjSize(  )
{
    GameObjDbStruct *P = NULL;
	//int Size = sizeof( GameObjDbStruct );
    int Size = sizeof( GameObjDbStruct ) - sizeof( P->Temp ) +  sizeof( P->Suit ) + 3;
    Size = Size & 0xfffffC;
    return Size;
}
void    SuitForm::OnValueChange()
{
    DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
//    LB_Inc_Magic->Text = SearchObjName( TempObj->GameData.Attirbute.Inc_Magic ); 

	switch( TempObj->GameData.Suit.TotalCount )
	{
	case 10:	GB_SuitAbility10->Show();
	case 9:		GB_SuitAbility9->Show();
	case 8:		GB_SuitAbility8->Show();
	case 7:		GB_SuitAbility7->Show();
	case 6:		GB_SuitAbility6->Show();
	case 5:		GB_SuitAbility5->Show();
	case 4:		GB_SuitAbility4->Show();
	case 3:		GB_SuitAbility3->Show();
	case 2:		GB_SuitAbility2->Show();
	}

	switch( TempObj->GameData.Suit.TotalCount )
	{
	default:	GB_SuitAbility2->Hide();
	case 2:		GB_SuitAbility3->Hide();
	case 3:		GB_SuitAbility4->Hide();
	case 4:		GB_SuitAbility5->Hide();
	case 5:		GB_SuitAbility6->Hide();
	case 6:		GB_SuitAbility7->Hide();
	case 7:		GB_SuitAbility8->Hide();
	case 8:		GB_SuitAbility9->Hide();
	case 9:		GB_SuitAbility10->Hide();
	case 10:	break;
	}
/*
	GameObjectSuitStruct& Suit = TempObj->GameData.Suit;
	//計算總合
	memset( Suit.SuitValue , 0 , sizeof( Suit.SuitValue ) );
	for( int i = 0 ; i < 3 ; i++ )
	{
		Suit.SuitValue[0].EqType[i] = Suit.SuitBase[0].EqType[i];
		Suit.SuitValue[0].EqTypeValue[i] = Suit.SuitBase[0].EqTypeValue[i];
	}

	for( int i = 1 ; i < Suit.TotalCount-1 ; i++ )
	{
		for( int k = 0 ; k < 10 ; k++ )
		{
			Suit.SuitValue[i].EqType[k] = Suit.SuitValue[i-1].EqType[k];
			Suit.SuitValue[i].EqTypeValue[k] = Suit.SuitValue[i-1].EqTypeValue[k];
		}

		for( int j = 0 ; j < 3 ; j++ )
		{
			for( int k = 0 ; k < 10 ; k++ )
			{
				if( Suit.SuitBase[i].EqType[j] == EM_WearEqType_None )
					break;

				if( Suit.SuitBase[i].EqType[j] == Suit.SuitValue[i].EqType[k] )
				{
						Suit.SuitValue[i].EqTypeValue[k] += Suit.SuitBase[i].EqTypeValue[j];
						break;
				}
				if( Suit.SuitValue[i].EqType[k] == EM_WearEqType_None )
				{
					Suit.SuitValue[i].EqTypeValue[k] = Suit.SuitBase[i].EqTypeValue[j];
					Suit.SuitValue[i].EqType[k]		 = Suit.SuitBase[i].EqType[j];
					break;
				}				
			}
		}
	}
	*/


	ReCalSuitValue( TempObj->GameData.Suit );
	Fix_TotalValue( );	

	GameObjectSuitStruct &Suit = TempObj->GameData.Suit;
	LB_SuitISkillD1->Text	= SearchObjName( Suit.SuitSkillID[0] );
	LB_SuitISkillD2->Text	= SearchObjName( Suit.SuitSkillID[1] );
	LB_SuitISkillD3->Text	= SearchObjName( Suit.SuitSkillID[2] );
	LB_SuitISkillD4->Text	= SearchObjName( Suit.SuitSkillID[3] );
}
//######################################################################################################
void	SuitForm::ReCalSuitValue( GameObjectSuitStruct& Suit )
{
//	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
//	GameObjectSuitStruct& Suit = TempObj->GameData.Suit;
	//計算總合
	memset( Suit.SuitValue , 0 , sizeof( Suit.SuitValue ) );
	for( int i = 0 ; i < 3 ; i++ )
	{
		Suit.SuitValue[0].EqType[i] = Suit.SuitBase[0].EqType[i];
		Suit.SuitValue[0].EqTypeValue[i] = Suit.SuitBase[0].EqTypeValue[i];
	}

	for( int i = 1 ; i < Suit.TotalCount-1 ; i++ )
	{
		for( int k = 0 ; k < 10 ; k++ )
		{
			Suit.SuitValue[i].EqType[k] = Suit.SuitValue[i-1].EqType[k];
			Suit.SuitValue[i].EqTypeValue[k] = Suit.SuitValue[i-1].EqTypeValue[k];
		}

		for( int j = 0 ; j < 3 ; j++ )
		{
			for( int k = 0 ; k < 10 ; k++ )
			{
				if( Suit.SuitBase[i].EqType[j] == EM_WearEqType_None )
					break;

				if( Suit.SuitBase[i].EqType[j] == Suit.SuitValue[i].EqType[k] )
				{
					Suit.SuitValue[i].EqTypeValue[k] += Suit.SuitBase[i].EqTypeValue[j];
					break;
				}
				if( Suit.SuitValue[i].EqType[k] == EM_WearEqType_None )
				{
					Suit.SuitValue[i].EqTypeValue[k] = Suit.SuitBase[i].EqTypeValue[j];
					Suit.SuitValue[i].EqType[k]		 = Suit.SuitBase[i].EqType[j];
					break;
				}				
			}
		}
	}
}

void	SuitForm::Fix_TotalValue( )
{
	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
	GameObjectSuitStruct& Suit = TempObj->GameData.Suit;

	int Count = CB_PreviewTotal->SelectedIndex;
/*
	CB_AbilityList1->SelectedIndex = Suit.SuitValue[Count].EqType[0];
	CB_AbilityList2->SelectedIndex = Suit.SuitValue[Count].EqType[1];
	CB_AbilityList3->SelectedIndex = Suit.SuitValue[Count].EqType[2];
	CB_AbilityList4->SelectedIndex = Suit.SuitValue[Count].EqType[3];
	CB_AbilityList5->SelectedIndex = Suit.SuitValue[Count].EqType[4];
	CB_AbilityList6->SelectedIndex = Suit.SuitValue[Count].EqType[5];
	CB_AbilityList7->SelectedIndex = Suit.SuitValue[Count].EqType[6];
	CB_AbilityList8->SelectedIndex = Suit.SuitValue[Count].EqType[7];
	CB_AbilityList9->SelectedIndex = Suit.SuitValue[Count].EqType[8];
	CB_AbilityList10->SelectedIndex = Suit.SuitValue[Count].EqType[9];

	NUD_Value1->Value = Suit.SuitValue[Count].EqTypeValue[0];
	NUD_Value2->Value = Suit.SuitValue[Count].EqTypeValue[1];
	NUD_Value3->Value = Suit.SuitValue[Count].EqTypeValue[2];
	NUD_Value4->Value = Suit.SuitValue[Count].EqTypeValue[3];
	NUD_Value5->Value = Suit.SuitValue[Count].EqTypeValue[4];
	NUD_Value6->Value = Suit.SuitValue[Count].EqTypeValue[5];
	NUD_Value7->Value = Suit.SuitValue[Count].EqTypeValue[6];
	NUD_Value8->Value = Suit.SuitValue[Count].EqTypeValue[7];
	NUD_Value9->Value = Suit.SuitValue[Count].EqTypeValue[8];
	NUD_Value10->Value = Suit.SuitValue[Count].EqTypeValue[9];
*/

	for( int i = 0 ; i < 10 ; i++ )
	{
		ComboBox*		CB_Temp = static_cast<ComboBox*>( CB_AbilityList->Item[i] );
		NumericUpDown*	NUD_Temp = static_cast<NumericUpDown*>( NUD_Value->Item[i] );

		CB_Temp->SelectedIndex = Suit.SuitValue[Count].EqType[i];
		NUD_Temp->Value = Suit.SuitValue[Count].EqTypeValue[i];
	}


	for( int i = 0 ; i < 37 ; i++ )
	{
		ComboBox*		CB_Temp = static_cast<ComboBox*>( CB_AbilityList->Item[i] );
		NumericUpDown*	NUD_Temp = static_cast<NumericUpDown*>( NUD_Value->Item[i] );

		if( CB_Temp->SelectedIndex == 0 )
		{
			CB_Temp->BackColor = System::Drawing::SystemColors::Control;
			NUD_Temp->Hide();
		}
		else
		{
			CB_Temp->BackColor = System::Drawing::SystemColors::Window;            
			NUD_Temp->Show();
		}
	}
}

void	SuitForm::Fix_SuitList( )
{
	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
	GameObjectSuitStruct& Suit = TempObj->GameData.Suit;


	LB_SuitList1->Text		= SearchObjName( Suit.SuitList[0] );
	LB_SuitList2->Text		= SearchObjName( Suit.SuitList[1] );
	LB_SuitList3->Text		= SearchObjName( Suit.SuitList[2] );
	LB_SuitList4->Text		= SearchObjName( Suit.SuitList[3] );
	LB_SuitList5->Text		= SearchObjName( Suit.SuitList[4] );
	LB_SuitList6->Text		= SearchObjName( Suit.SuitList[5] );
	LB_SuitList7->Text		= SearchObjName( Suit.SuitList[6] );
	LB_SuitList8->Text		= SearchObjName( Suit.SuitList[7] );
	LB_SuitList9->Text		= SearchObjName( Suit.SuitList[8] );
	LB_SuitList10->Text		= SearchObjName( Suit.SuitList[9] );


	TB_SuitList1->Text		= String::Format( "{0}" , __box( Suit.SuitList[0] ) );
	TB_SuitList2->Text		= String::Format( "{0}" , __box( Suit.SuitList[1] ) );
	TB_SuitList3->Text		= String::Format( "{0}" , __box( Suit.SuitList[2] ) );
	TB_SuitList4->Text		= String::Format( "{0}" , __box( Suit.SuitList[3] ) );
	TB_SuitList5->Text		= String::Format( "{0}" , __box( Suit.SuitList[4] ) );
	TB_SuitList6->Text		= String::Format( "{0}" , __box( Suit.SuitList[5] ) );
	TB_SuitList7->Text		= String::Format( "{0}" , __box( Suit.SuitList[6] ) );
	TB_SuitList8->Text		= String::Format( "{0}" , __box( Suit.SuitList[7] ) );
	TB_SuitList9->Text		= String::Format( "{0}" , __box( Suit.SuitList[8] ) );
	TB_SuitList10->Text		= String::Format( "{0}" , __box( Suit.SuitList[9] ) );



}