package {
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;
	
	//класс вида для игры
	//управляет видом игровой координатной сеткой
	internal class View {
		
		public var _clip:Sprite;
		private var _width:uint;
		private var _height:uint;
		private var _sprites:Object;
		private var _size:uint;
		private var _cells:Array;
		
		public var status_x:Number;
		public var status_y:Number;
		
		/**
		* Конструктор
		* @param clip Sprite объект отображения
		* @param playWidth uint ширина игрового поля
		* @param playHeight uint высота игрового поля
		* @param size uint размер одной ячейки
		* @param sprites Object объект спрайтов фигур
		*/
		public function View(clip:Sprite, playWidth:uint, playHeight:uint, size:uint, sprites:Object) {
			_clip = clip;
			_width = playWidth;
			_height = playHeight;
			_size = size;
			_sprites = sprites;
			
			_cells = new Array();

			//заполняем игровое поле
			for(var i:uint = 0; i<_width; i++) {
				
				_cells[i] = new Array();
				for(var j:uint = 0; j<_height; j++) {
					//создаем ячейку
					var cell:Sprite = new Sprite();
					cell.x = _size * i + (_size * 0.5);
					cell.y = _size * j + (_size * 0.5);
					
					var mask:Sprite = new Sprite();
					mask.graphics.beginFill(0);
					mask.graphics.drawRect(0, 0, _size, _size);
					mask.x = _size * i;
					mask.y = _size * j;
					
					_clip.addChild(cell);
					_clip.addChild(mask);
					
					cell.mask = mask;
					_cells[i][j] = cell;
				}
				
			}
		}
		private function clearCell( cell:Sprite ):void
		{
			while ( cell.numChildren )
			{
				cell.removeChildAt( 0 );
			}
		}
		internal function clearCells():void
		{
			for ( var i:uint = 0; i < _width; ++i )
			{
				for ( var j:uint = 0; j < _height; ++j )
				{
					clearCell( _cells[i][j] );
				}
			}
		}
		
		internal function onUpdateCells( ev:TetrisEvent ):void
		{
			var offset:Number = _size * 0.5;
			for ( var i:uint = 0; i < ev.cells.length; ++i )
			{
				var a:Cell = ev.cells[i];
				if ( a.state.shape == Shape.CLEAR )
				{
					clearCell( _cells[a.location.x][a.location.y] );
				}
				else
				{
					clearCell( _cells[a.location.x][a.location.y] );
					var id:Class = getDefinitionByName( _sprites[ a.state.shape.label ] ) as Class;
					var clip:Sprite = new id();
					
					
					
					status_y = a.location.y;
					
					_cells[a.location.x][a.location.y].addChild( clip );
					switch( a.state.shape )
					{
						case Shape.I:
							switch( a.state.number )
							{
								case 0:
									clip.x = - offset;
									clip.y = - offset;
									break;
								case 1:
									clip.x = - _size - offset;
									clip.y = - offset;
									break;
								case 2:
									clip.x = - 2 * _size - offset;
									clip.y = - offset;
									break;
								case 3:
									clip.x = - 3 * _size - offset;
									clip.y = - offset;
									break;
							}
							break;
						case Shape.O:
							switch( a.state.number )
							{
								case 0:
									clip.x = - offset;
									clip.y = - offset;
									break;
								case 1:
									clip.x = - _size - offset;
									clip.y = - offset;
									break;
								case 2:
									clip.x = - offset;
									clip.y = - _size - offset;
									break;
								case 3:
									clip.x = - _size - offset;
									clip.y = - _size - offset;
									break;
							}
							break;
						case Shape.T:
							switch( a.state.number )
							{
								case 0:
									clip.x = - offset;
									clip.y = - offset;
									break;
								case 1:
									clip.x = - _size - offset;
									clip.y = - offset;
									break;
								case 2:
									clip.x = - 2 * _size - offset;
									clip.y = - offset;
									break;
								case 3:
									clip.x = - _size - offset;
									clip.y = - _size - offset;
									break;
							}
							break;
						case Shape.S:
							switch( a.state.number )
							{
								case 0:
									clip.x = - _size - offset;
									clip.y = - offset;
									break;
								case 1:
									clip.x = - 2 * _size - offset;
									clip.y = - offset;
									break;
								case 2:
									clip.x = - offset;
									clip.y = - _size - offset;
									break;
								case 3:
									clip.x = - _size - offset;
									clip.y = - _size - offset;
									break;
							}
							break;
						case Shape.Z:
							switch( a.state.number )
							{
								case 0:
									clip.x = - offset;
									clip.y = - offset;
									break;
								case 1:
									clip.x = - _size - offset;
									clip.y = - offset;
									break;
								case 2:
									clip.x = - _size - offset;
									clip.y = - _size - offset;
									break;
								case 3:
									clip.x = - 2 * _size - offset;
									clip.y = - _size - offset;
									break;
							}
							break;
						case Shape.L:
							switch( a.state.number )
							{
								case 0:
									clip.x = - offset;
									clip.y = - offset;
									break;
								case 1:
									clip.x = - _size - offset;
									clip.y = - offset;
									break;
								case 2:
									clip.x = - 2 * _size - offset;
									clip.y = - offset;
									break;
								case 3:
									clip.x = - offset;
									clip.y = - _size - offset;
									break;
							}
							break;
						case Shape.F:
							switch( a.state.number )
							{
								case 0:
									clip.x = - offset;
									clip.y = - offset;
									break;
								case 1:
									clip.x = - _size - offset;
									clip.y = - offset;
									break;
								case 2:
									clip.x = - 2 * _size - offset;
									clip.y = - offset;
									break;
								case 3:
									clip.x = - 2 * _size - offset;
									clip.y = - _size - offset;
									break;
							}
							break;
					}
					_cells[a.location.x][a.location.y].rotation = a.state.rotation;
				}
			}
		}
		
	}
}