/*
 *   EZ GL2D Platfom Game engine Example
 *   Source is provided "as is" without express or implied warranty of any kind.
 * 
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using EZGL2D;

namespace GL2DexampleJump.Game
{
    public class LevelMap
    {
        private GL2D.SpriteGL[] tileAtlas;

        private int width = 0;
	    private int height = 0;
    	
	    private int tileSize = 32;


        int[,] map;

        public LevelMap()
        {

        }

	    public void init( int width, int height, int tileSize, GL2D.SpriteGL[] tileAtlas )
	    {
		    this.tileAtlas = tileAtlas;
		    this.width = width;
		    this.height = height;
		    this.tileSize = tileSize;
    		
		    map = new int[width,height];
    		
		    for( int y = 0; y < height; y++ )	
		    {
			    for( int x = 0; x < width; x++ )
			    {
				    map[x,y] = 0;
			    }
		    }
    		
		    String[] textMap = 
		    {
				    //012345678901234567890123456789012345678901234567890
				    "++++++++++++++++++++",		// 0
				    "+                  +",		// 1	
				    "+                  +",		// 2	
				    "+               ++++",		// 3	
				    "+       ##         +",		// 4	
				    "++++++         +   +",		// 5	
				    "+                  +",		// 6	
				    "+                  +",		// 7	
				    "+   ##### # #      +",		// 8	
				    "+                  +",		// 9	
				    "+              +++++",		// 0	
				    "+##                +",		// 1	
				    "+    ###  ++++     +",		// 2	
				    "+                  +",		// 3	
				    "+##################+",		// 4	
    				
		    };

		    for( int y = 0; y < height; y++ )	
		    {
    		
			    int len = textMap[y].Length; 
			    for( int x = 0; x < len; x++ )
			    {
				    int index = textMap[y][x];
				    if( index > 0 )
				    {
					    switch( index )
					    {
						    case '#':
							    map[x,y] = 1;
						    break;
						    case '+':
							    map[x,y] = 2;
						    break;
					    }
				    }
			    }
		    }
    		
	    }
    	
	    public void fill( int value )
	    {
    		
		    for( int y = 0; y < height; y++ )	
		    {
			    for( int x = 0; x < width; x++ )
			    {
				    map[x,y] = value;
			    }
		    }
	    }
    	
	    public void update( float speed )
	    {
		    updateMap( speed );
	    }
    	
	    public void updateMap( float speed )
	    {
    			
	    }

	    public void renderMap()
	    {
    		
		    for( int y = 0; y < height; y++ )
		    {
			    for( int x = 0; x < width; x++ ) 
			    {
				    int i = map[x,y];		// get map index
				    if( i > 0 )
				    {

					    int sx = (x * tileSize);      //Calculate where to put a
					    int sy = (y * tileSize);      //particular tile
                        GL2D.Sprite(sx, sy, GL2D.SpriteGL.FLIP_NONE, tileAtlas[i-1]);
            
                        //g.drawImage( tileAtlas[i-1], 
						//	 	     sx,
						//	 	     sy,
						//	 	     screen );
    			
				    }
			    }
		    }
    		
    		
	    }

	    public int getWidth()
	    {
		    return width;
	    }

	    public int getHeight()
	    {
		    return height;
	    }

	    public int getTileSize()
	    {
		    return tileSize;
	    }


	    public void setWidth(int width)
	    {
		    this.width = width;
	    }

	    public void setHeight(int height)
	    {
		    this.height = height;
	    }

	    public void setTileSize(int tileSize)
	    {
		    this.tileSize = tileSize;
	    }

	    public int[,] getMap()
	    {
		    return map;
	    }
	
    }
}
