#include "CampForm.h"
#define CompComboStringCount 4

using namespace ObjDataEdit;
//######################################################################################################
//主要需要繼承的涵式
ReaderClass< DB_GameObjDbStruct >* CampForm::ObjReader( )
{
	return  GameObjDbStructEx::ObjReader_Camp( );
}
void    CampForm::OnActive()
{
	BaseForm::OnActive();
}
void    CampForm::OnSelectObj( bool LockOk )
{
    _ObjDB.GameData.Classification = EM_ObjectClass_Camp;

	//資料更新
	GameCampStatuteStruct __pin *Camp = &_ObjDB.GameData.Camp;		//陣營資訊


	dataGridView1->Rows->Clear();
	dataGridView1->Columns->Clear();

	dataGridView1->Columns->Add( Column1 );

	for( int i = 0 ; i < 15 ; i++ )
	{
		if( strlen( Camp->CampName[i] ) <=1 )		
			break;
		String* Str = Camp->CampName[i];

		dataGridView1->Rows->Add( 1 );
		dataGridView1->Rows->Item[ i ]->Cells->Item[0]->Value = Str;
	}


	
	_IsNeedUpdateCol = true;
	_IsNeedUpdateCell = true;

	dataGridView1->Enabled = LockOk;

}
void    CampForm::OnOutputObjSetting( DB_GameObjDbStruct* TempObj )
{
    TempObj->GameData.Classification = EM_ObjectClass_Camp;

	for( int i = 0 ; i < 15 ; i++ )
		for( int j = 0 ; j < 15 ; j++ )
		{
			TempObj->GameData.Camp.CampInfo[i][j] -= '0';

			if(		TempObj->GameData.Camp.CampInfo[i][j] < 0 
				||	TempObj->GameData.Camp.CampInfo[i][j] >= CompComboStringCount )
				TempObj->GameData.Camp.CampInfo[i][j] = 0;
		}
		
}
void    CampForm::OnTimeProc()
{
	GameCampStatuteStruct __pin *Camp = &_ObjDB.GameData.Camp;

	if( _IsNeedUpdateCol == true )
	{
		_IsNeedUpdateCol = false;

		int ColCount = dataGridView1->ColumnCount;
		int RowCount = dataGridView1->RowCount;

		if( RowCount > 15 )
			RowCount = 15;


		if( ColCount > RowCount )
		{
			for( int i = RowCount ; i < ColCount ; i++ )
			{
				dataGridView1->Columns->RemoveAt( RowCount );
			}
		}
		else if( ColCount < RowCount )
		{

			for( int i = ColCount ; i < RowCount ; i++ )
			{

				DataGridViewComboBoxColumn * Col = static_cast< DataGridViewComboBoxColumn * >( _ColList[i-1] );
				String* Str = String::Format( "{0}" , dataGridView1->Rows->Item[i-1]->Cells->Item[0]->Value );
				Col->HeaderText = Str;
				dataGridView1->Columns->Add( Col );
			}
		}

		for( int i = 1 ; i < dataGridView1->ColumnCount ; i++  )
		{
//			DataGridViewComboBoxColumn * Col = static_cast< DataGridViewComboBoxColumn * >( dataGridView1->Columns[i] );
			String* Str = Camp->CampName[ i-1 ];
			dataGridView1->Columns->Item[i]->HeaderText = String::Format( "{0}" , Str );
		}


	}
	
	if( _IsNeedUpdateCell == true )
	{
		_IsNeedUpdateCell = false;
		int ColCount = dataGridView1->ColumnCount;
		int RowCount = dataGridView1->RowCount;

		for( int i = 0 ; i < RowCount-1 ; i++ )
		{
			for( int j = 1 ; j < ColCount ; j++ )
			{
				int SelectID = Camp->CampInfo[i][j-1] - '0';
				if( SelectID < 0 || SelectID >= CompComboStringCount )
				{
					SelectID = 0;
					Camp->CampInfo[i][j-1] = 0;
				}
				String* Str = _ComboBoxStr[ SelectID  ];
				dataGridView1->Rows->Item[i]->Cells->Item[j]->Value = Str;
			}
		}
	}

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

    return Size;
}

//######################################################################################################
void	CampForm::Initial( )
{
	_IsNeedUpdateCol = false;
	_IsNeedUpdateCell = false;

	_ComboBoxStr = new String*[CompComboStringCount];
	_ComboBoxStr[0] = S"同盟";
	_ComboBoxStr[1] = S"友善";
	_ComboBoxStr[2] = S"中立";
	_ComboBoxStr[3] = S"敵人";


	_ColList = new DataGridViewComboBoxColumn*[15];
	for( int i = 0 ; i < 15 ; i++ )
	{
		DataGridViewComboBoxColumn* Col = _ColList[i] = new DataGridViewComboBoxColumn();
		Col->DisplayStyle = System::Windows::Forms::DataGridViewComboBoxDisplayStyle::Nothing;
		Col->Items->AddRange( _ComboBoxStr );
		Col->Resizable = System::Windows::Forms::DataGridViewTriState::True;
		Col->Width = 55;
	}
	_ObjDB.IsModify = false;
}
void	CampForm::Release( )
{
}


System::Void CampForm::dataGridView1_CellValueChanged(System::Object*  sender, System::Windows::Forms::DataGridViewCellEventArgs*  e)
{
	GameCampStatuteStruct __pin *Camp = &_ObjDB.GameData.Camp;

	int Row = e->RowIndex;
	int	Col = e->ColumnIndex;
	int ColCount = dataGridView1->ColumnCount;
	int RowCount = dataGridView1->RowCount;

	if( Row < 0 || Row > 15 )
		return;

	String* Str = static_cast<String*>(dataGridView1->Rows->Item[Row]->Cells->Item[Col]->Value);

	if( Col != 0 )
	{
		int		SelectID = -1;
		for( int i = 0 ; i < CompComboStringCount; i++ )
		{
			if( String::Compare( Str , _ComboBoxStr[i] ) == 0  )
			{
				SelectID = i;
				break;
			}
		}

		if( SelectID == -1 )
		{
			SelectID = 0;
		}

		if( Camp->CampInfo[Row][Col-1] != '0' + SelectID )
		{
			Camp->CampInfo[Row][Col-1] = '0' + SelectID;
			_ObjDB.IsModify = true;
		}
		
		
	}
	else
	{

		if( strcmp( Camp->CampName[Row] , DotNetStr::StringToChar( Str ).c_str() ) != 0 )
		{
			strncpy( Camp->CampName[Row] , DotNetStr::StringToChar( Str ).c_str() , sizeof( Camp->CampName[Row] ) );
			_ObjDB.IsModify = true;

			_IsNeedUpdateCol = true;
			_IsNeedUpdateCell = true;
		}
	}


}