//Woo I finally got my own damn cutin. About time.
//Uses Dark's interpolated movement functions and Ultima's hermite movement functions

let spellInfoImg = GetCurrentScriptDirectory ~ "../IMG/Effect/Cutin/spellAttack.png";
let warningImg = GetCurrentScriptDirectory ~ "../IMG/Effect/Cutin/nootnootwarning.png";
let warningBGImg = GetCurrentScriptDirectory ~ "../IMG/Effect/Cutin/warning_bg.png";

let boss; //boss enemy object
let bossScene; //boss scene object

task CutinPythonA1(spellName, difficulty, img, color){
	boss = GetEnemyBossObjectID[0];
	bossScene = GetEnemyBossSceneObjectID;

	if(!IsCommonDataAreaExists("cutin_History")){
		CreateCommonDataArea("cutin_History");
	}
	LoadCommonDataAreaA2("cutin_History", GetCurrentScriptDirectory ~ "./../data/data_cutinHistory.dat");

	Name(spellName, color);
	Image(img, color);
	Warning(color);
	WarningBG(color);
	wait(180);
	BonusPuppies(spellName, difficulty, color);
}

task Name(text, color){
	let bg = ObjPrim_Create(OBJ_SPRITE_2D); //spell line
	ObjPrim_SetTexture(bg, spellInfoImg);
	ObjSprite2D_SetSourceRect(bg, 0, 0, 255, 32);
	ObjSprite2D_SetDestRect(bg, -255, -16, 0, 16);
	ObjRender_SetColor(bg, color[0], color[1], color[2]);
	Obj_SetRenderPriorityI(bg, 80);

	let obj = ObjText_Create; //spell text
	ObjText_SetFontType(obj, "Noto Sans Japanese Medium");
	ObjText_SetText(obj, text);
	ObjText_SetFontSize(obj, 14);
	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, GetStgFrameWidth - 24);
	ObjText_SetHorizontalAlignment(obj, ALIGNMENT_RIGHT);
	Obj_SetRenderPriorityI(obj, 80);

	ObjRender_SetAlpha(bg, 0);
	ObjRender_SetScaleXYZ(bg, 4, 4, 0);
	ObjRender_SetPosition(bg, GetStgFrameWidth - 16, GetStgFrameHeight/2 + 8, 0);

	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetScaleXYZ(obj, 4, 4, 0);
	ObjRender_SetPosition(obj, 16, GetStgFrameHeight/2, 0);

	let al = 0;
	let s = 4;
	loop(60){
		ObjRender_SetAlpha(bg, al);
		ObjRender_SetScaleXYZ(bg, s, s, s);

		ObjRender_SetAlpha(obj, al);
		ObjRender_SetScaleXYZ(obj, s, s, s);

		al += 255/60;
		s -= 3/60;
		yield;
	}
	ObjRender_SetAlpha(bg, 255);
	ObjRender_SetScaleXYZ(bg, 1, 1, 1);

	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	loop(60){yield;}
	moveDeceleration(bg, GetStgFrameWidth - 16, 14 + 8, 35, false);
	moveDeceleration(obj, 16, 14, 35, false);

	while(ObjEnemy_GetInfo(boss, INFO_LIFE) > 0){yield;}
	moveAcceleration(bg, ObjRender_GetX(bg) + 384, 14 + 8, 45, false);
	moveAcceleration(obj, ObjRender_GetX(obj) + 384, 14 + 8, 45, false);
}

task BonusPuppies(name, diff, color){
	let bonus = ObjEnemyBossScene_GetInfo(bossScene, INFO_SPELL_SCORE); //spell bonus


	//the following snippet was taken from GTBot's cutin library because there's not much to really do any differently

	//These are long-looking common data group names
	let SpellDataAttempt = name~"|"~diff~"|"~GetPlayerID~"|"~"Attempt";
	let SpellDataGet = name~"|"~diff~"|"~GetPlayerID~"|"~"Get";

	//This is actually the common data retrieval
	let SpellValueAttempt = GetAreaCommonData("cutin_History", SpellDataAttempt, 0);
	let SpellValueGet = GetAreaCommonData("cutin_History", SpellDataGet, 0);
		
		
	if(!IsReplay){
		SpellValueAttempt++;
		SetAreaCommonData("cutin_History", SpellDataAttempt, SpellValueAttempt);
		SaveCommonDataAreaA2("cutin_History", GetCurrentScriptDirectory ~ "./../data/data_cutinHistory.dat");
	}
	//GTBot code ends here

	let shit = rtos("000", min(999, GetAreaCommonData("cutin_History", SpellDataGet, 0))) ~ " / " ~ rtos("000", min(999, GetAreaCommonData("cutin_History", SpellDataAttempt, 0)));


	let obj = ObjText_Create; //bonus
	ObjText_SetFontType(obj, "Noto Sans Japanese Medium");
	ObjText_SetText(obj, "Bonus: " ~ IntToString(bonus));
	ObjText_SetFontSize(obj, 10);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, color[0]/2, color[1]/2, color[2]/2);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderWidth(obj, 1);
	ObjText_SetFontBorderColor(obj, 0, 0, 0);
	Obj_SetRenderPriorityI(obj, 80);

	let obj2 = ObjText_Create; //spell history
	ObjText_SetFontType(obj2, "Noto Sans Japanese Medium");
	ObjText_SetText(obj2, "History: " ~ shit);
	ObjText_SetFontSize(obj2, 10);
	ObjText_SetFontColorTop(obj2, 255, 255, 255);
	ObjText_SetFontColorBottom(obj2, color[0]/2, color[1]/2, color[2]/2);
	ObjText_SetFontBorderType(obj2, BORDER_FULL);
	ObjText_SetFontBorderWidth(obj2, 1);
	ObjText_SetFontBorderColor(obj2, 0, 0, 0);
	Obj_SetRenderPriorityI(obj2, 80);

	ObjRender_SetPosition(obj, GetStgFrameWidth - 165, 31, 0);
	ObjRender_SetAlpha(obj, 0);

	ObjRender_SetPosition(obj2, GetStgFrameWidth - 96, 31, 0);
	ObjRender_SetAlpha(obj2, 0);

	let al = 0;
	loop(30){
		ObjRender_SetAlpha(obj, al);
		ObjRender_SetAlpha(obj2, al);
		al += 255/30;
		yield;
	}
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetAlpha(obj2, 255);

	while(ObjEnemy_GetInfo(boss, INFO_LIFE) > 0){
		if(ObjEnemyBossScene_GetInfo(bossScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(bossScene, INFO_PLAYER_SPELL_COUNT) == 0){ //if the player has neither bombed nor died
			bonus = ObjEnemyBossScene_GetInfo(bossScene, INFO_SPELL_SCORE); //spell bonus
			ObjText_SetText(obj, "Bonus: " ~ IntToString(bonus));
		}else{
			bonus = 0;
			ObjText_SetText(obj, "Bonus: Failed");
		}
		yield;
	}

	if(ObjEnemyBossScene_GetInfo(bossScene, INFO_IS_DURABLE_SPELL)){
		if(bonus >= 1){
			if(!IsReplay){
				SpellValueGet++;
				SetAreaCommonData("cutin_History", SpellDataGet, SpellValueGet);
				SaveCommonDataAreaA2("cutin_History", GetCurrentScriptDirectory ~ "./../data/data_cutinHistory.dat");
			}
		}
	}else{
		if(bonus >= 1 && ObjEnemyBossScene_GetInfo(bossScene, INFO_TIMERF) > 0){
			if(!IsReplay){
				SpellValueGet++;
				SetAreaCommonData("cutin_History", SpellDataGet, SpellValueGet);
				SaveCommonDataAreaA2("cutin_History", GetCurrentScriptDirectory ~ "./../data/data_cutinHistory.dat");
			}
		}
	}

	loop(30){
		ObjRender_SetAlpha(obj, al);
		ObjRender_SetAlpha(obj2, al);
		al -= 255/30;
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(obj2);
}

task Image(img, color){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img);
	ObjSprite2D_SetSourceRect(obj, 0, 0, GetTextureWidth(img), GetTextureHeight(img));
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 78);

	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2 - 64, GetStgFrameHeight + 128, 0);

	MoveImage;
	TrailImage;

	let al = 0;
	loop(30){
		ObjRender_SetAlpha(obj, al);
		al += 255/30;
		yield;
	}

	ObjRender_SetAlpha(obj, 255);
	loop(120){
		yield;
	}

	loop(30){
		ObjRender_SetAlpha(obj, al);
		al -= 255/30;
		yield;
	}
	Obj_Delete(obj);

	task MoveImage{
		ObjMove_SetDestAtHermiteF(obj, GetStgFrameWidth/2 + 64, GetStgFrameHeight/2 - 32, 1250, 135-180, -500, 135, 60);
		loop(60){yield;}
		moveLinear(obj, GetStgFrameWidth/2 - 64, GetStgFrameHeight/2 + 32, 90, false);
		loop(90){yield;}
		ObjMove_SetDestAtHermiteF(obj, GetStgFrameWidth/2 + 64, -128, -1250, 135, 500, 135+180, 60);
	}

	task TrailImage{
		loop(30){
			AfterImage(img, color);
			loop(6){yield;}
		}
	}

	task AfterImage(path, c){
		let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj2, path);
		ObjSprite2D_SetSourceRect(obj2, 0, 0, GetTextureWidth(img), GetTextureHeight(img));
		ObjSprite2D_SetDestCenter(obj2);
		ObjRender_SetColor(obj2, c[0], c[1], c[2]);
		ObjRender_SetPosition(obj2, ObjRender_GetX(obj), ObjRender_GetY(obj), 0);
		ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obj2, 77);

		let s = 1;
		let alpha = 255;
		loop(45){
			ObjRender_SetAlpha(obj2, alpha);
			ObjRender_SetScaleXYZ(obj2, s, s, 0);
			alpha -= 255/45;
			s += 0.75/45;
			yield;
		}
		Obj_Delete(obj2);
	}
}

task Warning(color){
	let back = ObjPrim_Create(OBJ_SPRITE_2D); //shadow
	ObjPrim_SetTexture(back, warningImg);
	ObjSprite2D_SetSourceRect(back, 512, 0, 1024, 128);
	ObjSprite2D_SetDestCenter(back);
	ObjRender_SetColor(back, color[0], color[1], color[2]);
	Obj_SetRenderPriorityI(back, 80);

	let obj = ObjPrim_Create(OBJ_SPRITE_2D); //text
	ObjPrim_SetTexture(obj, warningImg);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 128);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 80);

	ObjRender_SetScaleXYZ(back, 0.75, 0.75, 0);
	ObjRender_SetAngleZ(back, 30);
	ObjRender_SetPosition(back, -32, GetStgFrameHeight/2 + 32, 0);
	ObjRender_SetAlpha(back, 0);

	ObjRender_SetScaleXYZ(obj, 0.75, 0.75, 0);
	ObjRender_SetAngleZ(obj, 30);
	ObjRender_SetPosition(obj, -32, GetStgFrameHeight/2 + 32, 0);
	ObjRender_SetAlpha(obj, 0);
	
	MoveWarning;

	let al = 0;
	let xan = 0;
	loop(30){
		ObjRender_SetAlpha(back, al);
		ObjRender_SetAlpha(obj, al);
		al += 255/30;
		yield;
	}
	ObjRender_SetAlpha(back, 255);
	ObjRender_SetAlpha(obj, 255);
	loop(60){yield;}


	//the following bit of code changes the text from "Warning!!" to "Spell Card Attack!!"
	loop(15){
		//ObjRender_SetAngleX(back, xan);
		//ObjRender_SetAngleX(obj, xan);

		ObjRender_SetAngleY(back, -xan);
		ObjRender_SetAngleY(obj, -xan);
		xan += 90/15;
		yield;
	}
	//ObjRender_SetAngleX(back, 90);
	ObjRender_SetAngleY(back, -90);
	ObjSprite2D_SetSourceRect(back, 512, 128, 1024, 256);
	ObjSprite2D_SetDestCenter(back);
	ObjRender_SetAngleZ(back, -30);

	//ObjRender_SetAngleX(obj, 90);
	ObjRender_SetAngleY(obj, -90);
	ObjSprite2D_SetSourceRect(obj, 0, 128, 512, 256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAngleZ(obj, -30);
	loop(15){
		//ObjRender_SetAngleX(back, xan);
		//ObjRender_SetAngleX(obj, xan);

		ObjRender_SetAngleY(back, -xan);
		ObjRender_SetAngleY(obj, -xan);
		xan += 90/15;
		yield;
	}
	//ObjRender_SetAngleX(back, 0);
	ObjRender_SetAngleY(back, 180);
	//ObjRender_SetAngleX(obj, 0);
	ObjRender_SetAngleY(obj, 180);


	loop(60){yield;}
	loop(30){
		ObjRender_SetAlpha(back, al);
		ObjRender_SetAlpha(obj, al);
		al -= 255/30;
		yield;
	}
	Obj_Delete(back);
	Obj_Delete(obj);

	task MoveWarning{
		moveDeceleration(back, -32 + 192*cos(30), GetStgFrameHeight/2 + 32 + 192*sin(30), 60, false);
		moveDeceleration(obj, -32 + 192*cos(30), GetStgFrameHeight/2 + 32 + 192*sin(30), 60, false);
		loop(60){yield;}
		moveLinear(back, -32 + 224*cos(30), GetStgFrameHeight/2 + 32 + 224*sin(30), 90, false);
		moveLinear(obj, -32 + 224*cos(30), GetStgFrameHeight/2 + 32 + 224*sin(30), 90, false);
		loop(90){yield;}
		moveAcceleration(back, -32 + 448*cos(30), GetStgFrameHeight/2 + 32 + 448*sin(30), 60, false);
		moveAcceleration(obj, -32 + 448*cos(30), GetStgFrameHeight/2 + 32 + 448*sin(30), 60, false);
	}
}

task WarningBG(color){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, warningBGImg);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 4096, 256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetColor(obj, color[0], color[1], color[2]);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj, 79);

	ObjRender_SetScaleXYZ(obj, 0.75, 0, 0);
	ObjRender_SetAngleZ(obj, 30);
	ObjRender_SetPosition(obj, -32, GetStgFrameHeight/2 + 32, 0);

	Scroll;

	let s = 0;
	loop(30){
		ObjRender_SetScaleY(obj, s);
		s += 0.5/30;
		yield;
	}
	ObjRender_SetScaleY(obj, 0.5);

	loop(150){yield;}

	loop(30){
		ObjRender_SetScaleY(obj, s);
		s -= 0.5/30;
		yield;
	}
	Obj_Delete(obj);

	task Scroll{
		let x = 0;
		while(!Obj_IsDeleted(obj)){
			if(x < -256){x = 0;}
			ObjSprite2D_SetSourceRect(obj, x, 0, x+4096, 256);
			ObjSprite2D_SetDestCenter(obj);

			x -= 8;
			yield;
		}
	}
}