#include "ImageForm.h"
#include "ViewForm.h"
using namespace ObjDataEdit;

//######################################################################################################
//主要需要繼承的涵式
ReaderClass< DB_GameObjDbStruct >* ImageForm::ObjReader( )
{
    return St_ObjReader( );
}
//////////////////////////////////////////////////////////////////////////
ReaderClass< DB_GameObjDbStruct >* ImageForm::St_ObjReader( )
{
	return GameObjDbStructEx::ObjReader_Image( );
}
//////////////////////////////////////////////////////////////////////////
void	ImageForm::OnCreateFrom()
{
	CB_GatherParticalPoint1->Items->Clear();
	CB_GatherParticalPoint2->Items->Clear();
	CB_GatherParticalPoint3->Items->Clear();
	CB_ShootPoint->Items->Clear();
//	CB_BeginPoint->Items->Clear();
	CB_FlyPoint->Items->Clear();
	CB_TargetPoint->Items->Clear();
	CB_ExtenPoint->Items->Clear();
	CB_BuffPoint->Items->Clear();

	for( unsigned int i = 0 ; i < Global::PointEnumName.size() ; i++ )
	{
		String* Str =  Global::PointEnumName[i].c_str();

		CB_GatherParticalPoint1->Items->Add(Str);
		CB_GatherParticalPoint2->Items->Add(Str);
		CB_GatherParticalPoint3->Items->Add(Str);
		CB_ShootPoint->Items->Add(Str);
//		CB_BeginPoint->Items->Add(Str);
		CB_FlyPoint->Items->Add(Str);
		CB_TargetPoint->Items->Add(Str);
		CB_ExtenPoint->Items->Add(Str);
		CB_BuffPoint->Items->Add(Str);
	}

	CB_GatherMotion->Items->Clear();
	CB_ShootMotion->Items->Clear();


	for( unsigned int i = 0 ; i < Global::MotionEnumName.size() ; i++ )
	{
		String* Str =  Global::MotionEnumName[i].c_str();

		CB_GatherMotion->Items->Add(Str);
		CB_ShootMotion->Items->Add(Str);
	}

	//////////////////////////////////////////////////////////////////////////
	//設定 裝備combobox資料 到ViewForm
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Clothes    );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Gloves     );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Shoulder   );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Back       );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Belt       );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Shoes      );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Pants      );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Head       );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Hair		);
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Face       );

	SqlDataReader* myReader;

	// 幣值設定
	{
		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_SmallIconType->Items->Clear();

		for( vector< std::string >::iterator it = vecPriceType.begin(); it != vecPriceType.end(); it++ )
		{
			String*		sContent		= new String( (*it).c_str() );
			CB_SmallIconType->Items->Add( sContent );
		}
	}
	CB_Sound->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_ActWorld->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_ActField->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_GatherParticalName1->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_GatherParticalName2->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_GatherParticalName3->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_ShootACT->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_FlyACT->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_ExplodeACT->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_ExtendACT->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_BuffACT->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
	CB_BuffACTEx->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
}
void    ImageForm::OnActive()
{
    static bool FirstTime = true;

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

	BaseForm::OnActive();

	FirstTime = false;
/*
    CB_GatherParticalPoint1->Items->Clear();
    CB_GatherParticalPoint2->Items->Clear();
	CB_GatherParticalPoint3->Items->Clear();
    CB_FlyPoint->Items->Clear();
    CB_TargetPoint->Items->Clear();
	CB_ExtenPoint->Items->Clear();
	CB_BuffPoint->Items->Clear();

    for( unsigned int i = 0 ; i < Global::PointEnumName.size() ; i++ )
    {
        String* Str =  Global::PointEnumName[i].c_str();

        CB_GatherParticalPoint1->Items->Add(Str);
        CB_GatherParticalPoint2->Items->Add(Str);
		CB_GatherParticalPoint3->Items->Add(Str);
        CB_FlyPoint->Items->Add(Str);
        CB_TargetPoint->Items->Add(Str);
		CB_ExtenPoint->Items->Add(Str);
		CB_BuffPoint->Items->Add(Str);
    }

    CB_GatherMotion->Items->Clear();
    CB_ShootMotion->Items->Clear();
	

    for( unsigned int i = 0 ; i < Global::MotionEnumName.size() ; i++ )
    {
        String* Str =  Global::MotionEnumName[i].c_str();

        CB_GatherMotion->Items->Add(Str);
        CB_ShootMotion->Items->Add(Str);
    }


	//////////////////////////////////////////////////////////////////////////
	//設定 裝備combobox資料 到ViewForm

	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Head       );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Gloves     );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Shoes      );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Clothes    );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Pants      );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Back       );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Belt       );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Shoulder   );

	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Face       );
	ViewForm::g_ViewForm->_ComboBoxEQList_Image->Add( CB_Hair   );
*/
}

//////////////////////////////////////////////////////////////////////////
void    ImageForm::OnSelectObj( bool LockOk )
{
    ImageID_Base->Text = String::Format( "{0}" , __box( _ObjGUID ) );
    _ObjDB.GameData.Classification = EM_ObjectClass_Image;
}
//////////////////////////////////////////////////////////////////////////
//輸出時額外設定
void    ImageForm::OnOutputObjSetting( DB_GameObjDbStruct* TempObj )
{
    TempObj->GameData.Classification = EM_ObjectClass_Image;
}
//////////////////////////////////////////////////////////////////////////
void    ImageForm::OnTimeProc()
{
	
}
//////////////////////////////////////////////////////////////////////////
int     ImageForm::ObjSize(  )
{
    //int Size = sizeof( GameObjDbStruct );
    //Size = Size & 0xfffffC;
	GameObjDbStruct *P = NULL;
	int Size = sizeof( GameObjDbStruct ) - sizeof( P->Temp ) +  sizeof( P->Image ) + 3;
	Size = Size & 0xfffffC;

    return Size;
}
//////////////////////////////////////////////////////////////////////////
void    ImageForm::OnValueChange()
{
}
//######################################################################################################
GameObjDbStruct* ImageForm::St_GetImageObj( int ObjGUID , SqlCommand*  sqlCommand1 )
{
    static DB_GameObjDbStruct ObjDB;
    DB_GameObjDbStruct __pin *TempObj = &ObjDB;


    bool    Ret = false;
    char    Buf[256];
    int     i;

    ReaderClass< DB_GameObjDbStruct >*      Reader = St_ObjReader( );
    map<string,ReadInfoStruct>*             ReadInfo ;
    map<string,ReadInfoStruct>::iterator    Iter;

    ReadInfo = Reader->ReadInfo();



    sprintf( Buf , "WHERE GUID = %d" , ObjGUID );
    CreateDBCmdClass<DB_GameObjDbStruct> CreateCMD( Reader  , "ImageObjectDB");
    sqlCommand1->CommandText = CreateCMD.GetSelectStr( Buf ).c_str();
    SqlDataReader* myReader = sqlCommand1->ExecuteReader( );

    TempObj->Init( );
    if( myReader->Read( ) )
    {
        Ret = true;
        for(    Iter  = ReadInfo->begin() , i = 0 ; 
            Iter != ReadInfo->end() ; 
            Iter++ , i++ )
        {
            ReadInfoStruct *IDData = &(Iter->second);

            switch(IDData->Type)
            {
            case ENUM_Type_FloatPoint:
                {
                    *((float*)(IDData->Point + int(TempObj))) = myReader->GetFloat( i );
                }
                break;

            case ENUM_Type_IntPoint:
                {
                    *((int*)(IDData->Point + int(TempObj))) = myReader->GetInt32( i );
                }
                break;

            case ENUM_Type_ShortPoint:
                {
                    *((short*)(IDData->Point + int(TempObj))) = myReader->GetInt16( i );
                }                
                break;
            case ENUM_Type_CharString:
			case ENUM_Type_TextData:
                {
                    String* Str = myReader->GetString( i ) ;
                    if( Str->Length != 0)
                    {
                        char utf8 __gc[] = Encoding::Default->GetBytes( Str );
                        char __pin *CharStr = &utf8[0];
                        strcpy( (char*)(IDData->Point + int(TempObj)) , CharStr );
                    }
                    else
                    {
                        strcpy( (char*)(IDData->Point + int(TempObj)) , "" );
                    }

                }    
                break;
			case ENUM_Type_CharStringPoint:
			case ENUM_Type_TextDataPoint:
				{
					//不處理
					i--;
				}
				break;
            case ENUM_Type_TimeCharString:
                {
                    DateTime TempDate = myReader->GetDateTime( i );
                    String* Tt= String::Format( "{0}:{1}:{2}" 
                        , __box( TempDate.get_Hour() )
                        , __box( TempDate.get_Minute() )
                        , __box( TempDate.get_Second() ) );

                    Tt =  String::Format( "{0} {1} " , TempDate.ToString( "d" ) , Tt );
                    char utf8 __gc[] = Encoding::Default->GetBytes( Tt );
                    char __pin *Str = &utf8[0];
                    strcpy( (char*)(IDData->Point + int(TempObj)) , Str );

                }
                break;
            }
        }   
    }
    myReader->Close( );
    TempObj->GameData.Classification = EM_ObjectClass_Image;

    if( Ret == true ) 
        return &TempObj->GameData;
    return NULL;
}

System::Void ImageForm::OnImageSettingChange_GetValueChange(System::Object*  sender, System::EventArgs*  e)
{
	GetValue_ValueChanged( sender , e );
	ViewForm::g_ViewForm->SetValueChange();
}


System::Void ImageForm::OnImageSettingChange_GetValueTextChange(System::Object*  sender, System::EventArgs*  e)
{
	GetIntValue_TextChanged( sender , e );
	ViewForm::g_ViewForm->SetValueChange();
}


System::Void ImageForm::OnEqTextChange(System::Object*  sender, System::EventArgs*  e)
{
	GetStr_TextChanged( sender , e );
	ViewForm::g_ViewForm->SetValueChange();
}

System::Void ImageForm::SelectColorClick(System::Object*  sender, System::EventArgs*  e)
{
	if( colorDialog1->ShowDialog() == ::DialogResult::OK )
	{
		unsigned ColorValue = colorDialog1->Color.A * 0x1000000 + colorDialog1->Color.R * 0x10000 + colorDialog1->Color.G * 0x100 + colorDialog1->Color.B;
		TextBox* tBox = static_cast<TextBox*>(sender);
		tBox->Text = String::Format( "0x{0:x}" , __box( ColorValue ) );
	};
}
//System::Void ImageForm::LoadFolderItem(System::Object*  sender, System::EventArgs*  e)
//{
//	ComboBox* senderBox = static_cast<ComboBox*>(sender);
//	if (senderBox->Items->Count == 0 &&
//		senderBox->Text->Length == 0)
//	{
//		return;
//	}
//	char* ResourcePath = Global::GetIniString("ResourcePath");
//	String* PathString = new String(ResourcePath);
//
//	if (String::Compare(PathString->Substring(PathString->Length - 1), "\\") != 0)
//	{
//		PathString = String::Format("{0}\\", PathString);
//	}
//	String* OrgText = senderBox->Text;
//	String* fileLoc = String::Format("{0}{1}", PathString, OrgText);
//	fileLoc = fileLoc->Replace("/", "\\");
//	fileLoc = fileLoc->Replace("\\\\", "\\");
//	fileLoc = fileLoc->Substring(0, fileLoc->LastIndexOf("\\") + 1);
//	if (System::IO::Directory::Exists(fileLoc))
//	{
//		String* Dirfiles __gc[] = System::IO::Directory::GetFiles(fileLoc);
//		senderBox->Items->Clear();
//		for (int i = 0 ; i < Dirfiles->Length ; i ++)
//		{
//			String* tempfileName = Dirfiles[i]->Replace(PathString, "");
//			senderBox->Items->Add(tempfileName);
//		}
//		senderBox->TextChanged -= new System::EventHandler(this, &ImageForm::LoadFolderItem);
//		senderBox->Text = OrgText;
//		senderBox->Select(senderBox->Text->Length, 0);
//		senderBox->TextChanged += new System::EventHandler(this, &ImageForm::LoadFolderItem);
//	}
//}