package com.Tutorial
{
    import com.adamatomic.flixel.*;
	
    public class PlayState extends FlxState
    {
		[Embed(source = '../../data/horizontal.png')] private var ImgTiles:Class;
		[Embed(source = '../../data/npcs.png')] private var ImgNPCs:Class;
        [Embed(source = '../../data/mainmap.txt', mimeType = "application/octet-stream")] private var DataMap:Class;
        [Embed(source = '../../data/spritelocs.txt', mimeType = "application/octet-stream")] private var NPCMap:Class;
        [Embed(source = '../../data/posterpositions.txt', mimeType = "application/octet-stream")] private var PosterMap:Class;
     
		[Embed(source="../../data/blip.mp3")] private var SndBlip:Class;
		
        private var _player:Player;
		private var _printer:Printer;
		private var _posterCount:int;
		private var _setPosters:Boolean;
		
		private var _conversation:Array;
		
        private var _map:FlxTilemap;
        private var _npcmap:FlxTilemap;
		
		private var _posters:FlxArray;
		
		private var _hudNote:FlxSprite;
		private var _noteCount:FlxText;
		
		private var _convLabel:FlxText;
		
		private var _doorway:Door;
		
        public static var lyrStage:FlxLayer;
        public static var lyrSprites:FlxLayer;
        public static var lyrHUD:FlxLayer;

        public function PlayState():void
        {			
            super();
			
			_conversation = new Array;
			
			_posterCount = 0;
			
            lyrStage = new FlxLayer;
			lyrSprites = new FlxLayer;
            lyrHUD = new FlxLayer;
            
            _player = new Player(31*21, 19*21);
			
			_printer = new Printer(33*21, 18*21);
					
			_posters = new FlxArray;
			
            FlxG.follow(_player,5);
           // FlxG.followAdjust(0.5, 0.5);
            FlxG.followBounds(0,0,840,567);
            			
			_noteCount = new FlxText(4, 2, 48, 16, _posterCount.toString(), 0xff000000, null, 16, "left");
            _noteCount.scrollFactor.x = _noteCount.scrollFactor.y = 0;            
			lyrHUD.add(_noteCount);
			
			_convLabel = new FlxText(0, 60,150, 120, "", 0xff000000, null, 16, "center");
            _convLabel.scrollFactor.x = _convLabel.scrollFactor.y = 0;            
			lyrHUD.add(_convLabel);
		
            _map = new FlxTilemap(new DataMap, ImgTiles, 59);
            lyrStage.add(_map);        
			
            _npcmap = new FlxTilemap(new NPCMap, ImgNPCs, 2);
            lyrStage.add(_npcmap);    
			
			//gross note stuff begin
			var widthInTiles:int = 0;
			var heightInTiles:int = 0;
			var tempPoster:FlxArray = new FlxArray;
			
			var sPosterMap:String = new PosterMap;
			
			var c:uint;
			var cols:Array;
			var rows:Array = sPosterMap.split("\n");
			heightInTiles = rows.length;
			for(var r:uint = 0; r < heightInTiles; r++)
			{
				cols = rows[r].split(",");
				if(cols.length <= 1)
				{
					heightInTiles--;
					continue;
				}
				if(widthInTiles == 0)
					widthInTiles = cols.length;
				for(c = 0; c < widthInTiles; c++)
					tempPoster.push(uint(cols[c]));
			}

			for (c = 0; c < tempPoster.length; c++)
			{
				if (tempPoster[c] != 0)
				{
					_posters.add(lyrSprites.add(new Poster(21 * (c % widthInTiles), 21 * (Math.floor(c / widthInTiles)) )));
					trace(21 * (c % widthInTiles), 21 * (Math.floor(c / widthInTiles)));
				}
			}
			
			//gross note stuff end
			
            lyrSprites.add(_printer);
            lyrSprites.add(_player);

			_doorway = new Door(32 * 21, 20 * 21);
			lyrSprites.add(_doorway);
			
            this.add(lyrStage);
            this.add(lyrSprites);
            this.add(lyrHUD);
        }
        
		private function cando():int
		{
			var nResult:int = 0;
			switch (_player.dir)
				{
					case 0:
						_player.y -= 8;
					break;
					case 1:
						_player.y += 8;
					break;
					case 2:
						_player.x -= 8;
					break;
					case 3:
						_player.x += 8;
					break;
				}

				if (_npcmap.overlaps(_player))
				{
					nResult = 2;
				}
				else if (_printer.overlaps(_player))
				{
					nResult = 1;					
				}
				
				switch (_player.dir)
				{
					case 0:
						_player.y += 8;
					break;
					case 1:
						_player.y -= 8;
					break;
					case 2:
						_player.x += 8;
					break;
					case 3:
						_player.x -= 8;
					break;
				}
				return nResult;
		}
		
		private function addPoster(a:FlxCore, b:FlxCore):void
		{
			if (_posterCount > 0 && !a.visible && !_setPosters)
			{
				a.visible = true;
				_posterCount--;
				_noteCount.setText(_posterCount.toString());
				FlxG.play(SndBlip, 0.1);					
				_setPosters = true;
			}
		}
		
		public function randomNumber(low:int, high:int):int
		{
		  return Math.round(Math.random() * (high - low)) + low;
		}
		
		private function updateConvText():void
		{
			if (_conversation.length > 0)
			{
					
				switch (_conversation.length)
				{
					case 3:
					case 4:
					case 6:
					case 7:
					_convLabel.setColor(0xff00ff00);
					break;
					default:
					_convLabel.setColor(0xff0000ff);
					break;
				}
				_convLabel.setText(_conversation[0]);
			}
			else 
			{
				_convLabel.setText("");
			}
		}
        override public function update():void
        {					
			
			if (_conversation.length > 0)
			{
				
				if ((FlxG.justPressed(FlxG.A) || FlxG.justPressed(FlxG.B)))
				{
					_conversation = _conversation.slice(1);
					updateConvText();
					FlxG.play(SndBlip, 0.1);
				}
			
				return;
			}
			
            super.update();
            _map.collide(_player);
            _npcmap.collide(_player);
			_printer.collide(_player);
		    //FlxG.overlapArray(_k, _p, KeyHit);
		    //FlxG.overlapArray(_d, _p, DoorHit);
			   
			
			var nDoResult:int = cando();
				
			if ((FlxG.justPressed(FlxG.A) || FlxG.justPressed(FlxG.B)))
			{
				trace(nDoResult);
				//action!
				if (nDoResult == 1)
				{
					if (_printer.hasPaper())
					{
						_printer.getPaper();
						_posterCount += 3;
						_noteCount.setText(_posterCount.toString());
					}
					else
					{
						_printer.doPrint();
					}
				}
				else if (nDoResult == 2)
				{
					FlxG.play(SndBlip, 0.1);
					
					_conversation.push("Excuse me,");
					_conversation.push("You haven't\nseen\nthis boy,\nhave you?");
					
					switch(randomNumber(0, 2))
					{
						case 0:
						_conversation.push("No,");
						break;
						case 1:
						_conversation.push("Hmm...");
						break;
						case 2:
						_conversation.push("Let me think...");
						break;
					}
					
					switch(randomNumber(0, 3))
					{
						case 0:
						_conversation.push("I don't think\nI have.");
						break;
						case 1:
						_conversation.push("Don't think so,\nno.");
						break;
						case 2:
						_conversation.push("I don't really\nrecall.");
						break;
						case 3:	
						_conversation.push("I haven't seen\nhim.");
						break;					
					}
					
					_conversation.push("He might be\na little taller\nnow.");
					switch(randomNumber(0, 2))
					{
						case 0:
					_conversation.push("No, I don't\nthink I have,");
						break;
						case 1:
						_conversation.push("It's not ringing\na bell,");
						break;
						case 2:
						_conversation.push("I really\ncouldn't say,");
						break;
					}
					switch(randomNumber(0, 1))
					{
						case 0:
						_conversation.push("Sorry.");
						break;
						case 1:
						_conversation.push("Apologies.");
						break;
					}
					_conversation.push("That's okay - ");
					_conversation.push("Thank you for\nyour time.");
					
					updateConvText();
				}
				else if (nDoResult == 0)
				{	
					switch (_player.dir)
					{
						case 0:
							_player.y -= 8;
						break;
						case 1:
							_player.y += 8;
						break;
						case 2:
							_player.x -= 8;
						break;
						case 3:
							_player.x += 8;
						break;
					}
					_setPosters = false;
					FlxG.overlapArray(_posters, _player, addPoster);	
					switch (_player.dir)
					{
						case 0:
							_player.y += 8;
						break;
						case 1:
							_player.y -= 8;
						break;
						case 2:
							_player.x += 8;
						break;
						case 3:
							_player.x -= 8;
						break;
					}
												
				}
				
				
			}
		}		
		
    }    
}