//=============================================================
//				DIALOG SYSTEM
//=============================================================
let DirCurr = GetCurrentScriptDirectory();

//-----------------------------------------------
//	TEXT COLORS
//-----------------------------------------------
let GRAY = [155,155,155]; 
let RED = [255,64,64];  
let LIGHT_RED = [255,124,124];  
let PURPLE = [255,64,255];  
let LIGHT_PURPLE = [255,124,255];  
let BLUE = [64,64,255];  
let LIGHT_BLUE = [124,124,255];
let AQUA = [64,255,255];  
let LIGHT_AQUA = [124,255,255];  
let GREEN = [64,255,64];  
let LIGHT_GREEN = [124,255,124];  
let LIME = [128,255,64];  
let YELLOW = [255,255,64];  
let LIGHT_YELLOW = [255,255,124];  
let ORANGE = [255,128,64]; 
let WHITE = [255,255,255];
let BLACK = [0,0,0];

//-----------------------------------------------
//	CREATE DIALOG TEXTS
//-----------------------------------------------
function CreateText(text,size,bold,color) {
	let objTxt = ObjText_Create();
	ObjText_SetFontType(objTxt, "Lady Copra Alternate");
	ObjText_SetText(objTxt, text);
	ObjText_SetFontSize(objTxt, size);
	ObjText_SetFontBold(objTxt, true);
	ObjText_SetFontColorTop(objTxt, color[0], color[1], color[2]);
	ObjText_SetFontColorBottom(objTxt, color[0], color[1], color[2]);
	ObjText_SetFontBorderColor(objTxt, 16,16,16);
	ObjText_SetFontBorderType(objTxt, BORDER_NONE);
	//ObjText_SetLinePitch(objTxt,-4);
    //ObjText_SetFontBorderWidth(objTxt, round(size/10+1));
	Obj_SetRenderPriority(objTxt, 91);

	return objTxt;
}


//-----------------------------------------------
//	CREATE DIALOG BALLOONS
//-----------------------------------------------
function CreateBalloon(type,lines,xsize,align) {
/*PARAMETERS*/
//	type : "FLAT" / "SPIKE" / "WAVE"
//	lines: 1 or 2
//	xsize : will be factor of 16
/*------END-----*/

	let balloon_path = DirCurr ~ "/img/balloon.png";
	let recty;

	if(type=="FLAT"){recty = [1,224];}
	if(type=="SPIKE"){recty = [225,448];}
	if(type=="WAVE"){recty = [449,672];}
	if(lines==1){
		if(type=="FLAT"){recty = [673,848];}
		if(type=="SPIKE"){recty = [849,1024];}
		if(type=="WAVE"){recty = [1025,1200];}
	}

	let high = (recty[1]-recty[0]);
	let size = truncate(xsize/16);
	if(xsize/16 > size) {size+=1;}	
	size = max(size, 1);

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, balloon_path);
	Obj_SetRenderPriority(obj, 0.92);

	if(align=="LEFT") {
		ObjRender_SetX(obj,0);
		ObjSpriteList2D_SetSourceRect(obj, 1, recty[0], 95, recty[1]);
	} else if(align=="RIGHT") {
		ObjRender_SetX(obj,-48-(size+1)*16);
		ObjSpriteList2D_SetSourceRect(obj, 96, recty[0], 191, recty[1]);
		ObjRender_SetScaleX(obj, -1);
	}
	ObjSpriteList2D_SetDestRect(obj, 0, 0, 0.5*96, 0.5*high);
	ObjSpriteList2D_AddVertex(obj);
	ascent(i in 0..size) {
			if(align=="LEFT") {
				ObjRender_SetX(obj, 48+i*16);
			} else if(align=="RIGHT") {
				ObjRender_SetX(obj, -48-i*16);
				ObjRender_SetScaleX(obj, -1);
			}
			ObjSpriteList2D_SetSourceRect(obj, 193, recty[0], 255, recty[1]);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 0.5*64, 0.5*high);

			ObjSpriteList2D_AddVertex(obj);

	}
	if(align=="LEFT") {
		ObjRender_SetX(obj,48+(size+1)*16);
		ObjSpriteList2D_SetSourceRect(obj, 96, recty[0], 191, recty[1]);
	} else if(align=="RIGHT") {
		ObjRender_SetX(obj,0);
		ObjSpriteList2D_SetSourceRect(obj, 1, recty[0], 95, recty[1]);
		ObjRender_SetScaleX(obj, -1);
	}
	ObjSpriteList2D_SetDestRect(obj, 0, 0, 0.5*96, 0.5*high);
	ObjSpriteList2D_AddVertex(obj);
	ObjSpriteList2D_CloseVertex(obj);

	let arg = [obj, size];
	return arg;
}

//-----------------------------------------------
//	CREATE FULL DIALOG 
//-----------------------------------------------
function CreateDialog(x,y,text,size,bold,color,type,lines,align,auto) {
/*PARAMETERS*/
//	x : x position
//	y : y position
//	text : "text"
//	size : font size
//	bold : true or false
// 	color : GRAY / RED / LIGHT_RED /PURPLE / LIGHT_PURPLE / BLUE / LIGHT_BLUE / AQUA / LIGHT_AQUA / GREEN / LIGHT_GREEN / LIME / YELLOW / LIGHT_YELLOW / ORANGE / WHITE
//	type : "FLAT" / "SPIKE" / "WAVE"
//	lines : 1 or 2
//	align : "LEFT" or "RIGHT"
//	auto : frame wait before dialog auto pass. -1 for waiting until key push
/*------END-----*/
	let anti_skip = false;
	let alpha = 255;
	let objtext = CreateText(text,size,bold,color);
	let textsize  = ObjText_GetTotalWidth(objtext);
	let objballoon = CreateBalloon(type,lines,textsize-48,align);
	ObjRender_SetPosition(objballoon[0],x,y,0);
	let balloonsize = (objballoon[1]*32+192)/2;	
	let offsetx = (balloonsize-textsize)/2+8;
	if(align=="LEFT"){
		ObjRender_SetPosition(objtext,x+offsetx,y+41,0);
		//ObjRender_SetPosition(objtext,x+32,y+36,0);
	}else if(align=="RIGHT") {
		ObjRender_SetPosition(objtext,x-offsetx-ObjText_GetTotalWidth(objtext),y+41,0);
		//ObjRender_SetPosition(objtext,x+16-ObjText_GetTotalWidth(objtext)-50,y+36,0);
	}
	if(auto>-1){
		loop(auto){yield;};
	} else {
		loop(20){yield;}
		if(anti_skip==true){while(GetVirtualKeyState(VK_SHOT)==KEY_HOLD){yield;}}
		while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH&&GetVirtualKeyState(VK_SHOT)!=KEY_HOLD){yield;}
	}
	loop(6){
		alpha -= 255/6;
		ObjSpriteList2D_SetAlpha(objballoon[0], alpha);
		ObjRender_SetAlpha(objtext, alpha);
		yield;
	}
	Obj_Delete(objtext);
	Obj_Delete(objballoon[0]);
}


function ObjSpriteList2D_SetAlpha(obj, a){
	ascent(v in 0..ObjPrim_GetVertexCount(obj)){
		ObjPrim_SetVertexAlpha(obj, v, a);
	}
}


