#RequireContext CSmMapType
#Include "TextLib" as TextLib
#Include "Libs/Nadeo/Anchor.Script.txt" as Anchor

declare Text Rules;

// ---------------------------------- //
// Initialize the anchors
Void InitAnchors() {
	foreach (Anchor in AnchorData) {
		if (Anchor.DefaultTag == "Spawn") {
			Anchor.Tag = Anchor.DefaultTag;
			if (Anchor.Order != 1 && Anchor.Order != 2) Anchor.Order = 1;
		} else if (Anchor.DefaultTag == "Goal") {
			Anchor.Tag = Anchor.DefaultTag;
			if (Anchor.Order != 1 && Anchor.Order != 2 && Anchor.Order != 3) Anchor.Order = 1;
		} else if (Anchor.DefaultTag == "Checkpoint") {
			Anchor.Tag = "Goal";
			if (Anchor.Order != 1 && Anchor.Order != 2 && Anchor.Order != 3) Anchor.Order = 3;
		} else {
			Anchor.Tag = Anchor.DefaultTag;
			Anchor.Order = Anchor.DefaultOrder;
		}
	}
}

// ---------------------------------- //
// Check if the map is valid
Void UpdateValidability() {
	InitAnchors();
	declare NbGoal1 = 0;
	declare NbGoal2 = 0;
	declare NbEngage = 0;
	foreach(Data in AnchorData) {		
		if (Data.Tag == "Goal" && Data.Order == 1) NbGoal1 += 1;
		if (Data.Tag == "Goal" && Data.Order == 2) NbGoal2 += 1;
		if (Data.Tag == "Goal" && Data.Order == 3) NbEngage += 1;
	}
	
	Anchor::UpdateAnchorCounts();
	if(NbEngage > 1) {
		ValidabilityRequirementsMessage = _("You cannot place more than one Goal 'Engage'");
		ValidationStatus = CSmMapType::ValidationStatus::NotValidable;
		return;
	}
	
	for(I, 1, 2) {
		if (!Anchor::HasAtLeastOneAnchor("Goal", I, TextLib::Compose(_("You must place at least one Goal #%1"), TextLib::ToText(I)))) return;	
		if (!Anchor::HasExactlyOneAnchor("Spawn", I, TextLib::Compose(_("You must place exactly one Spawn #%1"), TextLib::ToText(I)))) return;
	}
	if (NbGoal1 != NbGoal2) {
		ValidationStatus = CSmMapType::ValidationStatus::NotValidable;
		ValidabilityRequirementsMessage = _("You must place the same number of Goal #1 and Goal #2");
		return;
	}
	
	ValidationStatus = CSmMapType::ValidationStatus::Validated;	
}

// ---------------------------------- //
// Return the manialink for anchor edition
Text EditAnchorManialink(Text _Tag) {
	
	declare MLText = 		
	"""
	<script><!--
		#Include "TextLib" as TextLib
		
		main() {
			declare Buttons = [
			(Page.GetFirstChild("1") as CMlLabel), 
			(Page.GetFirstChild("2") as CMlLabel),
			(Page.GetFirstChild("Engage") as CMlLabel)
			];
			
			declare Integer AnchorOrder for Page;
			declare Text	AnchorTag for Page;
			declare Boolean Finish for Page;
			Finish = False;	
			while(!Finish) {
				yield;
				foreach(Button in Buttons) {
					if(((Button.ControlId == "1") || (Button.ControlId == "2")) 
						&& (AnchorOrder == TextLib::ToInteger(Button.ControlId))) {
						Button.Value = "$090"^Button.ControlId;
					} else if((Button.ControlId == "Engage") && (AnchorOrder == 3)) {
						Button.Value = "$090"^Button.ControlId;
					} else {
						Button.Value = "$fff"^Button.ControlId;
					}
					
					/*
					if(AnchorOrder == TextLib::ToInteger(Button.ControlId))
						Button.Value = "$090"^Button.ControlId;
					else 
						Button.Value = "$fff"^Button.ControlId;
					*/
				}
				
				foreach(Event in PendingEvents) {
					switch(Event.Type){
						case CMlEvent::Type::MouseClick : {
							if (Event.ControlId == "Engage") {
								AnchorOrder = 3;
							} else if (Event.ControlId != "Cancel") {
								AnchorOrder = TextLib::ToInteger(Event.ControlId);
							}
							Finish = True;
						}	
					}
				}
			}
		}			
	--></script>
	<frame posn="120 42">
		<quad posn="0 0 -1" sizen="70 35" halign="center" style="Bgs1" substyle="BgWindow2"/>
		<quad posn="0 0 2" sizen="74 15" halign="center" valign="bottom" style="Bgs1InRace" substyle="BgTitle3_5"/>
		<label posn="0 5 3" halign="center" valign="bottom" style="TextTitle3" textprefix="$fff" text="{{{TextLib::Compose("%1 %2", _("Edit"), _Tag)}}}" />
		<frame posn="0 -5">
			<label id="1" 		posn="-10  0" scale="2" halign="center" style="TextButtonMedium" ScriptEvents="true"/>
			<label id="2" 		posn=" 10  0" scale="2" halign="center" style="TextButtonMedium" ScriptEvents="true"/>
			<label id="Engage" 	posn="  0 -8" scale="2" halign="center" style="TextButtonMedium" ScriptEvents="true"/>
		</frame>
		<label id="Cancel" text="{{{_("Cancel")}}}" posn="0 -22" halign="center" style="CardButtonSmall" ScriptEvents="true"/>
    </frame>
	<frame posn="120 -10">
		<quad posn="0 0 2" sizen="74 15" halign="center" valign="bottom" style="Bgs1InRace" substyle="BgTitle3_5"/>
		<label posn="0 5 3" halign="center" valign="bottom" style="TextTitle3" textprefix="$fff" text="{{{_("Rules")}}}" />
		<quad posn="0 0 -1" sizen="70 60" halign="center" style="Bgs1" substyle="BgWindow2"/>
		<frame posn="-30 -4">
			<label posn="0 0" sizen="60 5" autonewline="1" style="TextCardSmallScores2" textprefix="$222" text="{{{ Rules }}}" />
		</frame>
	</frame>
	""";
  
	return MLText;
}

// ---------------------------------- //
// Manialink for non editable anchor
Text NoEditManialink() {
	return """
<frame posn="120 35">
	<quad posn="0 0 -1" sizen="70 15" halign="center" style="Bgs1" substyle="BgWindow2"/>
	<quad posn="0 0 2" sizen="74 15" halign="center" valign="bottom" style="Bgs1InRace" substyle="BgTitle3_5"/>
	<label posn="0 5.5 3" scale="0.9" halign="center" valign="bottom" style="TextTitle3" textprefix="$fff" text="{{{_("This anchor is not editable")}}}" />
	<label text="OK" posn="0 -3.5 3" halign="center" style="CardButtonMedium" ScriptEvents="true"/>
    </frame>
<frame posn="120 5">
	<quad posn="0 0 2" sizen="74 15" halign="center" valign="bottom" style="Bgs1InRace" substyle="BgTitle3_5"/>
	<label posn="0 5 3" halign="center" valign="bottom" style="TextTitle3" textprefix="$fff" text="{{{_("Rules")}}}" />
	<quad posn="0 0 -1" sizen="70 55" halign="center" style="Bgs1" substyle="BgWindow2"/>
	<frame posn="-30 -4">
		<label posn="0 0" sizen="60 5" autonewline="1" style="TextCardSmallScores2" textprefix="$222" text="{{{ Rules }}}" />
	</frame>
</frame>
<script><!--
main() {
	declare Boolean Finish for Page;
	Finish = False;	
	while(!Finish) {
		yield;
		
		foreach(Event in PendingEvents) {
			switch(Event.Type){
				case CMlEvent::Type::MouseClick : {
					Finish = True;
				}	
			}
		}
	}
}
--></script>
""";
}

// ---------------------------------- //
// Show the anchor edition manialink
Void EditAnchorData(Ident _EditedAnchorDataId)
{	
	UpdateValidability();
	declare Integer AnchorOrder for ManialinkPage;
	declare Boolean Finish for ManialinkPage;
	
	declare DefaultTag = AnchorData[_EditedAnchorDataId].DefaultTag;
	if (DefaultTag == "Spawn" || DefaultTag == "Checkpoint" || DefaultTag == "Goal") ManialinkText = EditAnchorManialink(AnchorData[_EditedAnchorDataId].Tag);
	else ManialinkText = NoEditManialink();
	
	AnchorOrder = AnchorData[_EditedAnchorDataId].Order;
	Finish = False;
	while(!Finish) {
		yield;
	}
	AnchorData[_EditedAnchorDataId].Order = AnchorOrder;
	UpdateValidability();
}

// ---------------------------------- //
// Main
// ---------------------------------- //

main() {		
	UpdateValidability();
	CustomEditAnchorData = True;
	Rules = _("You must place exactly one Spawn #1 and one Spawn #2.\nYou must place the same number of Goal #1 and Goal #2 with at least one of each.\n\nYou can place one Goal 'Engage' to determine which team will start attacking in Battle Waves mode.");
	while (True) {	
		yield;
		ManialinkText = "";			
		foreach(Event in PendingEvents) {
			if(Event.Type == CPluginEvent::Type::MapModified) {
				UpdateValidability();			
			} else if(Event.Type == CPluginEvent::Type::EditAnchor) {
				EditAnchorData(Event.EditedAnchorDataId);
			}
		}		
	}	
}
