package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.math.FlxMath;
import flixel.math.FlxPoint;
import flixel.util.FlxColor;
import flixel.math.FlxAngle;
import flixel.tweens.FlxTween;
import flixel.tweens.FlxEase;
import flixel.util.FlxTimer;
import flixel.FlxObject;
import flixel.group.FlxSpriteGroup;

/**
 * A FlxState which can be used for the actual gameplay.
 */
class Town extends FlxSprite
{
	/**
	 * Function that is called up when to state is created to set it up. 
	 */

	//public var townSprite:FlxSpriteGroup;
	public var townName:String;
	public var population:Int;
	public var hotDogStockText:FlxText;
	public var hotDogStock: Int;
	public var nameText: FlxText;
	//public var hotDogStockText: FlxText;
	
	//public var gamepad:FlxGamepad;


	 public function new(X:Float=0, Y:Float=0)
     {
        super(X, Y);

        scrollFactor.x = 0;

        generateName();
        generateHotDogs();




		loadGraphic("assets/images/pin.png",false,29,39);
		setSize(49, 80);

		nameText = new FlxText (this.x - 86, this.y - 30, 200, "", 30);
		//var nameText = new FlxText (-86-200, -30-70, 200, "", 30);
		//var nameText = new FlxText (-86, -30, 200, "", 30);
		//-200,70
		nameText.text = townName;
		nameText.setFormat("assets/fonts/ChevyRay - Tentown.ttf", 10, FlxColor.WHITE, CENTER);
		FlxG.state.add(nameText);
		nameText.scrollFactor.x = 0;
		//FlxG.log.add("NameText Pos: (" + nameText.x + "," + nameText.y + ")");

		hotDogStockText = new FlxText (this.x - 60, this.y - 15, 150, "", 30);
		hotDogStockText.text = "Needs: " + hotDogStock + " Hot Dogs";
		hotDogStockText.setFormat("assets/fonts/ChevyRay - Express.ttf", 9, FlxColor.WHITE, CENTER);
		FlxG.state.add(hotDogStockText);	
		hotDogStockText.scrollFactor.x = 0;
		FlxG.log.add("HotDogText Pos: (" + hotDogStockText.x + "," + hotDogStockText.y + ")");

		/*var collider = new FlxSprite(-10,0);
		collider.makeGraphic(49, 80, FlxColor.TRANSPARENT, false);
		townSprite.add(collider);*/
      
 	}

 	 override public function update(elapsed:Float):Void
 	{
     	super.update(elapsed);

     	if ( FlxG.keys.justPressed.UP)
		{
			hotDogStockText.size += 1;
		}

		nameText.x = this.x - 86;
		hotDogStockText.x = this.x - 60;

		if (hotDogStock < 0)
		{
			hotDogStockText.text = "Needs: " + -hotDogStock + " Hot Dogs";
		}


		else if (hotDogStock > 0)
		{
			hotDogStockText.text = "Has: " + hotDogStock + " Hot Dogs";
		}

		else if (hotDogStock == 0)
		{
			hotDogStockText.text = "We got enough hot dogs!";
			

		}


 	}

 	override public function kill():Void
	{
		if (hotDogStock > 0)
		{
	    Reg.hotdogs += hotDogStock;
	    Reg.score -= Math.round(hotDogStock/2);
		}

	    
	    kill2();

	}

	public function kill2():Void
	{

		new FlxTimer().start(.1).onComplete = function(t:FlxTimer):Void
			{
				hotDogStockText.alive = false;
				hotDogStockText.exists = false;
				nameText.alive = false;
				nameText.exists = false;
				alive = false;
	    		exists = false;
			}
		
	}

 	private function generateHotDogs():Void
 	{
 		//var p = new Point(-1, 65);
 		var coinFlip = FlxG.random.bool(50);

 		if (coinFlip == true)
 		{
 		hotDogStock = FlxG.random.int(1,10);
 		}

 		else if (coinFlip == false)
 		{
 		hotDogStock = FlxG.random.int(-10, -1);
 		}

 	}

	private function generateName():Void
		{
			//var randNum = FlxG.random.int(0,3);

			var arrayOfPrefixes = ["New ", "Neo ", "Fort ", "Old ", "O'"];

			var arrayOfFirstParts = ["Fart", "Barf", "Poo", "Tinkel", "Toil", "Boog", "Toot", "Scum", "Piss",
									 "Taint", "Gooch", "Dook", "Bonk", "Shat", "Scat", "Turd", "Ding", "Dung", "Crap", "Colon", "Plop", "Duck", "Dump", "Tush", "Burp", "Belch", ];

			var arrayOfSecondParts = ["ing", "er", "nana", "son", "i", "ain", "wich", "a", "le", "caca", "ick", "en", "ex", "ui", "ine", "um", "co", "lin", "o",];

			var arrayOfThirdParts = ["ton", "burg", " City", "field", "town", " Ridge", "boro", " Town", "land", "istan", "bourg", "delphia", "topia", " Point", " Pass", "ville", "shire", "bury", "opolis"];

			////

			var prefix = "";
			var secondPart = "";
			var firstPart = arrayOfFirstParts[FlxG.random.int(0, arrayOfFirstParts.length-1)];
			var thirdPart = arrayOfThirdParts[FlxG.random.int(0, arrayOfThirdParts.length-1)];

			//if (randNum <= 1)
			//{
			//var prefix = arrayOfPrefixes[FlxG.random.int(0, arrayOfPrefixes.length-1)];
			//}
			
			//else if (randNum >= 2)
			//{
			var secondPart = arrayOfSecondParts[FlxG.random.int(0, arrayOfSecondParts.length-1)];
			//}
			//else{}

			

			townName = "" + prefix + firstPart + secondPart + thirdPart;

		}


}