package com.Tutorial 
{
    import com.adamatomic.flixel.*;
	
	/**
	 * ...
	 * @author Stephen Lavelle
	 */
	public class ChooseDieState extends FlxState
	{ 
		public static var showInteract:Boolean = false;
		
        public static var lyrMain:FlxLayer;

		public static var inventory:Boolean;
		
		private static var curselected:int;
		
		private static var curtop:int;
		
		public static var chosendie:int;
		
		private var _names:Array;
		
		private var _npc:NPC;
		
		private var _listtext:FlxText;
		
		private var _desc:FlxText;
		private var _owner:FlxText;
		
		public function ChooseDieState()
		{
			_names = new Array();
			for (var i:int = 0; i < 33; i++)
			{
				if (FlxG.hasdie[i])
				{
					var npc:NPC;
					npc = new NPC(0, 0, i);				
					_names.push(npc.diename);
				}
			}
			
			curselected = 0;
			curtop = 0;
			
			lyrMain = new FlxLayer();

			
			lyrMain.add(new FlxSprite(null, 0, 0, false, false, FlxG.width, 16, 0xff8d431f));
			lyrMain.add(new FlxSprite(null, 0, 81, false, false, FlxG.width, 1, 0xff8d431f));
			lyrMain.add(new FlxSprite(InspectState.GfxSmallFrame, 10, 85));
			lyrMain.add(new FlxSprite(InspectState.GfxDice, 10+2, 85+2));
			lyrMain.add(new FlxSprite(InspectState.GfxSmallFrame, 130, 85));
			calcdie();
			_npc = new NPC(130 + 2, 85 + 2, chosendie);
			lyrMain.add(_npc);
			
			_desc = new FlxText(34, 82, 200, 200, _npc.description(), 0xff8d431f);
			lyrMain.add(_desc);
			
			_owner = new FlxText(0, 107, FlxG.width-4, 8,_npc.ownername, 0xff8d431f,null,8,"right");
			lyrMain.add(_owner);
			
			var npc2:NPC;
			npc2 = new NPC(0, 0, WalkState.toInspect);
			if (!inventory)
				lyrMain.add(new FlxText(16, 2, 300, 20, "Choose Die vs "+npc2.nums, 0xfff36f36)	);
			else
				lyrMain.add(new FlxText(16, 2, 100, 8, "Inventory ("+ BattleState.dieCount+"/32)", 0xfff36f36));
			
			
			_listtext = new FlxText(11, 20, 100, 60, dielist() , 0xff8d431f);
			lyrMain.add(_listtext);			
						
			this.add(lyrMain);
		}
		
		private function dielist():String
		{
			var s:String;
			s = new String();

			if (curselected >= 2)
				curtop = curselected - 2;
			else
				curtop = 0;
				
			for (var i:int = curtop; i < _names.length; i++)
			{
				if (curselected == i)
					s += ">";
				else
					s += " ";
				s += _names[i] + "\n";
			}
			return s;
		}
		
		public function calcdie():void
		{
			chosendie = -1;
			var j:int = -1;
			for (var i:int = 0; i < 33; i++)
			{
				if (FlxG.hasdie[i])
				{
					j++;
				}
				if (j == curselected)
				{
					chosendie = i;

					break;
				}
			}
		}
		override public function update():void
		{
			if (FlxG.justPressed(FlxG.A) )
			{
				if (inventory)
					FlxG.switchState(WalkState);
				else
				{
					calcdie();
					
					FlxG.switchState(BattleState);
				}
			}
			if (FlxG.justPressed(FlxG.B) )
			{
				if (!inventory)
					showInteract = true;
				FlxG.switchState(WalkState);
			}
			if (FlxG.justPressed(FlxG.UP))
			{
				curselected = (curselected - 1 + _names.length) % _names.length;
				_listtext.setText(dielist());
				
				calcdie();
				trace("chosendie", chosendie);
				_npc.changeframe(chosendie);
				_npc.specificFrame(chosendie);
				_npc.update();
				_desc.setText(_npc.description());
				_owner.setText(_npc.ownername);
				
			}
			
			if (FlxG.justPressed(FlxG.DOWN))
			{
				curselected = (curselected +1) % _names.length;		
				_listtext.setText(dielist());	
				
				calcdie();
				_npc.changeframe(chosendie);
				_npc.specificFrame(chosendie);
				_npc.update();
				_desc.setText(_npc.description());
				_owner.setText(_npc.ownername);
				_desc.update();
				_owner.update();
			}
		}
	}

}