/**
 *	Window library
 *
 *	Create custom window. Bundled with default styles : "SM"
 */
#Const	Version		"2014-11-21"
#Const	ScriptName	"Window.Script.txt"

#Include "MathLib" as ML

// ---------------------------------- //
// Globales
// ---------------------------------- //
declare Vec2[Text][Text] G_LibWindow_Styles;	///< Stock the loaded styles

// ---------------------------------- //
// Functions
// ---------------------------------- //
// ---------------------------------- //
// Public
// ---------------------------------- //
// ---------------------------------- //
/**	Return the version number of the script
 *
 *	@return   The version number of the script
 */
Text GetScriptVersion() {
	return Version;
}

// ---------------------------------- //
/**	Return the name of the script
 *
 *	@return		The name of the script
 */
Text GetScriptName() {
	return ScriptName;
}

// ---------------------------------- //
/** Create a new style
 *
 *	@param	_Name		The name of the style
 *	@param	_Images		An array with the images and their dimensions in this order
 *						[
 *							"TopLeft.png" => <10., 10.>,
 *							"Top.png" => <10., 10.>,
 *							"TopRight.png" => <10., 10.>,
 *							"Right.png" => <10., 10.>,
 *							"BottomRight.png" => <10., 10.>,
 *							"Bottom.png" => <10., 10.>,
 *							"BottomLeft.png" => <10., 10.>,
 *							"Left.png" => <10., 10.>,
 *							"Center.png" => <10., 10.>
 *						]
 */
Void CreateStyle(Text _Name, Vec2[Text] _Images) {
	if (_Images.count != 9) return;
	
	G_LibWindow_Styles[_Name] = _Images;
}

// ---------------------------------- //
/** Delete a style
 *
 *	@param	_Name		The name of the style to delete
 */
Void DestroyStyle(Text _Name) {
	declare Removed = G_LibWindow_Styles.removekey(_Name);
}

// ---------------------------------- //
/** Create a new window
 *
 *	@param	_Style			The style to use for the window
 *	@param	_Size			The size of the window
 *	@param	_BorderRatio	Size ratio of the border comapred to the original image
 *
 *	@return				The manialink of the window
 */
Text Create(Text _Style, Vec2 _WindowSize, Real _BorderRatio) {
	if (!G_LibWindow_Styles.existskey(_Style)) return "";
	declare Style = G_LibWindow_Styles[_Style];
	
	declare Size = Vec2[Integer];
	declare Path = Text[Integer];
	declare Pos = Vec2[Integer];
	declare I = 0;
	declare Ratio = 1.;
	declare Max = 0.;
	
	foreach (StylePath => StyleSize in Style) {
		declare TmpMax = ML::Max(StyleSize.X, StyleSize.Y);
		if (TmpMax > Max) Max = TmpMax;
		
		Size[I] = StyleSize;
		Path[I] = StylePath;
		
		I += 1;
		if (I >= 9) break;
	}
	
	// Outer sizes
	if (Max != 0) Ratio = (Max * _BorderRatio) / Max;
	for (I, 0, 8) Size[I] *= Ratio;
	
	// Inner sizes
	declare SizeX = _WindowSize.X - Size[0].X - Size[2].X;
	declare SizeY = _WindowSize.Y - Size[2].Y - Size[4].Y;
	if (SizeX > 0.) {
		Size[1].X = SizeX;
		Size[5].X = SizeX;
	}
	if (SizeY > 0.) {
		Size[3].Y = SizeY;
		Size[7].Y = SizeY;
	}
	
	// Center size
	Size[8] = <Size[1].X, Size[3].Y>;
	
	// Set positions
	Pos[0] = <0., 0.>;
	Pos[1] = <Size[0].X, 0.>;
	Pos[2] = <Pos[1].X + Size[1].X, 0.>;
	Pos[3] = <Pos[2].X, -Size[2].Y>;
	Pos[4] = <Pos[2].X, Pos[3].Y - Size[3].Y>;
	Pos[5] = <Pos[1].X, Pos[4].Y>;
	Pos[6] = <0., Pos[4].Y>;
	Pos[7] = <0., Pos[3].Y>;
	Pos[8] = <Pos[1].X, Pos[3].Y>;
	
	// Create manialink
	declare Result = "";
	for (I, 0, 8) {
		Result ^= """<quad posn="{{{Pos[I].X}}} {{{Pos[I].Y}}}" sizen="{{{Size[I].X}}} {{{Size[I].Y}}}" image="{{{Path[I]}}}" />""";
	}
	
	return Result;
}

// ---------------------------------- //
/// Unload the library
Void Unload() {
	G_LibWindow_Styles.clear();
}

// ---------------------------------- //
/// Load the library
Void Load() {
	Unload();
	
	// Create default styles
	declare Path = "file://Media/Manialinks/Shootmania/Window/";
	CreateStyle("SM",
		[
			Path^"TopLeft.png" => <88., 85.>,
			Path^"Top.png" => <1., 85.>,
			Path^"TopRight.png" => <88., 85.>,
			Path^"Right.png" => <88., 1.>,
			Path^"BottomRight.png" => <88., 403.>,
			Path^"Bottom.png" => <1., 403.>,
			Path^"BottomLeft.png" => <88., 403.>,
			Path^"Left.png" => <88., 1.>,
			Path^"Center.png" => <1., 1.>
		]
	);
}