package
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.events.MouseEvent;
	
	/**
	 * @author (t)ad
	 *
	 */
	public class blackJackGraphics extends Sprite
	{
		
		/* Anything to do specific with graphics is in this class
		 * This class places graphics on the display, the engine extends this class */
		
		public var background:DisplayObject;
		
		public var cardChoices:Array = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "King", "Queen", "Jack"];
		
		public var cardSpotOne:Sprite;
		public var cardSpotTwo:Sprite;
		public var cardSpotThree:Sprite;
		public var dealerSpot:Sprite;
		
		public var dealButton:Sprite;
		public var hitButton:Sprite;
		
		public var firstSplitButton:Sprite;
		public var secondSplitButton:Sprite;
		
		public var hitSplitOneButton:Sprite;
		public var hitSplitTwoButton:Sprite;
		public var hitSplitThreeButton:Sprite;
		
		public var cardConfigureButton:Sprite;

		public var dealerGo:Sprite;
		
		public var winField:TextField;
		
		public var theGame:tadsBlackJack;
		
		
		public function blackJackGraphics(theGameIs:tadsBlackJack) {
			
			background = new blackJackImages.GameBack();
			
			this.theGame = theGameIs;
			
			winField = makeTXTField(new TextField(), " ");
			
			establishGraphics();
			
		}
		
		private function establishGraphics():void {
			
			theGame.addChild(background);
			cardSpotOne = spriteConfigure( { x:20, y:(400 / 2 - 40) } );
			theGame.addChild(cardSpotOne);
			cardSpotTwo = spriteConfigure( { x:cardSpotOne.x + 135, y:(400 / 2 - 20) } );
			theGame.addChild(cardSpotTwo);
			cardSpotThree = spriteConfigure( { x:cardSpotTwo.x + 135, y:(400 / 2 - 40) } );
			theGame.addChild(cardSpotThree);
			dealerSpot = spriteConfigure( { x:5, y:5 } );
			theGame.addChild(dealerSpot);
			
			var dealTXT:TextField = makeTXTField(new TextField(), "Deal");
			dealButton = spriteConfigure(
				{ x:20, y:330,
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0x7FFF00], [0, 0, dealTXT.width, dealTXT.height]] } );
			dealButton.addChild(dealTXT);
			theGame.addChild(dealButton);
			
			var hitTXT:TextField = makeTXTField(new TextField(), "Hit");
			hitButton = spriteConfigure(
				{ x:dealButton.x + 55, y:330,
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0x0000FF], [0, 0, hitTXT.width, hitTXT.height]] } );
			hitButton.addChild(hitTXT);
			theGame.addChild(hitButton);
			
			var splitTXT:TextField = makeTXTField(new TextField(), "Split");
			firstSplitButton = spriteConfigure(
				{ x:hitButton.x + 65, y:330,
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0xC0C0C0], [0, 0, splitTXT.width, splitTXT.height]] } );
			firstSplitButton.addChild(splitTXT);
			theGame.addChild(firstSplitButton);
			
			var splitAgainTXT:TextField = makeTXTField(new TextField(), "Split Again");
			secondSplitButton = spriteConfigure(
				{ x:firstSplitButton.x + 120, y:330,
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0xC0C0C0], [0, 0, splitAgainTXT.width, splitAgainTXT.height]] } );
			secondSplitButton.addChild(splitAgainTXT);
			theGame.addChild(secondSplitButton);
			
			var hitFirstTXT:TextField = makeTXTField(new TextField(), "Hit First Hand");
			hitSplitOneButton = spriteConfigure(
				{ x:firstSplitButton.x, y:(330 + hitFirstTXT.height + 3),
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0x1E90FF], [0, 0, hitFirstTXT.width, hitFirstTXT.height]] } );
			hitSplitOneButton.addChild(hitFirstTXT);
			theGame.addChild(hitSplitOneButton);
			
			var hitSecondTXT:TextField = makeTXTField(new TextField(), "Hit Second Hand");
			hitSplitTwoButton = spriteConfigure(
				{ x:hitSplitOneButton.x, y:(hitSplitOneButton.y + hitSecondTXT.height + 3),
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0x1E90FF], [0, 0, hitSecondTXT.width, hitSecondTXT.height]] } );
			hitSplitTwoButton.addChild(hitSecondTXT);
			theGame.addChild(hitSplitTwoButton);
			
			var hitThirdTXT:TextField = makeTXTField(new TextField(), "Hit Third Hand");
			hitSplitThreeButton = spriteConfigure(
				{ x:secondSplitButton.x, y:(secondSplitButton.y + hitThirdTXT.height + 3),
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0x1E90FF], [0, 0, hitThirdTXT.width, hitThirdTXT.height]] } );
			hitSplitThreeButton.addChild(hitThirdTXT);
			theGame.addChild(hitSplitThreeButton);


			var cardConfigTXT:TextField = makeTXTField(new TextField(), "Configure Cards");
			cardConfigureButton = spriteConfigure(
				{ x:hitSplitThreeButton.x, y:15,
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0xC0C0C0], [0, 0, cardConfigTXT.width, cardConfigTXT.height]] } );
			cardConfigureButton.addChild(cardConfigTXT);
			theGame.addChild(cardConfigureButton);
			
			var doneTXT:TextField = makeTXTField(new TextField(), "Done");
			dealerGo = spriteConfigure(
				{ x:dealButton.x, y:(dealButton.y + 45),
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0xFC0000], [0, 0, doneTXT.width, doneTXT.height]] } );
			dealerGo.addChild(doneTXT);
			theGame.addChild(dealerGo);
			
		}
		
		public function getIndexFor(card:String):int {
			
			var cardIndex:int;
			
			if (card.match(/[KQJA]{1}/)) {
				if (card == "K") cardIndex = 10;
				if (card == "Q") cardIndex = 11;
				if (card == "J") cardIndex = 12;
				if (card == "A") cardIndex = 0;
			} else {
				cardIndex = int(card) - 1;
			}
			
			return cardIndex;
		}
		
		public function initializePlayerHandGraphics(carderone:String, cardertwo:String, s1:String, s2:String):void
        	{

			var cardOneIndex:int = getIndexFor(carderone);
			var cardTwoIndex:int = getIndexFor(cardertwo);
			
			cardSpotOne.addChild(blackJackImages[cardChoices[cardOneIndex] + s1+"Image"]());
			cardSpotOne.addChild(blackJackImages[cardChoices[cardTwoIndex]+s2+"Image"]());

            		cardSpotOne.getChildAt(0).x = 0;
            		cardSpotOne.getChildAt(0).y = 0;

            		cardSpotOne.getChildAt(1).x = cardSpotOne.getChildAt(0).x + 25;
            		cardSpotOne.getChildAt(1).y = cardSpotOne.getChildAt(0).y - 25;

            		if (cardSpotOne.scaleX == 1.0)
            		{cardSpotOne.scaleX /= 1.1;cardSpotOne.scaleY /= 1.1;}

        	}
		
		
		public function initializeDealerHandGraphics(carderone:String, cardertwo:String, s1:String, s2:String):void
        	{

            		dealerSpot.addChild(new blackJackImages.CardBackImage());
            		var dealSpotIndex:int = getIndexFor(cardertwo);
			
			dealerSpot.addChild(blackJackImages[cardChoices[dealSpotIndex] + s2+"Image"]());

            		dealerSpot.getChildAt(0).x = 0;
            		dealerSpot.getChildAt(0).y = 0;

            		dealerSpot.getChildAt(1).x = dealerSpot.getChildAt(0).x + 105;
            		dealerSpot.getChildAt(1).y = dealerSpot.getChildAt(0).y;

            		if (dealerSpot.scaleX == 1.0)
            		{dealerSpot.scaleX /= 1.80;dealerSpot.scaleY /= 1.80;}

        	}
		
		
		
		public function showHitGraphics(wcard:String, spot:DisplayObjectContainer = null, s1:String = "c" ):void
        	{
            		if (!spot) spot = cardSpotOne;
			
			var wcardIndex:int = getIndexFor(wcard);
			
			spot.addChild(blackJackImages[cardChoices[wcardIndex] + s1+"Image"]());

            		spot.getChildAt(spot.numChildren-1).x =spot.getChildAt(spot.numChildren-2).x + 25;
            		spot.getChildAt(spot.numChildren-1).y =spot.getChildAt(spot.numChildren-2).y - 25;

            		if (spot.scaleX == 1.0)
            		{spot.scaleX /= 1.1;spot.scaleY /= 1.1;}

        	}
		
		
		public function showDealerHitGraphics(card:String, suit:String):void
        	{

			var cardIndex:int = getIndexFor(card);
			
			
			dealerSpot.addChild(blackJackImages[cardChoices[cardIndex] + suit+"Image"]());

            		dealerSpot.getChildAt(dealerSpot.numChildren -1).x =
            		dealerSpot.getChildAt(dealerSpot.numChildren-2).x +105;
            		dealerSpot.getChildAt(dealerSpot.numChildren -1).y =dealerSpot.getChildAt(dealerSpot.numChildren-2).y;

        	}
		
		
		public function showSplitGraphics(bottc:String, topc:String, bottc2:String, topc2:String, spot1:DisplayObjectContainer = null,
					spot2:DisplayObjectContainer = null, s1:String = "c", s2:String = "c", s3:String = "c", s4:String = "c"):void
        	{

			if (!spot1) spot1 = cardSpotOne;
			if (!spot2) spot2 = cardSpotTwo;
			
            		spot1.removeChildAt(spot1.numChildren - 1);
			
			var topcIndex:int = getIndexFor(topc);
			
			spot1.addChild(blackJackImages[cardChoices[topcIndex] + s2+"Image"]());
			

            		spot1.getChildAt(spot1.numChildren-1).x =spot1.getChildAt(spot1.numChildren-2).x + 25;
            		spot1.getChildAt(spot1.numChildren-1).y =spot1.getChildAt(spot1.numChildren-2).y - 25;

			var bottc2Index:int = getIndexFor(bottc2);
			
			spot2.addChild(blackJackImages[cardChoices[bottc2Index] + s3+"Image"]());
			

            		var topc2Index:int = getIndexFor(topc2);
			
			spot2.addChild(blackJackImages[cardChoices[topc2Index] + s4+"Image"]());


            		spot2.getChildAt(0).x = 0;
            		spot2.getChildAt(0).y = 0;

            		spot2.getChildAt(spot2.numChildren-1).x =spot2.getChildAt(spot2.numChildren-2).x + 25;
            		spot2.getChildAt(spot2.numChildren-1).y =spot2.getChildAt(spot2.numChildren-2).y - 25;

            		if (cardSpotTwo.scaleX == 1.0)
            		{cardSpotTwo.scaleX /= 1.1; cardSpotTwo.scaleY /= 1.1; }
			
            		if (cardSpotThree.scaleX == 1.0)
            		{cardSpotThree.scaleX /= 1.1;cardSpotThree.scaleY /= 1.1;}

        	}
		
		
		
		public function spriteConfigure(options:Object = null):Sprite {
			
			var s:Sprite = new Sprite();
			
			if (options.commands && options.args) {
				
				for (var i:int = 0; i < options.commands.length; i++) {
					
					s.graphics[options.commands[i]].apply(s, options.args[i]);
					
				}
				
			}
			
			s.x = (options.x ? options.x : 0);
			s.y = (options.y ? options.y : 0);
			
			return s;
			
		}
		
		
		public function makeTXTField(fieldName:TextField, fieldTXT:String):TextField
		{

            		fieldName = new TextField();
            		fieldName.defaultTextFormat = theGame.brain.format;
            		fieldName.autoSize = TextFieldAutoSize.LEFT;
            		fieldName.height = 20;
            		fieldName.selectable = false;
            		fieldName.text = fieldTXT;
			fieldName.mouseEnabled = false;
            		return(fieldName);

		}


		public function addOrganizer(e:MouseEvent):void {


			if (!theGame.contains(theGame.cardShuteOrganizer)) theGame.addChild(theGame.cardShuteOrganizer);

		}

		
	}
	
}