#include "RuneForm.h"

using namespace ObjDataEdit;

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

	Reload_AttrTable();

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

	BaseForm::OnActive();

	FirstTime = false;
	CB_SetArmorType->SelectedIndex = 0;
	CB_SetWeaponType->SelectedIndex = 0;

	CB_AbilityList1->Items->Clear();
	CB_AbilityList2->Items->Clear();
	CB_AbilityList3->Items->Clear();
	CB_AbilityList4->Items->Clear();
	CB_AbilityList5->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 );
	}    

	sqlCommand_Base->CommandText = "Select ID , Name from Table_String Where Type = '符文類型' ORDER BY  ID";
	SqlDataReader*  myReader = sqlCommand_Base->ExecuteReader();
	CB_GroupType->Items->Clear();
	while( myReader->Read() )
	{                
		int     Value =  myReader->GetInt32( 0 );
		String* Str   =  myReader->GetString( 1 );
		std::string	std_str = DotNetStr::StringToChar(Str);
		ComboBoxItem* newItem = new ComboBoxItem( Value, std_str.c_str() );
		CB_GroupType->Items->Add( newItem );
	}
	myReader->Close();

	// 幣值設定
	{
		vector< std::string >	vecPriceType;
		sqlCommand_Base->CommandText = "Select Note from Table_SysKeyValue Where KeyStr LIKE 'SYS_PRICETYPE_ICONID_%' ORDER BY KeyStr";
		myReader = sqlCommand_Base->ExecuteReader();

		while( myReader->Read() )		
		{
			std::string sContent		=	DotNetStr::StringToChar( myReader->GetString( 0 ) );
			vecPriceType.push_back( sContent );
		}
		myReader->Close();

		CB_PricesType->Items->Clear();

		for( vector< std::string >::iterator it = vecPriceType.begin(); it != vecPriceType.end(); it++ )
		{
			String*		sContent		= new String( (*it).c_str() );
			CB_PricesType->Items->Add( sContent );
		}
	}
/*
	{
		sqlCommand_Base->CommandText = "Select ID , Name from Table_String Where Type = '符文顏色' ORDER BY  ID";
		SqlDataReader*  myReader = sqlCommand_Base->ExecuteReader();
		CB_ColorType->Items->Clear();
		while( myReader->Read() )
		{                
			int     Value =  myReader->GetInt32( 0 );
			String* Str   =  myReader->GetString( 1 );
			std::string	std_str = DotNetStr::StringToChar(Str);
			ComboBoxItem* newItem = new ComboBoxItem( Value, std_str.c_str() );
			CB_ColorType->Items->Add( newItem );
		}
		myReader->Close();
	}
	*/
}
void    RuneForm::OnSelectObj( bool LockOk )
{
	_ObjDB.GameData.Classification = EM_ObjectClass_Item;
	_ObjDB.GameData.Item.Class = EM_Item_Rune;
	//_ObjDB.GameData.PricesType	= EM_ACPriceType_GameMoney;

 //   panel1->Enabled = LockOk;
}
//輸出時額外設定
void    RuneForm::OnOutputObjSetting( DB_GameObjDbStruct* TempObj )
{
//    TempObj->GameData.MaxHeap = 1;
	TempObj->GameData.BuyUnit = 1;
	TempObj->GameData.Item.ItemType = EM_ItemType_Normal;
    TempObj->GameData.Classification = EM_ObjectClass_Item;
	TempObj->GameData.Item.Class = EM_Item_Rune;
	TempObj->GameData.Item.IsItemToItem = true;
	TempObj->GameData.LiveTimeType = EM_ItemLiveTimeType_None;
	TempObj->GameData.Item.SelectBaseAttrCount = 0;
	TempObj->GameData.Item.CastingTime = 3;
	TempObj->GameData.Item.BodyMaxCount = 0;
	TempObj->GameData.Mode._Mode = 0;
	TempObj->GameData.Mode.Sell = 1;
	TempObj->GameData.Mode.Expense = 1;
	//TempObj->GameData.PricesType	= EM_ACPriceType_GameMoney;
	TempObj->GameData.Item.IsUseNeedPassword = false;

	//ＡＣ搜尋分類資料設定
	TempObj->GameData.ACSearchType[0] = _DEF_ACSEARCHTYPE_POWERUP;
	TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_POWERUP_RUNE;
	TempObj->GameData.ACSearchType[2] = _DEF_ACSEARCHTYPE_EMPTY;
	/*
	switch( TempObj->GameData.Item.RuneType )
	{
	case EM_RuneType_Manufacture:	//生產
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_MANUFACTURE;
		break;
	case EM_RuneType_Weapon:		//武器
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_WEAPON;
		break;
	case EM_RuneType_Head:			//頭部
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_HEAD;
		break;
	case EM_RuneType_Body:			//上身
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_BODY;
		break;
	case EM_RuneType_Pant:			//下身
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_PANT;
		break;
	case EM_RuneType_Special:		//特殊
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_SPECIAL;
		break;
	case EM_RuneType_Armor:
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_ARMOR;
		break;
	case EM_RuneType_Ornament://飾品
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_ORNAMENT;
		break;
	case EM_RuneType_Shield://盾
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_SHIELD;
		break;
	case EM_RuneType_MagicTools://法器
		TempObj->GameData.ACSearchType[1] = _DEF_ACSEARCHTYPE_RUNE_MAGICTOOLS;
		break;
	}
*/
	//-----------------------------------------------------
	//限制設定
	//-----------------------------------------------------
	if( TempObj->GameData.Limit.Race._Race == 0 )
		TempObj->GameData.Limit.Race._Race = -1;

	if( TempObj->GameData.Limit.Sex._Sex == 0 )
		TempObj->GameData.Limit.Sex._Sex = -1;

	if( TempObj->GameData.Limit.Voc._Voc == 0 )
		TempObj->GameData.Limit.Voc._Voc = -1;

	if( TempObj->GameData.Limit.Level <= 0 )
		TempObj->GameData.Limit.Level = 1;

	//標準價計算
	if(		TempObj->GameData.Item.IsStandardCost 
		&&	TempObj->GameData.PricesType == EM_ACPriceType_GameMoney )
	{
		//TempObj->GameData.Cost = DotNetStr::StringToInt( TB_StandardCost->Text );
		int RLv = TempObj->GameData.Item.ReferenceLv;
		if( RLv > 200 )
			RLv = 200;
		if( RLv <= 1 )
			RLv = 1;
		float	StandardCost = float( RoleDataEx::StandardCostTable[ RLv -1 ] ) /10;

		TempObj->GameData.Cost = StandardCost;
	}
/*
	for( int i = 5 ; i < Max_WearEqType_Count ; i++ )
	{
		TempObj->GameData.Item.Ability.EqTypeValue[ i ] = EM_WearEqType_None;
		TempObj->GameData.Item.Ability.EqTypeValue[ i ] = 0;
	}
*/

	//NUD_Ability
	if( TempObj->GameData.Item.IsStandardAbility )
	{
		
		int LV	 = TempObj->GameData.Item.StandardAbility_Lv;
		for( int i = 0 ;i < 5 ; i++ )
			TempObj->GameData.Item.Ability.EqTypeValue[i] = GetStandardAbilityValue( LV , TempObj->GameData.Item.StandardAbility_ArgType[i] , TempObj->GameData.Item.StandardAbility_Arg[i] );
	}

}
void    RuneForm::OnTimeProc()
{


}

int     RuneForm::ObjSize(  )
{
    GameObjDbStruct *P = NULL;
    int Size = sizeof( GameObjDbStruct ) - sizeof( P->Temp ) + sizeof(P->Item ) + 3;
    Size = Size & 0xfffffC;
    return Size;
}

/*
int		RuneForm::_GetAbilityValue( int LV , int Type , float Rate )
{
	LV--;
	if( LV < 0 )
		LV = 0;
	if( LV >= Global::AbilityPoint.size() )
		LV = Global::AbilityPoint.size() -1;

	switch( Type )
	{
	case 0:	
		return int( Global::AbilityPoint[LV] * Rate );
	case 1:
		return int( Global::DefPoint[LV] * Rate );
	case 2:
		return int( Global::HitPoint[LV] * Rate );
	}

	return 0;
}*/
void    RuneForm::OnValueChange( )
{
	bool _IsValueChange = true;

	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
	LB_EqTypeOnAttackMagicID->Text = SearchObjName( TempObj->GameData.Item.Ability.OnAttackMagicID );  
	LB_CardName->Text = SearchObjName( TempObj->GameData.Item.CardID );

	//-----------------------------------------------------
	//標準價計算
	int RLv = DotNetStr::StringToInt( TB_ReferenceLv->Text );
	if( RLv > 200 )
		RLv = 200;
	if( RLv <= 1 )
		RLv = 1;
	float	StandardCost = float( RoleDataEx::StandardCostTable[ RLv -1 ] ) /10;

	TB_StandardCost->Text = String::Format( "{0}" , __box( int( StandardCost+0.5 ) ) );

	if(		TempObj->GameData.Item.IsStandardCost 
		&&	TempObj->GameData.PricesType == EM_ACPriceType_GameMoney )
	{
		//TB_Cost->Enabled = false;
	}
	else
	{
		//TB_Cost->Enabled = true;
	}

	if( CB_PricesType->SelectedIndex == 0 )
	{
		TB_SpecialMoneyCost->Visible = false;
	}
	else
	{
		TB_SpecialMoneyCost->Visible = true;
	}

	//NUD_Ability
	if( TempObj->GameData.Item.IsStandardAbility )
	{
		tabControl1->SelectedIndex = 1;		
		int LV	 = TempObj->GameData.Item.StandardAbility_Lv;
				
		NUD_Ability1->Value = GetStandardAbilityValue( LV , TempObj->GameData.Item.StandardAbility_ArgType[0] , TempObj->GameData.Item.StandardAbility_Arg[0] );
		NUD_Ability2->Value = GetStandardAbilityValue( LV , TempObj->GameData.Item.StandardAbility_ArgType[1] , TempObj->GameData.Item.StandardAbility_Arg[1] );
		NUD_Ability3->Value = GetStandardAbilityValue( LV , TempObj->GameData.Item.StandardAbility_ArgType[2] , TempObj->GameData.Item.StandardAbility_Arg[2] );
		NUD_Ability4->Value = GetStandardAbilityValue( LV , TempObj->GameData.Item.StandardAbility_ArgType[3] , TempObj->GameData.Item.StandardAbility_Arg[3] );
		NUD_Ability5->Value = GetStandardAbilityValue( LV , TempObj->GameData.Item.StandardAbility_ArgType[4] , TempObj->GameData.Item.StandardAbility_Arg[4] );
	}
	else
	{
		tabControl1->SelectedIndex = 0;		
	}
}

System::Void RuneForm::SetArmorClick(System::Object*  sender, System::EventArgs*  e)
{

	switch( CB_SetArmorType->SelectedIndex )
	{
	case 0:	
		for( int i = 0 ; i < CLB_ArmorPos->Items->Count ; i ++ )
		{
			CLB_ArmorPos->SetItemChecked( i , true );
		}

		ChangeFlag( CLB_ArmorPos  , NULL );

		for( int i = 0 ; i < CLB_ArmorType->Items->Count ; i ++ )
		{
			CLB_ArmorType->SetItemChecked( i , true );
		}

		ChangeFlag( CLB_ArmorType  , NULL );

		break;
	case 1:	
		for( int i = 0 ; i < CLB_ArmorPos->Items->Count ; i ++ )
		{
			CLB_ArmorPos->SetItemChecked( i , false );
		}

		ChangeFlag( CLB_ArmorPos  , NULL );

		for( int i = 0 ; i < CLB_ArmorType->Items->Count ; i ++ )
		{
			CLB_ArmorType->SetItemChecked( i , false );
		}

		ChangeFlag( CLB_ArmorType  , NULL );

		break;
	}
}


System::Void RuneForm::SetWeaponClick(System::Object*  sender, System::EventArgs*  e)
{
	switch( CB_SetWeaponType->SelectedIndex )
	{
	case 0://選擇全部類別
		for( int i = 0 ; i < CLB_WeaponType->Items->Count ; i ++ )
		{
			CLB_WeaponType->SetItemChecked( i , true );
		}

		CB_WeaponPos->SelectedIndex = 0;
		break;
	case 1://清除全部類別

		for( int i = 0 ; i < CLB_WeaponType->Items->Count ; i ++ )
		{
			CLB_WeaponType->SetItemChecked( i , false );
		}

		CB_WeaponPos->SelectedIndex = 0;
		break;

	case 2://近戰武器
		CB_WeaponPos->SelectedIndex = 0;

		CLB_WeaponType->SetItemChecked( 0 ,  true );	//拳套
		CLB_WeaponType->SetItemChecked( 1 ,  true );	//劍
		CLB_WeaponType->SetItemChecked( 2 ,  true );	//匕首
		CLB_WeaponType->SetItemChecked( 3 ,  true );	//槌棍棒
		CLB_WeaponType->SetItemChecked( 4 ,  true );	//權杖
		CLB_WeaponType->SetItemChecked( 5 ,  true );	//斧
		CLB_WeaponType->SetItemChecked( 6 ,  true );	//雙手斧
		CLB_WeaponType->SetItemChecked( 7 ,  true );	//雙手杖
		CLB_WeaponType->SetItemChecked( 8 ,  true );	//雙手劍
		CLB_WeaponType->SetItemChecked( 9 ,  true );	//雙手槌
		CLB_WeaponType->SetItemChecked( 10,  true );	//槍(長矛)
		CLB_WeaponType->SetItemChecked( 11, false );	//遠程武器 弓
		CLB_WeaponType->SetItemChecked( 12, false );	//遠程武器 十字弓
		CLB_WeaponType->SetItemChecked( 13, false );	//遠程武器 槍

		break;
	case 3://遠程武器
		CB_WeaponPos->SelectedIndex = 0;

		CLB_WeaponType->SetItemChecked( 0 , false );	//拳套
		CLB_WeaponType->SetItemChecked( 1 , false );	//劍
		CLB_WeaponType->SetItemChecked( 2 , false );	//匕首
		CLB_WeaponType->SetItemChecked( 3 , false );	//槌棍棒
		CLB_WeaponType->SetItemChecked( 4 , false );	//權杖
		CLB_WeaponType->SetItemChecked( 5 , false );	//斧
		CLB_WeaponType->SetItemChecked( 6 , false );	//雙手斧
		CLB_WeaponType->SetItemChecked( 7 , false );	//雙手杖
		CLB_WeaponType->SetItemChecked( 8 , false );	//雙手劍
		CLB_WeaponType->SetItemChecked( 9 , false );	//雙手槌
		CLB_WeaponType->SetItemChecked( 10, false );	//槍(長矛)
		CLB_WeaponType->SetItemChecked( 11, true  );	//遠程武器 弓
		CLB_WeaponType->SetItemChecked( 12, true  );	//遠程武器 十字弓
		CLB_WeaponType->SetItemChecked( 13, true  );	//遠程武器 槍

		break;
	case 4://雙手武器
		CB_WeaponPos->SelectedIndex = 0;

		CLB_WeaponType->SetItemChecked( 0 , true  );	//拳套
		CLB_WeaponType->SetItemChecked( 1 , false );	//劍
		CLB_WeaponType->SetItemChecked( 2 , false );	//匕首
		CLB_WeaponType->SetItemChecked( 3 , false );	//槌棍棒
		CLB_WeaponType->SetItemChecked( 4 , false );	//權杖
		CLB_WeaponType->SetItemChecked( 5 , false );	//斧
		CLB_WeaponType->SetItemChecked( 6 ,  true );	//雙手斧
		CLB_WeaponType->SetItemChecked( 7 ,  true );	//雙手杖
		CLB_WeaponType->SetItemChecked( 8 ,  true );	//雙手劍
		CLB_WeaponType->SetItemChecked( 9 ,  true );	//雙手槌
		CLB_WeaponType->SetItemChecked( 10,  true );	//槍(長矛)
		CLB_WeaponType->SetItemChecked( 11, false );	//遠程武器 弓
		CLB_WeaponType->SetItemChecked( 12, false );	//遠程武器 十字弓
		CLB_WeaponType->SetItemChecked( 13, false );	//遠程武器 槍
		break;
	case 5://單手武器
		CB_WeaponPos->SelectedIndex = 0;

		CLB_WeaponType->SetItemChecked( 0 , false );	//拳套
		CLB_WeaponType->SetItemChecked( 1 , true  );	//劍
		CLB_WeaponType->SetItemChecked( 2 , true  );	//匕首
		CLB_WeaponType->SetItemChecked( 3 , true  );	//槌棍棒
		CLB_WeaponType->SetItemChecked( 4 , true  );	//權杖
		CLB_WeaponType->SetItemChecked( 5 , true  );	//斧
		CLB_WeaponType->SetItemChecked( 6 , false );	//雙手斧
		CLB_WeaponType->SetItemChecked( 7 , false );	//雙手杖
		CLB_WeaponType->SetItemChecked( 8 , false );	//雙手劍
		CLB_WeaponType->SetItemChecked( 9 , false );	//雙手槌
		CLB_WeaponType->SetItemChecked( 10, false );	//槍(長矛)
		CLB_WeaponType->SetItemChecked( 11, false );	//遠程武器 弓
		CLB_WeaponType->SetItemChecked( 12, false );	//遠程武器 十字弓
		CLB_WeaponType->SetItemChecked( 13, false );	//遠程武器 槍
		break;
	case 6://法系武器
		CB_WeaponPos->SelectedIndex = 1;
		CLB_WeaponType->SetItemChecked( 0 , false );	//拳套
		CLB_WeaponType->SetItemChecked( 1 , true  );	//劍
		CLB_WeaponType->SetItemChecked( 2 , true  );	//匕首
		CLB_WeaponType->SetItemChecked( 3 , true  );	//槌棍棒
		CLB_WeaponType->SetItemChecked( 4 , true  );	//權杖
		CLB_WeaponType->SetItemChecked( 5 , false );	//斧
		CLB_WeaponType->SetItemChecked( 6 , false );	//雙手斧
		CLB_WeaponType->SetItemChecked( 7 , true  );	//雙手杖
		CLB_WeaponType->SetItemChecked( 8 , false );	//雙手劍
		CLB_WeaponType->SetItemChecked( 9 , false );	//雙手槌
		CLB_WeaponType->SetItemChecked( 10, false );	//槍(長矛)
		CLB_WeaponType->SetItemChecked( 11, false );	//遠程武器 弓
		CLB_WeaponType->SetItemChecked( 12, false );	//遠程武器 十字弓
		CLB_WeaponType->SetItemChecked( 13, false );	//遠程武器 槍
		break;
	}
	ChangeFlag( CLB_WeaponType  , NULL );
}
//######################################################################################################

System::Void RuneForm::AbilityType_SelectedIndexChange(System::Object*  sender, System::EventArgs*  e)
{

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

	if( (Box->SelectedIndex!= 0) ==  Data->GameData.Item.IsStandardAbility )
		return;

	if( CheckModifyAndCheckOut() == false )
		return;

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


	if( Box->SelectedIndex == 0 )
		Data->GameData.Item.IsStandardAbility = false;
	else
		Data->GameData.Item.IsStandardAbility = true;

	_OnValueChange = true;	
}