#include "RecipeForm.h"

using namespace ObjDataEdit;

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

	if( FirstTime  == false )
		return;

	BaseForm::OnActive();

	FirstTime = false;

	SqlDataReader* myReader;
	
	/*
	sqlCommand_Base->CommandText = "Select ID , Name from Table_String Where Type = '配方分類' ORDER BY  ID";
	myReader = sqlCommand_Base->ExecuteReader();
	CB_RecipeType->Items->Clear();
	while( myReader->Read() )
	{                
		int     Value =  myReader->GetInt32( 0 );
		String* Str   =  myReader->GetString( 1 );

		CB_RecipeType->Items->Add( Str );
	}
	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();


	sqlCommand_Base->CommandText = "Select content from StringDB Where KeyStr LIKE 'CRAFT_TYPELIST_%' ORDER BY KeyStr";
	myReader = sqlCommand_Base->ExecuteReader();
	CB_RecipeType->Items->Clear();
	while( myReader->Read() )
	{                
		//int     Value =  myReader->GetInt32( 0 );
		String* Str   =  myReader->GetString( 0 );

		CB_RecipeType->Items->Add( Str );
	}
	myReader->Close();
	sqlCommand_Base->CommandText = "select keystr,content from StringDB where LEFT(KeyStr,14) = 'SYS_STORE_TYPE' order by KeyStr";
	myReader = sqlCommand_Base->ExecuteReader();
	CB_SellType->Items->Clear();

	for( int i = 0 ; ; i++ )
	{                
		if( myReader->Read() == false )
			break;
		//int     Value =  myReader->GetInt32( 0 );
		String* KeyStr   = myReader->GetString( 0 );
		String* Content  = myReader->GetString( 1 );

		string tempStr = DotNetStr::StringToChar( KeyStr );
		const char* cKeyStr = tempStr.c_str();
		const char* cNumStr = strlen("SYS_STORE_TYPE_") + cKeyStr;
		if( i != atol( cNumStr ) )
			break;

		CB_SellType->Items->Add( Content );
	}
	myReader->Close();
}
void    RecipeForm::OnSelectObj( bool LockOk )
{
    //panel1->Enabled = LockOk;
    _ObjDB.GameData.Classification = EM_ObjectClass_Recipe;

	ObjectModeStruct Mode;
	int TempMode[10];

	Mode._Mode = 0;
	Mode.Sell = 1;
	TempMode[0] = Mode._Mode;
	Mode._Mode = 0;
	Mode.Logoutdel = 1;
	TempMode[1] = Mode._Mode;
	Mode._Mode = 0;
	Mode.Expense = 1;
	TempMode[2] = Mode._Mode;
	Mode._Mode = 0;
	Mode.PickupBound = 1;
	TempMode[3] = Mode._Mode;
	Mode._Mode = 0;
	Mode.VariableRare = 1;
	TempMode[4] = Mode._Mode;

	for( int i = 0 ; i < CLBModeFlag->Items->Count ; i ++ )
	{
		if( TempMode[i] & _ObjDB.GameData.Mode._Mode )
			CLBModeFlag->SetItemChecked( i , true );
		else
			CLBModeFlag->SetItemChecked( i , false );
	}
}
//輸出時額外設定
void    RecipeForm::OnOutputObjSetting( DB_GameObjDbStruct* TempObj )
{
    TempObj->GameData.Classification = EM_ObjectClass_Recipe;
	TempObj->GameData.Recipe.Request.Skill = (SkillValueTypeENUM)( TempObj->GameData.Recipe.Request.Skill + EM_SkillValueType_BlackSmith );
	
	TempObj->GameData.MaxHeap = 1;
	TempObj->GameData.BuyUnit = 1;
	TempObj->GameData.LiveTimeType = EM_ItemLiveTimeType_None;
	TempObj->GameData.Item.SelectBaseAttrCount = 0;
	TempObj->GameData.ItemEqType._Skill = 0;
	memset( &(TempObj->GameData.Limit) , 0 , sizeof(TempObj->GameData.Limit) );
	TempObj->GameData.Limit.Level = 1;


	//設定拍賣類別
	TempObj->GameData.ACSearchType[0] = _DEF_ACSEARCHTYPE_RECIPE;
	TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_EMPTY;
	TempObj->GameData.ACSearchType[2] = _DEF_ACSEARCHTYPE_EMPTY;

	TempObj->GameData.Recipe.Request.Skill = (SkillValueTypeENUM)( TempObj->GameData.Recipe.Request.Skill -1 );
	switch( TempObj->GameData.Recipe.Request.Skill  )
	{
	case EM_SkillValueType_BlackSmith   : //配方 打鐵
		TempObj->GameData.ACSearchType[0] = _DEF_ACSEARCHTYPE_RECIPE;
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RECIPE_BLACKSMITH;
		break;
	case EM_SkillValueType_Carpenter    : //配方 木工
		TempObj->GameData.ACSearchType[0] = _DEF_ACSEARCHTYPE_RECIPE;
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RECIPE_CARPENTER;
		break;
	case EM_SkillValueType_MakeArmor     	: //配方 製甲		
		TempObj->GameData.ACSearchType[0] = _DEF_ACSEARCHTYPE_RECIPE;
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RECIPE_ARMOR;
		break;
	case EM_SkillValueType_Tailor     	: //配方 裁縫
		TempObj->GameData.ACSearchType[0] = _DEF_ACSEARCHTYPE_RECIPE;
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RECIPE_TAILOR;
		break;
	case EM_SkillValueType_Alchemy     	: //配方 煉金		
		TempObj->GameData.ACSearchType[0] = _DEF_ACSEARCHTYPE_RECIPE;
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RECIPE_ALCHEMY;
		break;
	case EM_SkillValueType_Cook     	: //配方 烹飪
		TempObj->GameData.ACSearchType[0] = _DEF_ACSEARCHTYPE_RECIPE;
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RECIPE_COOK;
		break;
	}

	map<int , RecipeItemStruct>	SourceMap;
	bool ResortSource = false;
	for( int i = 0 ; i < _MAX_RECIPE_SOURCE_COUNT_ ; i++ )
	{
		RecipeItemStruct TempSource;
		TempSource = TempObj->GameData.Recipe.Source[i];
		
		if( TempSource.Count == 0 )
			continue;
		if( SourceMap.find( TempSource.OrgObjID ) != SourceMap.end() )
		{
			SourceMap[ TempSource.OrgObjID ].Count += TempSource.Count;
			ResortSource = true;
		}
		else
		{
			SourceMap[ TempSource.OrgObjID ] = TempSource;
		}
	}
	if( ResortSource == true )
	{
		memset( TempObj->GameData.Recipe.Source , 0 , sizeof( TempObj->GameData.Recipe.Source ) );
		map<int , RecipeItemStruct>::iterator Iter;
		int i = 0;
		for( Iter = SourceMap.begin() ; Iter != SourceMap.end() ; Iter++ , i++ )
		{
			TempObj->GameData.Recipe.Source[i] = Iter->second;
		}
	}
}
void    RecipeForm::OnTimeProc()
{

}

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

    return Size;
}
void    RecipeForm::OnValueChange( )
{
    DB_GameObjDbStruct __pin * TempObj = &_ObjDB;

	LB_KeyItemName->Text = SearchObjName( TempObj->GameData.Recipe.Request.KeyItemID );

    LB_Source1->Text = SearchObjName( TempObj->GameData.Recipe.Source[0].OrgObjID );
    LB_Source2->Text = SearchObjName( TempObj->GameData.Recipe.Source[1].OrgObjID );
    LB_Source3->Text = SearchObjName( TempObj->GameData.Recipe.Source[2].OrgObjID );
    LB_Source4->Text = SearchObjName( TempObj->GameData.Recipe.Source[3].OrgObjID );
    LB_Source5->Text = SearchObjName( TempObj->GameData.Recipe.Source[4].OrgObjID );
    LB_Source6->Text = SearchObjName( TempObj->GameData.Recipe.Source[5].OrgObjID );
    LB_Source7->Text = SearchObjName( TempObj->GameData.Recipe.Source[6].OrgObjID );
    LB_Source8->Text = SearchObjName( TempObj->GameData.Recipe.Source[7].OrgObjID );

    LB_Item1_Normal->Text = SearchObjName( TempObj->GameData.Recipe.Item1_Normal.OrgObjID );
    LB_Item1_HQ->Text     = SearchObjName( TempObj->GameData.Recipe.Item1_HQ.OrgObjID );
    LB_Item1_Slot1->Text  = SearchObjName( TempObj->GameData.Recipe.Item1_Slot1.OrgObjID );
    LB_Item1_Slot2->Text  = SearchObjName( TempObj->GameData.Recipe.Item1_Slot2.OrgObjID );
    LB_Item1_Slot3->Text  = SearchObjName( TempObj->GameData.Recipe.Item1_Slot3.OrgObjID );
    LB_Item1_Slot4->Text  = SearchObjName( TempObj->GameData.Recipe.Item1_Slot4.OrgObjID );

    LB_Item2->Text = SearchObjName( TempObj->GameData.Recipe.Item2.OrgObjID );
    LB_Item3->Text = SearchObjName( TempObj->GameData.Recipe.Item3.OrgObjID );
    LB_Item4->Text = SearchObjName( TempObj->GameData.Recipe.Item4.OrgObjID );

    char Buf[256];

    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item1_Normal.Rate ) / 1000 );
    LB_Item1_NormalRate->Text = Buf;
    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item1_HQ.Rate ) / 1000 );
    LB_Item1_HQRate->Text     = Buf;
    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item1_Slot1.Rate ) / 1000 );
    LB_Item1_Slot1Rate->Text  = Buf;
    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item1_Slot2.Rate ) / 1000 );
    LB_Item1_Slot2Rate->Text  = Buf;
    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item1_Slot3.Rate ) / 1000 );
    LB_Item1_Slot3Rate->Text  = Buf;
    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item1_Slot4.Rate ) / 1000 );
    LB_Item1_Slot4Rate->Text  = Buf;
    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item2.Rate ) / 1000 );
    LB_Item2Rate->Text = Buf;       
    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item3.Rate ) / 1000 );
    LB_Item3Rate->Text = Buf;
    sprintf( Buf , "%.3f%%" , float( TempObj->GameData.Recipe.Item4.Rate ) / 1000 );
    LB_Item4Rate->Text = Buf;
   

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

	ObjectModeStruct Mode;
	int TempMode[10];

	Mode._Mode = 0;
	Mode.Sell = 1;
	TempMode[0] = Mode._Mode;
	Mode._Mode = 0;
	Mode.Logoutdel = 1;
	TempMode[1] = Mode._Mode;
	Mode._Mode = 0;
	Mode.Expense = 1;
	TempMode[2] = Mode._Mode;
	Mode._Mode = 0;
	Mode.PickupBound = 1;
	TempMode[3] = Mode._Mode;
	Mode._Mode = 0;
	Mode.VariableRare = 1;
	TempMode[4] = Mode._Mode;

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

	int Flag = 0;
	for( int i = CBox->Items->Count - 1  ; i >= 0 ; i -- )
	{
		if( CBox->GetItemChecked( i ) == true )
			Flag = Flag + TempMode[ i ];
	}
	_ObjDB.GameData.Mode._Mode = Flag;
}
//######################################################################################################
