class UR_ClassMenu extends UTUIScene;

var transient UILabelButton ButClassSurfer, ButClassStealth, ButClassAkimbo;
var transient UILabelButton ButWeaponSniper, ButWeaponLink, ButWeaponRox, ButWeaponHammer;
var transient UILabelButton ButKitSlowField, ButKitShield, ButKitLandMine, ButKitSkyMine;
var transient UILabelButton ButRespawn;
var transient UISlider SliWeaponEffects, SliArmour;
var LinearColor RedButton, GreenButton;
var transient UILabel LblDescription;
var string SurferText, StealthText, AkimboText, RoxText, LinkText, SniperText, HammerText, SlowfieldText, ShieldText, LandMineText, SkyMineText;

var UR_PlayerController PC;

event PostInitialize()
{
    ButClassSurfer = UILabelButton(FindChild('ButClassSurfer', true));
	ButClassStealth = UILabelButton(FindChild('ButClassStealth', true));
	ButClassAkimbo = UILabelButton(FindChild('ButClassAkimbo', true));

	ButWeaponSniper = UILabelButton(FindChild('ButWeaponSniper', true));
	ButWeaponLink = UILabelButton(FindChild('ButWeaponLink', true));
	ButWeaponRox = UILabelButton(FindChild('ButWeaponRox', true));
	ButWeaponHammer = UILabelButton(FindChild('ButWeaponHammer', true));

	ButKitSlowField = UILabelButton(FindChild('ButKitSlowField', true));
	ButKitShield = UILabelButton(FindChild('ButKitShield', true));
	ButKitLandMine = UILabelButton(FindChild('ButKitLandMine', true));
	ButKitSkyMine = UILabelButton(FindChild('ButKitSkyMine', true));

	ButRespawn = UILabelButton(FindChild('ButRespawn', true));

	SliWeaponEffects = UISlider(FindChild('SliEffects', true));
	SliArmour = UISlider(FindChild('SliArmour', true));

	LblDescription = UILabel(FindChild('LblPlayerDescription', true));

	SliWeaponEffects.SliderValue.CurrentValue = 0.f;
	SliWeaponEffects.SliderValue.MinValue = 0.f;
	SliWeaponEffects.SliderValue.MaxValue = 360.f;
	SliWeaponEffects.SliderValue.NudgeValue = 10.f;
	SliWeaponEffects.SliderValue.bIntRange = true;

	SliArmour.SliderValue.CurrentValue = 2.f;
	SliArmour.SliderValue.MinValue = 0.f;
	SliArmour.SliderValue.MaxValue = 7.f;
	SliArmour.SliderValue.NudgeValue = 1.f;
	SliArmour.SliderValue.bIntRange = true;

	ButClassSurfer.OnClicked = OnClickedDelegate;
	ButClassStealth.OnClicked = OnClickedDelegate;
	ButClassAkimbo.OnClicked = OnClickedDelegate;

	ButWeaponSniper.OnClicked = OnClickedDelegate;
	ButWeaponLink.OnClicked = OnClickedDelegate;
	ButWeaponRox.OnClicked = OnClickedDelegate;
	ButWeaponHammer.OnClicked = OnClickedDelegate;

	ButKitSlowField.OnClicked = OnClickedDelegate;
	ButKitShield.OnClicked = OnClickedDelegate;
	ButKitLandMine.OnClicked = OnClickedDelegate;
	ButKitSkyMine.OnClicked = OnClickedDelegate;

	ButRespawn.OnClicked = OnClickedDelegate;

	super.PostInitialize();

	GetPlayerSelection();
}

function GetPlayerSelection()
{

    switch (PC.getClass())
	{
		case eSurfer:
			HighlightButtons("SURFER"); //can't have rockets
			break;
		case eStealth:
			HighlightButtons("STEALTH"); //can't have sniper
			break;
		case eAkimbo:
			HighlightButtons("AKIMBO"); //can't have hammer
			break;
		default:
			`log("Class Failed in GetPlayerSelection");
			break;
	}

	switch (PC.getWeapon())
	{
		case eRox:
			HighlightButtons("ROX");
			break;
		case eLink:
			HighlightButtons("LINK");
			break;
		case eSniper:
			HighlightButtons("SNIPER");
			break;
		case eHammer:
			HighlightButtons("HAMMER");
			break;
		default:
			`log("Weapon Failed in GetPlayerSelection");
			break;
	}

	switch (PC.getKit())
	{
		case eSkyMine:
			HighlightButtons("SKYMINE");
			break;
		case eLandMine:
			HighlightButtons("LandMine");
			break;
		case eSlowField:
			HighlightButtons("SLOWFIELD");
			break;
		case eShield:
			HighlightButtons("SHIELD");
			break;
		default:
			`log("Weapon Failed in GetPlayerSelection");
			break;
	}

	SliArmour.SliderValue.CurrentValue = UR_PlayerReplicationInfo(PC.PlayerReplicationInfo).ArmourRatio;
	SliWeaponEffects.SliderValue.CurrentValue = UR_PlayerReplicationInfo(PC.PlayerReplicationInfo).ColourData;
}

function HighlightButtons(string Selection)
{
	switch(Selection)
	{
		case "SURFER":
			ButClassSurfer.BackgroundImageComponent.SetColor(GreenButton);
			ButClassStealth.BackgroundImageComponent.SetColor(RedButton);
			ButClassAkimbo.BackgroundImageComponent.SetColor(RedButton);
			break;
		case "STEALTH":
			ButClassSurfer.BackgroundImageComponent.SetColor(RedButton);
			ButClassStealth.BackgroundImageComponent.SetColor(GreenButton);
			ButClassAkimbo.BackgroundImageComponent.SetColor(RedButton);
			break;
		case "AKIMBO":
			ButClassSurfer.BackgroundImageComponent.SetColor(RedButton);
			ButClassStealth.BackgroundImageComponent.SetColor(RedButton);
			ButClassAkimbo.BackgroundImageComponent.SetColor(GreenButton);
			break;
		case "ROX":
			ButWeaponRox.BackgroundImageComponent.SetColor(GreenButton);
			ButWeaponLink.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponSniper.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponHammer.BackgroundImageComponent.SetColor(RedButton);
			break;
		case "LINK":
			ButWeaponRox.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponLink.BackgroundImageComponent.SetColor(GreenButton);
			ButWeaponSniper.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponHammer.BackgroundImageComponent.SetColor(RedButton);
			break;
		case "SNIPER":
			ButWeaponRox.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponLink.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponSniper.BackgroundImageComponent.SetColor(GreenButton);
			ButWeaponHammer.BackgroundImageComponent.SetColor(RedButton);
			break;
		case "HAMMER":
			ButWeaponRox.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponLink.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponSniper.BackgroundImageComponent.SetColor(RedButton);
			ButWeaponHammer.BackgroundImageComponent.SetColor(GreenButton);
			break;
		case "SKYMINE":
			ButKitSkymine.BackgroundImageComponent.SetColor(GreenButton);
			ButKitLandMine.BackgroundImageComponent.SetColor(RedButton);
			ButKitSlowfield.BackgroundImageComponent.SetColor(RedButton);
			ButKitShield.BackgroundImageComponent.SetColor(RedButton);
			break;
		case "LandMine":
			ButKitSkymine.BackgroundImageComponent.SetColor(RedButton);
			ButKitLandMine.BackgroundImageComponent.SetColor(GreenButton);
			ButKitSlowfield.BackgroundImageComponent.SetColor(RedButton);
			ButKitShield.BackgroundImageComponent.SetColor(RedButton);
			break;
		case "SLOWFIELD":
			ButKitSkymine.BackgroundImageComponent.SetColor(RedButton);
			ButKitLandMine.BackgroundImageComponent.SetColor(RedButton);
			ButKitSlowfield.BackgroundImageComponent.SetColor(GreenButton);
			ButKitShield.BackgroundImageComponent.SetColor(RedButton);
			break;
		case "SHIELD":
			ButKitSkymine.BackgroundImageComponent.SetColor(RedButton);
			ButKitLandMine.BackgroundImageComponent.SetColor(RedButton);
			ButKitSlowfield.BackgroundImageComponent.SetColor(RedButton);
			ButKitShield.BackgroundImageComponent.SetColor(GreenButton);
			break;
	}
}

function bool OnClickedDelegate(UIScreenObject EventObject, int PlayerIndex)
{
    switch(EventObject)
	{
		case ButClassSurfer:
			PC.changeClass(eSurfer);
			HighlightButtons("SURFER");
			return true;
		case ButClassStealth:
			PC.changeClass(eStealth);
			HighlightButtons("STEALTH");
			return true;
		case ButClassAkimbo:
			PC.changeClass(eAkimbo);
			HighlightButtons("AKIMBO");
			return true;

		case ButWeaponSniper:
			PC.changeWeapon(eSniper);
			HighlightButtons("SNIPER");
			return true;
		case ButWeaponLink:
			PC.changeWeapon(eLink);
			HighlightButtons("LINK");
			return true;
		case ButWeaponRox:
			PC.changeWeapon(eRox);
			HighlightButtons("ROX");
			return true;
		case ButWeaponHammer:
			PC.changeWeapon(eHammer);
			HighlightButtons("HAMMER");
			return true;

		case ButKitSlowField:
			PC.changeKit(eSlowField);
			HighlightButtons("SLOWFIELD");
			return true;
		case ButKitShield:
			PC.changeKit(eShield);
			HighlightButtons("SHIELD");
			return true;
		case ButKitLandMine:
			PC.changeKit(eLandMine);
			HighlightButtons("LandMine");
			return true;
		case ButKitSkyMine:
		    PC.changeKit(eSkyMine);
			HighlightButtons("SKYMINE");
			return true;

		case ButRespawn:
			SetAndRespawnPlayer();
			return true;

		Default:
			`log("Event Object was not caught in OnClickedDelegate: " $ EventObject);
			return true;
	}

	return false;
}

static simulated function LinearColor CalculateRBGfromHue(float PreucilAngle)
{
	local LinearColor ColourToUse;
    local float DensityMax, DensityMin, Red,Green,Blue,Hue,Saturation,Value,EuclidianDistance,ComponentX,ComponentY,ComponentZ;
    local int Ordering;

	ColourToUse = MakeLinearColor(255,1,1,1);

    Red =ColourToUse.R;///255.0;
    Green = ColourToUse.G;///255.0;
    Blue = ColourToUse.B;///255.0;

    if(Red == Green && Green == Blue)
	{
        return ColourToUse;
	}

    DensityMax = FMax(Red,FMax(Green,Blue));
    DensityMin = FMin(Red,FMin(Green,Blue));

    if(DensityMax == Red)
    {
        if(Green >= Blue)
		{
            Hue = 60.0*(Green-Blue)/(DensityMax-DensityMin);
		}
        else
		{
            Hue = 60.0*(Green-Blue)/(DensityMax-DensityMin)+360.0;
		}
    }
    else if(DensityMax == Green)
    {
        Hue = 60.0*(Blue-Red)/(DensityMax-DensityMin)+120.0;
    }
    else
    {
        Hue = 60.0*(Red-Green)/(DensityMax-DensityMin)+240.0;
    }

    if(DensityMax == 0.0)
	{
        Saturation = 0.0;
	}
    else
	{
        Saturation = 1.0-DensityMin/DensityMax;
	}

    Value = DensityMax;


    Hue = (Hue + PreucilAngle)%360;
    Ordering = int(Hue/60)%6;
    EuclidianDistance = Hue/60.0-Ordering;
    ComponentX = Value*(1-Saturation);
    ComponentY = Value*(1-EuclidianDistance*Saturation);
    ComponentZ = Value*(1-(1-EuclidianDistance)*Saturation);

    switch (Ordering)
    {
		case 0:   return MakeLinearColor(Value,ComponentZ,ComponentX,1);
		case 1:   return MakeLinearColor(ComponentY,Value,ComponentX,1);
		case 2:   return MakeLinearColor(ComponentX,Value,ComponentZ,1);
		case 3:   return MakeLinearColor(ComponentX,ComponentY,Value,1);
		case 4:   return MakeLinearColor(ComponentZ,ComponentX,Value,1);
		case 5:   return MakeLinearColor(Value,ComponentX,ComponentY,1);
	}
    return ColourToUse;
}

function ExitMenu()
{
	CloseScene(self);
}

function bool HandleInputKey( const out InputEventParameters EventParms )
{
	local bool bResult;

	bResult=false;

	if(EventParms.EventType==IE_Released)
	{
		if(EventParms.InputKeyName=='Escape')
		{
			ExitMenu();
			bResult=true;
		}
	}

	return bResult;
}

function SetAndRespawnPlayer()
{
	PC.changeShield(SliArmour.SliderValue.CurrentValue);
	PC.changePlayerEffect(SliWeaponEffects.SliderValue.CurrentValue, CalculateRBGfromHue(SliWeaponEffects.SliderValue.CurrentValue));
	PC.ServerForceRestartPlayer();
	ExitMenu();
}

function UpdateDescriptionText()
{
	local string DescriptionText;

	switch(PC.getClass())
	{
		case eSurfer:
			DescriptionText = SurferText;
			break;
		case eStealth:
			DescriptionText = StealthText;
			break;
		case eAkimbo:
			DescriptionText = AkimboText;
			break;
	}
	switch(PC.getWeapon())
	{
		case eRox:
			DescriptionText = ( DescriptionText $ RoxText );
			break;
		case eLink:
			DescriptionText = ( DescriptionText $ LinkText );
			break;
		case eSniper:
			DescriptionText = ( DescriptionText $ SniperText );
			break;
		case eHammer:
			DescriptionText = ( DescriptionText $ HammerText );
			break;
	}
	switch(PC.getKit())
	{
		case eSlowfield:
			DescriptionText = ( DescriptionText $ SlowFieldText );
			break;
		case eShield:
			DescriptionText = ( DescriptionText $ ShieldText );
			break;
		case eLandMine:
			DescriptionText = ( DescriptionText $ LandMineText );
			break;
		case eSkyMine:
			DescriptionText = ( DescriptionText $ SkyMineText );
			break;
	}

	LblDescription.SetValue(DescriptionText);
}

DefaultProperties
{
	RedButton = (R = 0.9, G = 0.1, B = 0.1)
	GreenButton = (R = 0.1, G = 0.9, B = 0.1)
	SurferText = "You're a [ROCKET RIDER]. You live for the thrill of the chase and the surfing of the redeemer. Use your speed and agility to carry you and your team mates through to the objectives. You can wear armour, but be warned it will slow you down."
	StealthText = "You're a [SILENT SENTINEL]. Stealth is your key, remain un-noticed. Press your invisibility to your advantage to defend and assault when least expected, or deploy a surprise for the enemy. You can wear little armour, but have little fear of being slowed down."
	AkimboText = "You're a [DOUBLE DAMAGE]. Fight fire with firepower. You alone can wield much more impressive firepower than your team mates. Bring it to bear and make the enemy feel it. You can wear a lot of armour, and be slowed down, but become a tank."
	RoxText = "You are armed with a [ROCKET LAUNCHER]. The weapon is versatile and ideal for the offensive, especially when assaulting the objective."
	LinkText = "You are armed with a [LINK GUN]. With this, you are in a good position to defend the objective, killing enemies who stray to close, and repairing any damage that may have been done."
	SniperText = "You are armed with a [SNIPER RIFLE]. Use the reach and power of your weapon to take down hardened opponents from afar."
	HammerText = "You are armed with an [IMPACT HAMMER]. Get up close and personal to deliver a fatal blow."
	SlowfieldText = "You carry a [SLOW FIELD GENERATOR]. Place it in a strategic position to hinder enemy movement."
	ShieldText = "You carry a [DEPLOYABLE SHIELD]. Place it in a strategic position to prevent enemy fire."
	LandMineText = "You carry a [LANDMINE]. Leave it somewhere for the enemy to find."
	SkyMineText = "You carry a [SKYMINE]. Leave it somewhere for the enemy to find."
}