#pragma once
#include<DxLib.h>
#include<vector>
#define NULL_HANDLE -1

namespace DXFW
{
/// <summary>
/// 説明
/// </summary>
enum class TransMode
{
	TransNone  =	DX_MASKTRANS_NONE,
	TransBlack =	DX_MASKTRANS_BLACK,
	TransWhite =	DX_MASKTRANS_WHITE		
};
	
///<summary>
///ステンシルバッファのマスククラス
///</summary>
///<example>
///[サンプルコード:未完成]<BR>
///サンプル説明<BR>
///<code>
///int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow )<BR>
///{<BR>
///	if( !Dx::Initialise() ) return -1;<BR>
///	Dx::End();<BR>
///	return 0;<BR>
///}<BR>
///</code>	
///</example>
class Mask
{
friend class MaskPack;

private:
	int handle;
	static bool isCreateScreen;
	static bool isUseScreen;
	unsigned char *data;
public:
	Mask(void)
	{
		this->handle = NULL_HANDLE;
	}

	Mask(const char *ファイル名)
	{
		handle = DxLib::LoadMask(ファイル名);
	}

	Mask(int w,int h)
	{
		this->handle = DxLib::MakeMask(w,h);
	}

	int Load(const char *ファイル名)
	{
		if( this->handle != NULL_HANDLE )DxLib::DeleteMask( this->handle );
		return handle = DxLib::LoadMask(ファイル名);
	}

	bool Release()
	{
		return !DxLib::DeleteMask( this->handle );
	}

	bool Draw(int x,int y,TransMode transMode)
	{
		return !DxLib::DrawMask( x , y , this->handle , (int)transMode );
	}

	bool DrawFill(int x1,int y1,int x2,int y2)
	{
		return !DxLib::DrawFillMask( x1 , y1 , x2 , y2 , this->handle );
	}

	bool FillScreen(bool isWhite)
	{
		return !DxLib::FillMaskScreen( isWhite );
	}

	int GetWidth()
	{
		int w,h;
		DxLib::GetMaskSize( &w , & h , this->handle );
		return w;
	}

	int GetHeight()
	{
		int w,h;
		DxLib::GetMaskSize( &w , & h , this->handle );
		return h;
	}

	bool SetData(int w , int h ,unsigned char *maskdata)
	{
		int w2,h2;
		DxLib::GetMaskSize( &w2 , & h2 , this->handle );
		if( w != w2 || h != h2 ) return false;
		return !DxLib::SetDataToMask( w , h , maskdata , this->handle );
	}

	static bool DrawDirect(int x,int y,int w,int h,unsigned char *maskData,TransMode transMode)
	{
		return !DxLib::DrawMaskToDirectData( x , y , w , h , maskData , (int)transMode);
	}

	static bool DrawFillDirect(int x1,int y1,int x2,int y2,int w,int h,unsigned char *maskData)
	{
		return !DxLib::DrawFillMaskToDirectData( x1 , y1 , x2 , y2 , w , h , maskData ); 
	}

	static bool CreateScreen()
	{
		Mask::isCreateScreen = true;
		Mask::isUseScreen = true;
		return !DxLib::CreateMaskScreen();
	}

	static bool DeleteScreen()
	{
		Mask::isCreateScreen = false;
		return !DxLib::DeleteMaskScreen();
	}

	static bool SetUseScreen(bool isUseble)
	{
		Mask::isUseScreen = isUseble;
		return !DxLib::SetUseMaskScreenFlag( isUseble );
	}

};
	
///<summary>
///[準備中]
///</summary>
///<example>
///[サンプルコード:未完成]<BR>
///サンプル説明<BR>
///<code>
///int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow )<BR>
///{<BR>
///	if( !Dx::Initialise() ) return -1;<BR>
///	Dx::End();<BR>
///	return 0;<BR>
///}<BR>
///</code>	
///</example>
class MaskPack
{
private:
	Mask *part;
	int size;
public:
	MaskPack();
		
	MaskPack::MaskPack(char *ファイル名,int allNum,int xNum,int yNum,int xSize,int ySize)
	{
		int *buf = new int[allNum];

		this->part = new Mask[allNum];

		DxLib::LoadDivMask(ファイル名,allNum,xNum,yNum,xSize,ySize,buf);

		for(int i=0;i<allNum;i++)
		{
			this->part[i].handle = buf[i];
		}

		size = allNum;

		delete [] buf;
	}

	bool Load(char *ファイル名,int allNum,int xNum,int yNum,int xSize,int ySize)
	{
		if( size > 0 ) delete [] this->part;
		int *buf = new int[allNum];

		this->part = new Mask[allNum];

		bool ok = !DxLib::LoadDivMask(ファイル名,allNum,xNum,yNum,xSize,ySize,buf);

		for(int i=0;i<allNum;i++)
		{
			this->part[i].handle = buf[i];
		}

		size = allNum;

		delete [] buf;

		return ok;
	}
		
	bool Release()
	{
		for(int i=0;i<size;i++)
		{
			DxLib::DeleteMask( this->part[i].handle );
		}
		if( size > 0 ) delete [] this->part;
		return true;
	}

	Mask& operator[](int index)
	{
		return part[index];
	}
};
}