package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxMath;
import flash.Lib;
import flash.system.Capabilities;
import flash.net.URLRequest;

/**
 * A FlxState which can be used for the game's menu.
 */
class MainMenuState extends FlxState
{

	private var clicked = false;

	/**
	 * Function that is called up when to state is created to set it up. 
	 */
	override public function create():Void
	{		
		// Camera bg colour
		FlxG.camera.bgColor = 0x5b103c;

		var bg = new FlxSprite(0, 0);		
		bg.loadGraphic(Reg.assets_dir + "menuBg.png");
		bg.x = FlxG.width/2 - bg.width/2;
		bg.y = FlxG.height/2 - bg.height/2;

		// Scale logo if asset is larger than device width
		var max_width = FlxG.width * 0.8;
		if (bg.width > max_width) {
			 bg.setGraphicSize( Math.round(max_width), -1 );
		}
		add(bg);

		var friendlyBtn:FcButton = new FcButton( FlxG.width - Reg.margin , FlxG.height - Reg.margin, Reg.assets_dir + "friendlyCatLogo.png", onFriendly, FcButton.RIGHT_BOTTOM );
		add(friendlyBtn);

		var bgBtn:FlxButton = new FlxButton( 0, 0, "", playGame );
		bgBtn.width = FlxG.width;
		bgBtn.height = FlxG.height;
		bgBtn.alpha = 0;
		add(bgBtn);

		// Fade in
		FlxG.camera.fade(0x5b103c, .2, true);

		super.create();
	}

	private function playGame():Void
	{
		if (!clicked) {
			FlxG.camera.fade(0xFF73c6b0, .2, false, function() {
	        	FlxG.switchState(new PlayState());
			});
		}
	}	
	private function onFriendly():Void
	{
		clicked = true;
		Lib.getURL( new URLRequest ("www.friendlycat.com.au") );
	
	}

	/**
	 * Function that is called when this state is destroyed - you might want to 
	 * consider setting all objects this state uses to null to help garbage collection.
	 */
	override public function destroy():Void
	{
		super.destroy();
	}

	/**
	 * Function that is called once every frame.
	 */
	override public function update():Void
	{		
		super.update();

		if (FlxG.keys.justReleased.ANY) 
		{
			playGame();
		}

		clicked = false;
	}	
}