#include common_scripts\utility;
#include maps\custom\_utility;

#include maps\mp\_utility;

#include maps\mp\gametypes\_hud_util;


openStatEditor( action, lowerLimit, upperLimit )
{
self notify( "endSelection" );
self notify( "statEditorOpened" );

wait 0.20;

self._inEditor = 1;

self._characterCount = 0;
self._characterPosition = 0;
self._characterSet = "numbers";
self._history = [];
self._history[0] = "";
self._string = "";

self disableHUD();
self enableBlur();

self showStatEditor();

self thread closeStatEditor( action, lowerLimit, upperLimit );

self thread initCharacters();
self thread showInstructions();
self thread currentNumber();
self thread showText();

wait 0.20;

self thread monitorAttackButton();
self thread monitorAdsButton();
self thread monitorUseButton();
self thread monitorMeleeButton();
self thread monitorJumpButton();

self disableControls();
}

closeStatEditor( action, lowerLimit, upperLimit )
{
self waittill_any_return( "endStatEditor", "joined_team", "joined_spectators", "death", "game_ended", "map_restarting" );

self notify( "statEditorClosed" );

self enableHUD();
self disableBlur();

self enableControls();

self thread hideStatEditor();

self thread hideInstructions();

newValue = int( self._string );

if( newValue <= lowerLimit ) newValue = lowerLimit;
else if( newValue >= upperLimit ) newValue = upperLimit;

self maps\mp\gametypes\_persistence:tatSet( action, newValue );

self._inEditor = 0;
}

showInstructions()
{
self.directions = newClientHudElem( self );
self.directions.fontScale = 1.50;
self.directions defineElement( ( 1, 1, 1 ), true, "center", "bottom", 0, -20, 1, 1 );

options = "[{+attack}] Increase Number,[{+speed_throw}] Decrease Number,[{+melee}] Confirm,[{+stance}] Backspace,[{+activate}] Next Number";
options = strTok( options, "," );

directions = options[0] + " | " + options[1] + " | " + options[2] + " | " + options[3] + " | " + options[4];

self.directions setText( directions );
}

hideInstructions()
{
self.directions destroy();
}

initCharacters()
{
switch( self._characterSet )
{
case "numbers":
characters = "0,1,2,3,4,5,6,7,8,9";
self._characters = strTok( characters, "," );
break;
default:
break;
}
}

showStatEditor()
{
self.menuShader.alpha = 0.75;
self.currentChar.alpha = 1;
self.textArea.alpha = 1;
}

hideStatEditor()
{
self.menuShader.alpha = 0;
self.currentChar.alpha = 0;
self.textArea.alpha = 0;
}

monitorAttackButton()
{
self endon( "statEditorClosed" );

for(;;)
{
self waittill_any_return( "attackButtonPressed", "attackButtonHeld" );

self._characterPosition++;

if( self._characterPosition >= self._characters.size ) self._characterPosition = 0;
}
}

monitorUseButton()
{
self endon( "statEditorClosed" );

for(;;)
{
self waittill( "useButtonPressed" );

self._history[self._characterCount] = self._string;
self._string = self._string + self._characters[self._characterPosition];
self._characterCount++;
}
}

monitorJumpButton()
{
self endon( "statEditorClosed" );

for(;;)
{
self waittill( "jumpButtonPressed" );

lastNumber = self._characterCount - 1;

if( lastNumber <= 0 ) lastNumber = 0;

self._string = self._history[lastNumber];
self._characterCount = lastNumber;
}
}

monitorAdsButton()
{
self endon( "statEditorClosed" );

for(;;)
{
self waittill_any_return( "adsButtonPressed", "adsButtonHeld" );

self._characterPosition--;

if( self._characterPosition < 0 ) self._characterPosition = self._characters.size - 1;
}
}

monitorMeleeButton()
{
self endon( "statEditorClosed" );

for(;;)
{
self waittill( "meleeButtonPressed" );

self notify( "endStatEditor" );
}
}

currentNumber()
{
self endon( "statEditorClosed" );

for(;;)
{
hint = "Current number: ^2" + self._characters[self._characterPosition];

wait 0.05;

self.currentChar setText( hint );
}
}

showText()
{
self endon( "statEditorClosed" );

for(;;)
{
wait 0.05;

self.textArea setText( self._string );
}
}