package;

import flixel.FlxState;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.tweens.FlxTween;
import flixel.tweens.FlxEase;
import flixel.tweens.motion.CubicMotion;
import flixel.util.FlxTimer;
import flixel.group.FlxGroup;
import flixel.group.FlxGroup.FlxTypedGroup;
import flixel.text.FlxText;
import flixel.util.FlxColor;
import flixel.addons.display.FlxBackdrop;
import flixel.group.FlxSpriteGroup;


class PlayState extends FlxState
{


	private var truck:Truck;
	public var allTheTowns:FlxTypedGroup<Town>;
	public var scoreText:FlxText;
	public var canFeed:Bool = true;
	public var canDestroy:Bool = true;

	public var hotDogText:FlxText;


	// lower speed = faster
	public var townSpeed:Int = 7;

	override public function create():Void
	{
		//FlxG.sound.playMusic(FlxAssets.getSound("assets/music/myMusic"))
		
		// FlxG.sound.playMusic(AssetPaths.music__wav, 1, true);
		//FlxG.sound.playMusic("assets/music/music.wav", 1, true);
		FlxG.sound.play("assets/sounds/engine.wav", .1, true);

		//FlxG.sound.playMusic(FlxAssets.getSound("assets/music/music.wav"));
		FlxG.camera.bgColor = 0xff5b5f73;

		/*var bg:FlxSprite = new FlxSprite(0,0);
		bg.loadGraphic("assets/images/bg_grass.png",false,320,180);
		add (bg);
		bg.scrollFactor.x = 2;*/
		var bg:FlxBackdrop = new FlxBackdrop("assets/images/bg_grass.png", .1, 0, true, false);
		add(bg);

		/*var bg_trail:FlxSprite = new FlxSprite(0,0);
		bg_trail.loadGraphic("assets/images/bg_trail.png",false,320,180);
		add (bg_trail);
		bg_trail.scrollFactor.x = 3;*/
		var bg_trail:FlxBackdrop = new FlxBackdrop("assets/images/bg_trail.png", .25, 0, true, false);
		bg_trail.y += 20;
		add(bg_trail);

		allTheTowns = new FlxTypedGroup();
		add (allTheTowns);
		//allTheTowns.scrollFactor.x = 0;

		//allTheTowns.add(new Town(100,60)); //test
		allTheTowns.add(new Town(-200,70));
		var _firsttownTween:FlxTween = FlxTween.tween(allTheTowns.members[allTheTowns.length - 1], { x: allTheTowns.members[allTheTowns.length - 1].x + 600}, townSpeed, {type: FlxTween.ONESHOT});
		//allTheTowns.add(new Town(-100,98));
		
		/*var explosionGraphic:FlxSprite = new FlxSprite(270, 120);
		explosionGraphic.loadGraphic("assets/images/explosion.png",true,121,77);
		explosionGraphic.animation.add("explode",[0,1,2,3,4,5,6,7,8],24,true);
		explosionGraphic.animation.play("explode");
		add(explosionGraphic);
		explosionGraphic.scrollFactor.x = 0;*/


		new FlxTimer().start(townSpeed/2,0).onComplete = function(t:FlxTimer):Void
			{	
				allTheTowns.add(new Town(-200,70));
				var _townTween:FlxTween = FlxTween.tween(allTheTowns.members[allTheTowns.length - 1], { x: allTheTowns.members[allTheTowns.length - 1].x + 600}, townSpeed, {type: FlxTween.ONESHOT});
				
			
			}

		/*new FlxTimer().start(.2).onComplete = function(t:FlxTimer):Void
			{
				allTheTowns.add(new Town(0,98));
				allTheTowns.add(new Town(100,108));
				allTheTowns.add(new Town(200,98));
			}*/

		truck = new Truck(200, 121);
		add(truck);
		truck.scrollFactor.x = 0;

		var _truckTween:FlxTween = FlxTween.tween(truck, { y: truck.y + 2}, .25, {type: FlxTween.PINGPONG, ease: FlxEase.sineInOut });

		var scoreBox = new FlxSprite(0,0);
		scoreBox.makeGraphic(360,15, 0xff2d2a21, false);
		add(scoreBox);
		scoreBox.scrollFactor.x = 0;

		scoreText = new FlxText (2, 2, 0, "", 30);
		scoreText.text = "Score: " + Reg.score;
		scoreText.setFormat("assets/fonts/ChevyRay - Express.ttf", 9, FlxColor.WHITE, LEFT);
		add(scoreText);	
		scoreText.scrollFactor.x = 0;

		hotDogText = new FlxText (FlxG.width-150, 2, 150, "", 30);
		hotDogText.text = "Hot Dogs: " + Reg.hotdogs;
		hotDogText.setFormat("assets/fonts/ChevyRay - Express.ttf", 9, FlxColor.WHITE, RIGHT);
		add(hotDogText);	
		hotDogText.scrollFactor.x = 0;


		super.create();

	}

	override public function update(elapsed:Float):Void
	{

		super.update(elapsed);

		scoreText.text = "Score: " + Reg.score;
		hotDogText.text = "Hot Dogs: " + Reg.hotdogs;

		FlxG.camera.scroll.add(-5, 0);

		if (FlxG.keys.justPressed.H)
		{
		FlxG.log.add("pressed H");	
			if (canFeed == true)
			{
				FlxG.overlap(truck, allTheTowns, feedTown);
			}
			canFeed = true;
		}

		if (FlxG.keys.justPressed.D)
		{		
			if (canDestroy == true)
				{
					FlxG.overlap(truck, allTheTowns, destroyTown);
				}
			canDestroy = true;
		}	

	}

	private function feedTown(P:FlxSprite, T:Town):Void
	{
		FlxG.log.add("feeding town");

		if (canFeed == false)
		{
			return;
		}


		if (P.alive && P.exists && T.alive && T.exists && Reg.hotdogs > 0)
	    {
	        T.hotDogStock += 1;
	        Reg.hotdogs -= 1;
	        Reg.score += 1;

	        canFeed = false;
	        throwHotDog();
	        //T.feed();
	    }

	}

	private function destroyTown(P:FlxSprite, T:Town):Void
	{
		FlxG.log.add("destroying town");

		if (canDestroy == false)
		{
			return;
		}

		if (P.alive && P.exists && T.alive && T.exists)
	    {
	       explosion(T);
	       T.kill();
	       canDestroy = false;
	    }
	}

	private function throwHotDog():Void
	{
		FlxG.sound.play("assets/sounds/hit" + FlxG.random.int(1,4) + ".wav", .5);
		//FlxG.sound.play(AssetPaths.hit + FlxG.random.int(1,4) + ".wav");
		var hotDog:FlxSprite = new FlxSprite(270, 120);
		hotDog.loadGraphic("assets/images/hotdog" + FlxG.random.int(1,4) + ".png",false,10,15);
		add(hotDog);
		hotDog.scrollFactor.x = 0;
		var _hotDogTween:FlxTween = FlxTween.tween(hotDog, { y: hotDog.y -150}, .5, {type: FlxTween.ONESHOT, ease: FlxEase.sineInOut });

	}

	private function explosion(T:Town):Void
	{

		FlxG.sound.play("assets/sounds/explode1.wav", .7);
		//var explosionGraphic:FlxSprite = new FlxSprite(208, 40);
		var explosionGraphic:FlxSprite = new FlxSprite(T.x - 58, T.y - 35);
		explosionGraphic.loadGraphic("assets/images/explosion.png",true,121,77);
		explosionGraphic.animation.add("explode",[1,2,3,4,5,6,7,8,0],24,false);
		explosionGraphic.animation.play("explode");
		add(explosionGraphic);
		explosionGraphic.scrollFactor.x = 0;


		new FlxTimer().start(.1,1).onComplete = function(t:FlxTimer):Void
			{
				new FlxTimer().start(.1,8).onComplete = function(t:FlxTimer):Void
				{
					FlxG.sound.play("assets/sounds/hit" + FlxG.random.int(1,4) + ".wav", .25);
				}
				var hotDogRain:FlxSprite = new FlxSprite(220, -77);
				hotDogRain.loadGraphic("assets/images/hotdograin.png",false,69,77);
				add(hotDogRain);
				hotDogRain.scrollFactor.x = 0;
				var _hotDogTween:FlxTween = FlxTween.tween(hotDogRain, { y: 180}, 1, {type: FlxTween.ONESHOT, ease: FlxEase.sineInOut });
			}
		//var _hotDogTween:FlxTween = FlxTween.tween(hotDog, { y: hotDog.y -150}, .5, {type: FlxTween.ONESHOT, ease: FlxEase.sineInOut });
	}



}
