/*
 * A drag and drop class for flixel. All you have to do is implement
 * DraggableFlxSprite instead of FlxSprite and voila, it's draggable.
 * 
 * Drag&drop done easy for flixel 2
 * */
package util
{
	import flash.events.MouseEvent;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;
	
	public class DraggableFlxSprite extends FlxSprite
	{
		public var dragging:Boolean;
		public var _type:String;
		private var _currentSelection: String;
		private var _originalX:Number;
		private var _originalY:Number;
		private var _destinyXmin:Number;
		private var _destinyYmin:Number;
		private var _destinyXmax:Number;
		private var _destinyYmax:Number;
		
		public function DraggableFlxSprite(type:String="",X:Number=0,Y:Number=0,SimpleGraphic:Class=null, dXmin:Number=0,dYmin:Number=0, dXmax:Number=640,dYmax:Number=480) 
		{
			super(X, Y, SimpleGraphic);
			FlxG.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);			
			FlxG.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_originalX = X;
			_originalY = Y;
			_destinyXmin = dXmin;
			_destinyYmin = dYmin;
			_destinyXmax = dXmax;
			_destinyYmax = dYmax;
			_type = type;
		}
		
		public override function update():void
		{
			if (this.dragging) {
				this.x = FlxG.mouse.screenX - this.width / 2;
				this.y = FlxG.mouse.screenY - this.height / 2;
			}
			
			super.update();
		}
		
		protected function onMouseDown(event:MouseEvent):void
		{
			// I don't know if this check is important - copied it from FlxButton
			if (!exists || !visible || !active) {
				return;
			}
			_currentSelection = "";
			if (overlapsPoint(new FlxPoint(FlxG.mouse.x, FlxG.mouse.y))) {
				this.dragging = true;
				_currentSelection = _type;
				FlxG.log(_currentSelection);
			}
		}
	
		protected function onMouseUp(event:MouseEvent):void
		{
			// I don't know if this check is important - copied it from FlxButton
			if (!exists || !visible || !active || !FlxG.mouse.justReleased()) {
				return;
			}
			
			this.dragging = false;
			this.x = _originalX;
			this.y = _originalY;
			
			if (FlxG.mouse.x > _destinyXmin && FlxG.mouse.x < _destinyXmax && FlxG.mouse.y > _destinyYmin && FlxG.mouse.y < _destinyYmax)
			{
				if (_currentSelection == "pot"){
					if(Registry.inventory.getPotions()> 0){
						Registry.inventory.removePotion();
						if (Registry.inventory.getPotions() == 0)
							this.kill();
					}
					else
						this.kill();
					return;
				}
				if (_currentSelection == "spot"){
					if(Registry.inventory.getSuperPotions()> 0){
						Registry.inventory.removeSuperPotion();
						if (Registry.inventory.getSuperPotions() == 0)
							this.kill();
					}
					else
						this.kill();
					return;
				}
				if (_currentSelection == "ppower"){
					if(Registry.inventory.getPlusPowers()> 0){
						Registry.inventory.removePlusPower();
						if (Registry.inventory.getPlusPowers() == 0)
							this.kill();
					}
					else
						this.kill();
					return;
				}
				if (_currentSelection == "defender"){
					if(Registry.inventory.getDefenders()> 0){
						Registry.inventory.removeDefender();
						if (Registry.inventory.getDefenders() == 0)
							this.kill();
					}
					else
						this.kill();
					return;
				}
				
			}
			
		}
	}
}