/***************************************************************/
//
//
//		DirectX	[ texturemanager.cpp ]
//
//											Author	kazuki tanaka
//											Date	2017 5/17
/*---------------------------------------------------------------
■Update : 2017/5/17
			texturemanager.cppの作成

/*---------------------------------------------------------------
	Include File
---------------------------------------------------------------*/

#define _CRT_SECURE_NO_WARNINGS

#include "texture_manager.h"

#include "texturedx9_interface.h"
#include "texturegl_interface.h"

#include "renderer.h"
#include "game_manager.h"

/*---------------------------------------------------------------
	Macro Difinition
---------------------------------------------------------------*/


/*---------------------------------------------------------------
	Const Number Difinition
---------------------------------------------------------------*/


/*----------------------------------------------------------------
	static member variable Initialize 
----------------------------------------------------------------*/

TextureManager::MANAGE_DATA TextureManager::data[ INTERFACE_MAX ] = { };



// TextureManager Constructor
TextureManager::TextureManager( )
{

	// Menber Clear Process

}

// TextureManager Destructor
TextureManager::~TextureManager( )
{

	// Do Nothing!!

}

// TextureManager Create Instance 
TextureManager* TextureManager::Create( void )
{

	// Create Class Instance
	return new TextureManager;

}

// TextureManager LoadFromManager
TextureInterface* TextureManager::LoadFromManager( const char* FILE_NAME )
{

	TextureInterface* pTexture = nullptr;
	if( !(pTexture = TextureManager::Bind( "data\\texture\\field.jpg" )) ){
		TextureManager::Load( "data\\texture\\field.jpg" );
		pTexture = TextureManager::Bind( "data\\texture\\field.jpg" );
	}

	return pTexture;
}

// TextureManager Directory Search
void TextureManager::SearchLoadDirectory( void )
{

	char path[256]    = {}; 

	// Get < Client Absolute Path >
	GetModuleFileName( NULL, path, MAX_PATH );

	// Synthesys Command Order
	char command[256] = {};
	char drive[256] = {};
	char dir[256] = {};
	char filename[256] = {};
	char extension[256] = {};
	char search[256] = {"data\\texture"};

	_splitpath( path, drive, dir, filename, extension );

	sprintf( filename, "%s.txt", filename );

	// Create output file
	sprintf( command, "dir %s%s%s >> %s", drive, dir, search, filename );
	system( command );

	// File Process
	FILE* pfile;

	if( !(pfile = fopen( filename, "rt" )) ){
		fclose(pfile);
		//return nullptr;

	}else{

		// ファイルサイズ取得
		fseek( pfile, 0, SEEK_END );
		unsigned int size = ftell( pfile );						// ファイルサイズ取得
		fseek( pfile, 0, SEEK_SET );

		// バッファ確保
		char* buf = new char[ size ]; 

		// ファイル読み込み
		fread( buf, size, 1, pfile );
		fclose(pfile);
		delete[] buf;
		//return buf;
	}


	// Delete output file
	sprintf( command, "del /q  %s", filename );
	system( command );


}

// TextureManager Load Texture
bool TextureManager::Load( const char* FILE_NAME )
{
	
	for( int index = 0; index < INTERFACE_MAX; index++ ){
		
		if( !data[index].texture ){

			// Confirm Platform
			Renderer::TYPE type = GameManager::PlatformInfo( );
			
			switch( type ){
			
			case  Renderer::RENDERER_DX9:

				data[index].texture = TextureDX9Interface::Create( );
				if( data[index].texture->Load( FILE_NAME ) ){
					data[index].ID = GetID( FILE_NAME );
				}
				return true;
				break;

			case  Renderer::RENDERER_GL:

				data[index].texture = TextureGLInterface::Create( );
				if( data[index].texture->Load( FILE_NAME ) ){
					data[index].ID = GetID( FILE_NAME );
				}
				return true;
				break;

			default:
				break;
			}
		}
	}
	
	return false;
	
}

// TextureManager Bind Texture
TextureInterface* TextureManager::Bind( const char* FILE_NAME )
{
	
	// モデルデータを取得してバインド
	for( int index = 0; index < INTERFACE_MAX; index++ ){
		
		if( GetID( FILE_NAME ) == data[index].ID ){
			
			return data[index].texture;
		}
	}
	return 0;
}
 
// TextureManager UnloadAll
void TextureManager::UnloadAll( void )
{
	
	for( int index = 0; index < INTERFACE_MAX; index++ ){
		
		// データが入っていれば
		if( data[index].texture ){
			
			data[index].texture->Release( );
			SAFE_DELETE( data[index].texture )
		}
	}

}

// TextureManager Create ID number
__int64 TextureManager::GetID( const char* FILE_NAME )
{

	__int64 data = 0;

	// Divide File Name
	char filename [256] = { };		// ファイルネーム
	char extension[256] = { };		// 拡張子
	char loadFile [256] = { };		// ロードファイルネーム

	// Split Path
	_splitpath( FILE_NAME, NULL, NULL, filename, extension );
	
	// Synthesis String < Load File Name >
	sprintf( loadFile, "%s%s", filename , extension );

	for( int index = 0; index < strlen(loadFile); index++ ){
		data = atoi( &loadFile[index] ); 
		data *= 10;
	}

	return  data;

}






