#include "./Interpolation.dnh"

let portrait_player = [];
let portrait_enemy = [];

let dialogue_box;

let playerobjects = [];
let enemyobjects = [];

let playeroffsets = []; //coordinate offsets for player potraits
let enemyoffsets = []; //coordinate offsets for enemy potraits

let playerscales = []; //scales for player potraits
let enemyscales = []; //scales for enemy potraits

let dialogue_text = "";

let globalspeaker = 0; //0 is player, 1 is enemy

let speakerplayer = 0;
let speakerenemy = 0;

let nameObj; //name of the talking character
let boxObj; //text box
let titleObj; //title box of the text box

function SetPortraitNumber(numplayer, numenemy){
	ascent(i in 0 .. numplayer){
		let obj = CreatePortrait;
		ObjRender_SetAngleY(obj, 180);
		playerobjects = playerobjects ~ [obj];
		playerscales = playerscales ~ [1];
		playeroffsets = playeroffsets ~ [[0, 0]];
	}
	ascent(i in 0 .. numenemy){
		let obj = CreatePortrait;
		//ObjRender_SetAngleY(obj, 180);
		enemyobjects = enemyobjects ~ [obj];
		enemyscales = enemyscales ~ [1];
		enemyoffsets = enemyoffsets ~ [[0, 0]];
	}

	ascent(i in 0 .. numplayer){
		let path = "";
		portrait_player = portrait_player ~ [path];
	}
	ascent(i in 0 .. numenemy){
		let path = "";
		portrait_enemy = portrait_enemy ~ [path];
	}
}

function SetPortraitData(offsetsPlayer, offsetsEnemy, scalesPlayer, scalesEnemy){
	playeroffsets = offsetsPlayer;
	enemyoffsets = offsetsEnemy;
	playerscales = scalesPlayer;
	enemyscales = scalesEnemy;
}

task InitiateDialogueSequence{
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	SetCommonData("IsDialogueSequenceActive", true);

	//sets the scales for the portraits
	ascent(i in 0 .. length(playerobjects)){
		ObjRender_SetScaleXYZ(playerobjects[i], playerscales[i], playerscales[i], 0);
	}
	ascent(i in 0 .. length(enemyobjects)){
		ObjRender_SetScaleXYZ(enemyobjects[i], enemyscales[i], enemyscales[i], 0);
	}

	DialogueBox;
	nameObj = ObjText_Create();
}

sub EndDialogueSequence{
	SetCommonData("IsDialogueSequenceActive", false);
	Obj_Delete(nameObj);

	ascent(i in 0 .. length(playerobjects)){
		RemovePlayerPort(i);
	}
	ascent(i in 0 .. length(enemyobjects)){
		RemoveEnemyPort(i);
	}
	loop(30){yield;}

	ascent(i in 0 .. length(playerobjects)){
		Obj_Delete(playerobjects[i]);
	}
	ascent(i in 0 .. length(enemyobjects)){
		Obj_Delete(enemyobjects[i]);
	}
	playerobjects = [];
	enemyobjects = [];

	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
}

task DialogueBox{
	boxObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(boxObj, GetCurrentScriptDirectory ~ "../System/img/diabox.png");
	ObjSprite2D_SetSourceRect(boxObj, 0, 0, 512, 128); //actual size is 380 * 96
	ObjSprite2D_SetDestCenter(boxObj);
	ObjRender_SetAlpha(boxObj, 192);
	ObjRender_SetPosition(boxObj, GetStgFrameWidth/2, GetStgFrameHeight - 50, 0);
	Obj_SetRenderPriorityI(boxObj, 75);
	ObjRender_SetScaleXYZ(boxObj, 0, 1, 0);

	titleObj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(titleObj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(titleObj, GetCurrentScriptDirectory ~ "../IMG/Field.png");
	ObjPrim_SetVertexCount(titleObj, 4);
	ObjRender_SetAlpha(titleObj, 192);
	Obj_SetRenderPriorityI(titleObj, 75);

	let border = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(border, PRIMITIVE_LINESTRIP);
	ObjPrim_SetTexture(border, GetCurrentScriptDirectory ~ "../IMG/Field.png");
	ObjPrim_SetVertexCount(border, 7);
	Obj_SetRenderPriorityI(border, 76);



	let pointOffsets = [[-190, -48], [190, -48], [190, 48], [-190, 48], [-190, -48], [-190, -24], [190, -24]];
	let centerPoint = [GetStgFrameWidth/2, GetStgFrameHeight - 50];

	ascent(t in 0 .. 31){
		ascent(i in 0 .. 7){
			ObjPrim_SetVertexUVT(border, i, 0, 0);
			ObjPrim_SetVertexPosition(border, i, centerPoint[0] + pointOffsets[i][0] * (t/30), centerPoint[1] + pointOffsets[i][1], 0);
		}

		ObjPrim_SetVertexPosition(titleObj, 0, centerPoint[0] + pointOffsets[0][0] * (t/30), centerPoint[1] + pointOffsets[0][1], 0);
		ObjPrim_SetVertexPosition(titleObj, 1, centerPoint[0] + pointOffsets[5][0] * (t/30), centerPoint[1] + pointOffsets[5][1], 0);
		ObjPrim_SetVertexPosition(titleObj, 2, centerPoint[0] + pointOffsets[1][0] * (t/30), centerPoint[1] + pointOffsets[1][1], 0);
		ObjPrim_SetVertexPosition(titleObj, 3, centerPoint[0] + pointOffsets[6][0] * (t/30), centerPoint[1] + pointOffsets[6][1], 0);

		ObjRender_SetScaleXYZ(boxObj, linear(0, 1, t/30), 1, 0);
		yield;
	}

	while(GetCommonData("IsDialogueSequenceActive", false)){yield;}

	descent(t in 0 .. 31){
		ascent(i in 0 .. 7){
			ObjPrim_SetVertexUVT(border, i, 0, 0);
			ObjPrim_SetVertexPosition(border, i, centerPoint[0] + pointOffsets[i][0] * (t/30), centerPoint[1] + pointOffsets[i][1], 0);
		}

		ObjPrim_SetVertexPosition(titleObj, 0, centerPoint[0] + pointOffsets[0][0] * (t/30), centerPoint[1] + pointOffsets[0][1], 0);
		ObjPrim_SetVertexPosition(titleObj, 1, centerPoint[0] + pointOffsets[5][0] * (t/30), centerPoint[1] + pointOffsets[5][1], 0);
		ObjPrim_SetVertexPosition(titleObj, 2, centerPoint[0] + pointOffsets[1][0] * (t/30), centerPoint[1] + pointOffsets[1][1], 0);
		ObjPrim_SetVertexPosition(titleObj, 3, centerPoint[0] + pointOffsets[6][0] * (t/30), centerPoint[1] + pointOffsets[6][1], 0);

		ObjRender_SetScaleXYZ(boxObj, linear(0, 1, t/30), 1, 0);
		yield;
	}

	Obj_Delete(boxObj);
	Obj_Delete(titleObj);
	Obj_Delete(border);
}

function CreatePortrait{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, "");
	ObjSprite2D_SetSourceRect(obj, 0, 0, GetTextureWidth(""), GetTextureHeight(""));
	ObjSprite2D_SetDestCenter(obj);
	//ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0);
	Obj_SetRenderPriorityI(obj, 75);
	return obj;
}

function SetSpeaker(value){
	globalspeaker = value;
	if(globalspeaker ==  0){
		ascent(i in 0 .. length(playerobjects)){
			if(i == speakerplayer){
				moveDeceleration(playerobjects[i], GetStgFrameWidth/2 - 64 + playeroffsets[i][0], GetStgFrameHeight - 160 + playeroffsets[i][1], 30, false);
				ObjRender_SetAlpha(playerobjects[i], 256);
				Obj_SetRenderPriorityI(playerobjects[i], 75);
			}else{
				moveDeceleration(playerobjects[i], GetStgFrameWidth/2 - 192 + playeroffsets[i][0], GetStgFrameHeight - 64 + playeroffsets[i][1], 30, false);
				ObjRender_SetAlpha(playerobjects[i], 160);
				Obj_SetRenderPriorityI(playerobjects[i], 74);
			}
		}
		ascent(i in 0 .. length(enemyobjects)){
			moveDeceleration(enemyobjects[i], GetStgFrameWidth/2 + 192 + enemyoffsets[i][0], GetStgFrameHeight - 64 + enemyoffsets[i][1], 30, false);
			ObjRender_SetAlpha(enemyobjects[i], 160);
			Obj_SetRenderPriorityI(enemyobjects[i], 74);
		}
	}else{
		ascent(i in 0 .. length(enemyobjects)){
			if(i == speakerenemy){
				moveDeceleration(enemyobjects[i], GetStgFrameWidth/2 + 64 + enemyoffsets[i][0], GetStgFrameHeight - 160 + enemyoffsets[i][1], 30, false);
				ObjRender_SetAlpha(enemyobjects[i], 256);
				Obj_SetRenderPriorityI(enemyobjects[i], 75);
			}else{
				moveDeceleration(enemyobjects[i], GetStgFrameWidth/2 + 192 + enemyoffsets[i][0], GetStgFrameHeight - 64 + enemyoffsets[i][1], 30, false);
				ObjRender_SetAlpha(enemyobjects[i], 160);
				Obj_SetRenderPriorityI(enemyobjects[i], 74);
			}
		}

		ascent(i in 0 .. length(playerobjects)){
			moveDeceleration(playerobjects[i], GetStgFrameWidth/2 - 192 + playeroffsets[i][0], GetStgFrameHeight - 64 + playeroffsets[i][1], 30, false);
			ObjRender_SetAlpha(playerobjects[i], 160);
			Obj_SetRenderPriorityI(playerobjects[i], 74);
		}
	}
}

function SetPlayerSpeaker(value){
	speakerplayer = value;
}

function SetEnemySpeaker(value){
	speakerenemy = value;
}

function SetDialogueTextA1(text, color){ //event will advance only upon button press
	dialogue_text = text;

	let obj = ObjText_Create();
	ObjText_SetText(obj, dialogue_text);
	ObjText_SetFontType(obj, "MonoFonto-Regular");
	ObjText_SetFontSize(obj, 16);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, color[0], color[1], color[2]);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderWidth(obj, 1);
	ObjText_SetFontBorderColor(obj, 0, 0, 0);
	ObjText_SetLinePitch(obj, 10);
	ObjText_SetMaxWidth(obj, 375);
	Obj_SetRenderPriorityI(obj, 76);

	ObjRender_SetPosition(obj, 6, GetStgFrameHeight - 74, 0);

	loop(2){
		yield;
	}
	loop(3){
		if(GetVirtualKeyState(VK_CANCEL) == KEY_HOLD){break;}
		yield;
	}
	while(GetVirtualKeyState(VK_OK) != KEY_PUSH){
		if(GetVirtualKeyState(VK_CANCEL) == KEY_HOLD){break;}
		yield;
	}
	Obj_Delete(obj);
}

function SetDialogueTextA2(text, color, time){ //event will advance after set time
	dialogue_text = text;

	let obj = ObjText_Create();
	ObjText_SetText(obj, dialogue_text);
	ObjText_SetFontType(obj, "MonoFonto-Regular");
	ObjText_SetFontSize(obj, 16);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, color[0], color[1], color[2]);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderWidth(obj, 1);
	ObjText_SetFontBorderColor(obj, 0, 0, 0);
	ObjText_SetMaxWidth(obj, 345);
	Obj_SetRenderPriorityI(obj, 76);

	ObjRender_SetPosition(obj, 6, GetStgFrameHeight - 90, 0);

	loop(time){yield;}
	Obj_Delete(obj);
}

task SetSpeakerNameA1(text, color){ //event will advance only upon button press
	ObjText_SetText(nameObj, text);
	ObjText_SetFontType(nameObj, "MonoFonto-Regular");
	ObjText_SetFontSize(nameObj, 16);
	ObjText_SetFontColorTop(nameObj, 255, 255, 255);
	ObjText_SetFontColorBottom(nameObj, 255, 255, 255);
	ObjText_SetFontBorderType(nameObj, BORDER_FULL);
	ObjText_SetFontBorderWidth(nameObj, 1);
	ObjText_SetFontBorderColor(nameObj, 0, 0, 0);
	ObjText_SetMaxWidth(nameObj, 375);
	Obj_SetRenderPriorityI(nameObj, 76);
	ObjRender_SetPosition(nameObj, 6, GetStgFrameHeight - 96, 0);

	ObjRender_SetColor(titleObj, color[0], color[1], color[2]);
	ObjRender_SetColor(boxObj, color[0], color[1], color[2]);
}

function SetNewPlayerPort(img, i){ //moves in the player portrait from below
	portrait_player[i] = img;
	ObjRender_SetPosition(playerobjects[i], GetStgFrameWidth/2 - 64 + playeroffsets[i][0], GetStgFrameHeight + 384 + playeroffsets[i][1], 0); //start position

	ObjPrim_SetTexture(playerobjects[i], portrait_player[i]);
	ObjSprite2D_SetSourceRect(playerobjects[i], 0, 0, GetTextureWidth(portrait_player[i]), GetTextureHeight(portrait_player[i]));
	ObjSprite2D_SetDestCenter(playerobjects[i]);

	moveDeceleration(playerobjects[i], GetStgFrameWidth/2 - 64 + playeroffsets[i][0], GetStgFrameHeight - 160 + playeroffsets[i][1], 20, false);
}

function SetNewEnemyPort(img, i){ //moves in the enemy portrait from below
	portrait_enemy[i] = img;
	ObjRender_SetPosition(enemyobjects[i], GetStgFrameWidth/2 + 64 + enemyoffsets[i][0], GetStgFrameHeight + 384 + enemyoffsets[i][0], 0); //start position

	ObjPrim_SetTexture(enemyobjects[i], portrait_enemy[i]);
	ObjSprite2D_SetSourceRect(enemyobjects[i], 0, 0, GetTextureWidth(portrait_enemy[i]), GetTextureHeight(portrait_enemy[i]));
	ObjSprite2D_SetDestCenter(enemyobjects[i]);

	moveDeceleration(enemyobjects[i], GetStgFrameWidth/2 + 64 + enemyoffsets[i][0], GetStgFrameHeight - 160 + enemyoffsets[i][1], 20, false);
}

function SetPlayerPort(img, i){
	portrait_player[i] = img;
	ObjPrim_SetTexture(playerobjects[i], portrait_player[i]);
	ObjSprite2D_SetSourceRect(playerobjects[i], 0, 0, GetTextureWidth(portrait_player[i]), GetTextureHeight(portrait_player[i]));
	ObjSprite2D_SetDestCenter(playerobjects[i]);
}

function SetEnemyPort(img, i){
	portrait_enemy[i] = img;
	ObjPrim_SetTexture(enemyobjects[i], portrait_enemy[i]);
	ObjSprite2D_SetSourceRect(enemyobjects[i], 0, 0, GetTextureWidth(portrait_enemy[i]), GetTextureHeight(portrait_enemy[i]));
	ObjSprite2D_SetDestCenter(enemyobjects[i]);
}

task RemovePlayerPort(i){ //moves out the player portrait
	portrait_player[i] = "";

	moveDeceleration(playerobjects[i], ObjRender_GetX(playerobjects[i]), GetStgFrameHeight + 384, 20, false);
	wait(20);

	ObjPrim_SetTexture(playerobjects[i], "");
	ObjSprite2D_SetSourceRect(playerobjects[i], 0, 0, 0, 0);
	ObjSprite2D_SetDestCenter(playerobjects[i]);

}

task RemoveEnemyPort(i){ //moves out the enemy portrait
	portrait_enemy[i] = "";

	moveDeceleration(enemyobjects[i], ObjRender_GetX(enemyobjects[i]), GetStgFrameHeight + 384, 20, false);
	wait(20);

	ObjPrim_SetTexture(enemyobjects[i], "");
	ObjSprite2D_SetSourceRect(enemyobjects[i], 0, 0, 0, 0);
	ObjSprite2D_SetDestCenter(enemyobjects[i]);
}

task DisplayEnemyTitle(img, time){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img);
	ObjSprite2D_SetSourceRect(obj, 0, 0, GetTextureWidth(img), GetTextureHeight(img));
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2 + 32, GetStgFrameHeight - 160, 0);
	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	Obj_SetRenderPriorityI(obj, 77);
	moveSmoother(obj, GetStgFrameWidth, GetStgFrameHeight - 160, time*3, false);

	let a = 0;
	while(a < 256){
		ObjRender_SetAlpha(obj, a);
		a += 8;
		yield;
	}
	loop(time){
		if(!GetCommonData("IsDialogueSequenceActive", false)){break;}
		yield;
	}
	while(a > 0){
		ObjRender_SetAlpha(obj, a);
		a -= 8;
		yield;
	}
	Obj_Delete(obj);
}

function RetrieveDistance(x1,y1,x2,y2) {
	return ((x1-x2)^2+(y1-y2)^2)^(1/2);
}