#include "Rune Engine/Rune/Rune.h"
#include "Rune Engine/Rune/Rune Engine NULL.h"
#include "Rune Engine/Rune/Fusion/RuFusion_Trekker.h"
#include "ZoneForm.h"



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

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

	FirstTime = false;

	BaseForm::OnActive();

}
void    ZoneForm::OnSelectObj( bool LockOk )
{
    _ObjDB.GameData.Classification = EM_ObjectClass_Zone;

	LV_MapInfo->Items->Clear();
	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
	GameObjectZoneStruct& Zone = TempObj->GameData.Zone;

	int Count = Zone.MapCount;
	for( int i = 0 ; i < Count ; i++ )
	{
//		ListViewItem* Item = LV_MapInfo->Items->Item[i];
		System::String* StrList[] = new System::String*[3];		

		if( Zone.MapInfoList[i].IsEnabledSave == true )
			StrList[0] = "可";
		else
			StrList[0] = "否";

		StrList[1] = String::Format( "{0}" , __box( Zone.MapInfoList[i].MapID ) );
		StrList[2] = Zone.MapInfoList[i].MapName;

		ListViewItem* NewListViewItem = new ListViewItem( StrList );
		LV_MapInfo->Items->Add( NewListViewItem );
	}
	FixMapInfoList( );

}
//輸出時額外設定
void    ZoneForm::OnOutputObjSetting( DB_GameObjDbStruct* TempObj )
{
    TempObj->GameData.Classification = EM_ObjectClass_Zone;
}

void    ZoneForm::OnTimeProc()
{
    
}
int     ZoneForm::ObjSize(  )
{
    GameObjDbStruct *P = NULL;
    int Size = sizeof( GameObjDbStruct ) - sizeof( P->Temp ) +  sizeof( P->Zone ) + 3;
    Size = Size & 0xfffffC;
    return Size;
}
void    ZoneForm::OnValueChange()
{
    DB_GameObjDbStruct __pin * TempObj = &_ObjDB;

	LB_ZoneID->Text = String::Format( "第{0}區", __box( TempObj->GameData.GUID - Def_ObjectClass_Zone ) );

	GB_CtrlBlock->Enabled = TempObj->GameData.Zone.IsEnabledBlockCtrl;
	GB_PrivateRoom->Enabled = TempObj->GameData.Zone.IsPrivateZone;
	GB_Revive->Enabled = TempObj->GameData.Zone.IsEnabledRevPoint;

}
//######################################################################################################
System::Void ZoneForm::NewMapInfoClick(System::Object*  sender, System::EventArgs*  e) 
{
	if( CheckModifyAndCheckOut() == false )
		return;

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

	if( LV_MapInfo->Items->Count >= 40 )
	{
		MessageBox::Show( L"最多只可以有40筆資料" , L"錯誤" , MessageBoxButtons::OK );			
		return;
	}

	System::String* StrList[] = new System::String*[3];

	if( CB_SavePosition->Checked == true )
		StrList[0] = S"可";
	else
		StrList[0] = S"否";

	StrList[1] = TB_SubMapID->Text;
	StrList[2] = TB_SubMapName->Text;

	ListViewItem*  NewListViewItem = new ListViewItem( StrList );
	LV_MapInfo->Items->Add( NewListViewItem );
	FixMapInfoList( );
	_OnValueChange = true;
}
System::Void ZoneForm::DelMapInfoClick(System::Object*  sender, System::EventArgs*  e) 
{
	if( CheckModifyAndCheckOut() == false )
		return;

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

	ListView::SelectedListViewItemCollection* SelectItem = LV_MapInfo->SelectedItems;

	if( SelectItem->Count == 0 )
		return;

	ListViewItem* Item = SelectItem->Item[0];

	LV_MapInfo->Items->Remove( Item );
	FixMapInfoList( );
	_OnValueChange = true;
}
System::Void ZoneForm::ModifyMapInfoClick(System::Object*  sender, System::EventArgs*  e) 
{
	if( CheckModifyAndCheckOut() == false )
		return;

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

	ListView::SelectedListViewItemCollection* SelectItem = LV_MapInfo->SelectedItems;

	if( SelectItem->Count == 0 )
		return;

	ListViewItem* Item = SelectItem->Item[0];

	if( CB_SavePosition->Checked == true )
		Item->SubItems->Item[0]->Text = S"可";
	else
		Item->SubItems->Item[0]->Text = S"否";

	Item->SubItems->Item[1]->Text = TB_SubMapID->Text;
	Item->SubItems->Item[2]->Text = TB_SubMapName->Text;
	FixMapInfoList( );
	_OnValueChange = true;
}
System::Void ZoneForm::MapInfo_SelectedIndexChanged(System::Object*  sender, System::EventArgs*  e) 
{
	ListView::SelectedListViewItemCollection* SelectItem = LV_MapInfo->SelectedItems;

	if( SelectItem->Count == 0 )
		return;

	ListViewItem* Item = SelectItem->Item[0];

	//if( Item->SubItems->Item[0]->Text->Length == 0 )
	if( String::Compare( Item->SubItems->Item[0]->Text , "否" ) == 0 )
		CB_SavePosition->Checked = false;
	else
		CB_SavePosition->Checked = true;

	TB_SubMapID->Text = Item->SubItems->Item[1]->Text;
	TB_SubMapName->Text = Item->SubItems->Item[2]->Text;
}
//檔案讀取更新地圖資料
System::Void ZoneForm::UpdateMapInfoClick(System::Object*  sender, System::EventArgs*  e) 
{
	if( CheckModifyAndCheckOut() == false )
		return;

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

	CRuFusion_Trekker*	Trekker = NULL;
	CRuArrayList<CRuWorld_ZoneDescriptor *> zoneDescriptorsOut;

	char* RootPath = Global::GetIniString("ResourcePath");
	// Setup resource manager's root path
	g_ruResourceManager->SetRootDirectory( RootPath );
	// Initialize collision system
	char* filePath = CombineFilePath( RootPath , DotNetStr::StringToChar( TB_MapFile->Text ).c_str() );
	try
	{
		Trekker = ruNEW CRuFusion_Trekker( filePath );
	}
	catch( ... )
	{
		return;
	}

	Trekker->EnumerateSubZones( DotNetStr::StringToInt( TB_MapID->Text ) , &zoneDescriptorsOut);

	LV_MapInfo->Items->Clear();
	for( int i = 0 ; i < zoneDescriptorsOut.Count() ; i++ )	
	{		
		System::String* StrList[] = new System::String*[3];		

		StrList[0] = "否";

		StrList[1] = String::Format( "{0}" , __box( zoneDescriptorsOut[i]->GetZoneID() ) );
		StrList[2] = zoneDescriptorsOut[i]->GetName();

		ListViewItem* NewListViewItem = new ListViewItem( StrList );
		LV_MapInfo->Items->Add( NewListViewItem );
	}

	ruSAFE_RELEASE( Trekker );
	FixMapInfoList( );
}
void	ZoneForm::FixMapInfoList( )
{
	DB_GameObjDbStruct __pin * TempObj = &_ObjDB;
	GameObjectZoneStruct& Zone = TempObj->GameData.Zone;

	int Count = __min( LV_MapInfo->Items->Count , 40 );
	Zone.MapCount = Count;

	if( Count == 0 )
	{
		LV_MapInfo->Items->Clear();
		return;
	}

	for( int i = 0 ; i < Count ; i++ )
	{
		ListViewItem* Item = LV_MapInfo->Items->Item[i];
		if( String::Compare( Item->SubItems->Item[0]->Text , "可" ) == 0 )
			Zone.MapInfoList[i].IsEnabledSave = true;
		else
			Zone.MapInfoList[i].IsEnabledSave = false;
		Zone.MapInfoList[i].MapID = DotNetStr::StringToInt( Item->SubItems->Item[1]->Text );
		strcpy( Zone.MapInfoList[i].MapName, DotNetStr::StringToChar( Item->SubItems->Item[2]->Text ).c_str() );
	}

	//資料排序
	for( int i = Count - 1 ; i > 0 ; i-- )
	{
		for( int j = 1 ; j <= i ; j++ )
		{
			if( Zone.MapInfoList[ j-1 ].MapID > Zone.MapInfoList[j].MapID )
			{
				MapInfoStruct Temp = Zone.MapInfoList[ j-1 ];
				Zone.MapInfoList[ j-1 ] = Zone.MapInfoList[ j ];
				Zone.MapInfoList[ j ] = Temp;
			}
		}
	}
	//資料重設
	for( int i = 0 ; i < Count ; i++ )
	{
		ListViewItem* Item = LV_MapInfo->Items->Item[i];

		if( Zone.MapInfoList[i].IsEnabledSave == true )
			Item->SubItems->Item[0]->Text = "可";
		else
			Item->SubItems->Item[0]->Text = "否";

		Item->SubItems->Item[1]->Text = String::Format( "{0}" , __box( Zone.MapInfoList[i].MapID ) );
		Item->SubItems->Item[2]->Text = Zone.MapInfoList[i].MapName;
	}

}