package hello;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.IOException;

public class Background
{
    private boolean backgroundSet = false;

    private TiledLayer tiles;	// The tiled layer
    private Image image;		// The image used to define the tiles.
    private int [] map = {
                    2, 3, 2, 4, 2, 2, 5, 3, 5, 2, 4,
                    1, 1, 1, 6, 1, 1, 6, 1, 6, 1, 6,
                    1, 1, 1, 6, 1, 1, 6, 1, 6, 1, 6,
                    2, 5, 2, 3, 2, 5, 3, 2, 3, 5, 8,
                    1, 6, 1, 1, 1, 6, 1, 1, 1, 6, 1,
                    1, 6, 1, 1, 1, 6, 1, 1, 1, 6, 1,
                    1, 13, 2, 5, 2, 3, 2, 10, 1, 13, 2,
                    1, 6, 1, 6, 1, 1, 1, 6, 1, 6, 1,
                    2, 8, 1, 6, 1, 1, 1, 13, 2, 8, 1,
                    1, 1, 1, 13, 2, 5, 2, 12, 1, 1, 1,
                    2, 5, 2, 8, 1, 6, 1, 9, 2, 5, 2,
                    1, 6, 1, 1, 1, 6, 1, 1, 1, 6, 1,
                    1, 6, 1, 1, 1, 6, 1, 1, 1, 6, 1,
                    2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 2};
    private final int NCOLS = 11;	// These must match the number of
    private final int NROWS = 14;	// rows and columns in the map.  i.e.
    // NROWS * NCOLS == map.length();

    private int tileX;
    private int tileY = 16;
    private int rowNo = 0;
    private int colNo = 0;

    protected Background(int type)
    {
       try{
            image = Image.createImage("tiles.png");
         //   System.out.println("Background image loaded");
       } catch (IOException e)
        {
            e.printStackTrace();
        }

        if(image == null)
            System.out.println("Image is null");
        tiles = new TiledLayer(NCOLS, NROWS, image, 32, 32);
        setCells();	// Set up  the cells according to the map.
        backgroundSet = true;
    }

    // This function assigns cells according to the map...
    private void setCells()
    {
        for(int y=0; y<NROWS; y++)
        {
            for (int x = 0; x<NCOLS; x++)
            {
                tiles.setCell(x, y, map[y*NCOLS + x]);
            }
        }
    }
    // GameCanvas does not NEED a paint() method, but it is
    // convenient to have one..
    public void draw(Graphics g)
    {
        if(tiles == null)
        {
            System.out.println("The background is null");
        }
        else
             tiles.paint(g);
    }

    public boolean isSet()
    {
        return backgroundSet;
    }

    public void move(int x, int y)
    {
        tiles.move(x, y);
    }

    public int getX()
    {
        return tiles.getX();
    }

    public int getY()
    {
        return tiles.getY();
    }

    public void reset()
    {
        colNo = 0;
        rowNo = 0;
        tileX = 0;
        tileY = 0;
    }

    public boolean checkMap(int type1, int type2, int type3, int type4, int type5, int type6, int type7, int type8, int type9, int iterator)
    {
        if(colNo > NCOLS-1)
        {
            rowNo++;
            colNo = 0;
        }

       tileX = (16 + (colNo * 32));
       tileY = (16 + (rowNo * 32));

       colNo++;

       if((map[iterator] == type1) || (map[iterator] == type2) || (map[iterator] == type3) || (map[iterator] == type4) || (map[iterator] == type5) || (map[iterator] == type6) || (map[iterator] == type7 || (map[iterator] == type8) || map[iterator] == type9))
       {
            return true;
       }
       else
           return false;
    }

    public int numberOfTiles(int type1, int type2, int type3, int type4, int type5, int type6, int type7, int type8, int type9)
    {
        int counter = 0;

        for(int i = 0; i < map.length; i++)
        {
            if((map[i] == type1) || (map[i] == type2) || (map[i] == type3) || (map[i] == type4) || (map[i] == type5) || (map[i] == type6) || (map[i] == type7 || (map[i] == type8) || map[i] == type9))
            {
                counter++;
            }
        }
       // System.out.println(counter);
        return counter;
    }

    public int getMapLength()
    {
        return (NCOLS*NROWS);
    }

    public int getTileX()
    {
        return tileX;
    }

    public int getTileY()
    {
        return tileY;
    }
}


