package ;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.*;
import openfl.Assets;
import flixel.ui.FlxButton;

class FcButton extends flixel.group.FlxGroup
{
	private var btn:FlxButton;

	public static var TOP:String = "top";
	public static var BOTTOM:String = "bottom";
	public static var LEFT:String = "left";
	public static var RIGHT:String = "right";

	public static var CENTER_BOTH:String = "center_both";
	public static var CENTER_X:String = "center_x";
	public static var CENTER_Y:String = "center_y";
	public static var CENTER_BOTTOM:String = "center_bottom";
	public static var CENTER_TOP:String = "center_top";
	public static var LEFT_TOP:String = "left_top";
	public static var RIGHT_TOP:String = "right_top";
	public static var RIGHT_BOTTOM:String = "right_bottom";

	public var y:Float;
	public var x:Float;
	public var width:Int;
	public var height:Int;
	private var callback:Void->Void;

    public function new(X:Float=0, Y:Float=0, path:String, _callback:Void->Void, ?align:String)
    {
        super();	        

        callback = _callback;
        // Sprite is loaded simply to get the dimensions
        var sprite = new FlxSprite();
		sprite.loadGraphic( path );		

		// Add some stuff that FlxGroup doesn't have
		x = X;
        y = Y;
        width = Math.round( sprite.width / 3 );
        height = Math.round( sprite.height );

		// Button
		btn = new FlxButton( Math.round(X), Math.round(Y), "", onClick );
		btn.loadGraphic( path, false, width, height );
		//btn.y = FlxG.height - playBtn.height - Reg.ad_height * Reg.density_scale;
		//btn.x = FlxG.width / 2 - playBtn.width / 2;

		if (align != null) {
			set_align(align);
		}

		this.add(btn);
		sprite.kill();	

	}

	private function onClick():Void 
	{
		callback();
	}

	public function set_align(align:String) {
		if (align == CENTER_BOTH) {
			set_align( CENTER_X );
			set_align( CENTER_Y );
		}
		else if (align == BOTTOM) {
			btn.y = Math.round( btn.y - (btn.height) );
		}
		else if (align == TOP) {
			// nothing
		}
		else if (align == LEFT) {
			// nothing
		}
		else if (align == RIGHT) {
			btn.x = Math.round( btn.x - (btn.width) );
		}
		else if (align == CENTER_X) {
			btn.x = Math.round( btn.x - (btn.width / 2) );
		}
		else if (align == CENTER_Y) {
			btn.y = Math.round( btn.y - (btn.height / 2) );
		}
		else if (align == LEFT_TOP) {			
			set_align( LEFT );
			set_align( TOP );
		}
		else if (align == RIGHT_TOP) {
			set_align( RIGHT );
			set_align(TOP);
		}
		else if (align == RIGHT_BOTTOM) {
			set_align( RIGHT );
			set_align(BOTTOM);
		}
		else if (align == CENTER_BOTTOM) {
			set_align( CENTER_X );
			set_align( BOTTOM );
		}
		else if (align == CENTER_TOP) {
			set_align( CENTER_X );
			set_align( TOP );
		}		
	}
}