package main.java.engine.map;

import jgame.impl.JGEngineInterface;
import main.java.schema.map.GameMapSchema;
import main.java.schema.map.TileMapSchema;
import main.java.schema.map.TileSchema;

import java.io.Serializable;
import java.util.*;

public class TDMap {
    private static final int X_OFFSET = 0;
    private static final int Y_OFFSET = 0;
    private static final int X_SKIP = 0;
    private static final int Y_SKIP = 0;

    private static final String tileImageName = "tile";
    private static final String tileImagePrefix = "t";
    private static final int TILE_CID = 0;
    private static final String IMG_OP = "-";

    private List<TileSchema> tileSchemas;
    private List<TileMapSchema> tileMapSchemas;
    private Map<String, TileMap> tileMaps;
    private Set<String> definedTiles;

    public TDMap(JGEngineInterface engine, GameMapSchema gameMapSchema) {
        Map<String, Serializable> gameMapAttributes = gameMapSchema.getAttributesMap();
        tileSchemas = (List<TileSchema>) gameMapAttributes.get(GameMapSchema.MY_TILES);
        tileMapSchemas = (List<TileMapSchema>) gameMapAttributes.get(GameMapSchema.MY_TILEMAPS);
        tileMaps = new HashMap<>();
        definedTiles = new HashSet<>();

        // Load tilemaps used for this map
        for (TileMapSchema tms : tileMapSchemas) {
            TileMap tileMap = new TileMap(tms);
            tileMaps.put(tileMap.name, tileMap);

            String fixedName = tileMap.name.replace("\\", "//");
            engine.defineImageMap(tileMap.name, fixedName, X_OFFSET, Y_OFFSET,
                    tileMap.pixelSize, tileMap.pixelSize, X_SKIP, Y_SKIP);
        }

        // Load in tiles to engine
        for (TileSchema ts : tileSchemas) {
            Map<String, Serializable> tsAttributeMap = ts.getAttributesMap();
            String tileMapFileName = (String) tsAttributeMap.get(TileSchema.TILEMAP_FILE_NAME);
            int tileRow = (int) tsAttributeMap.get(TileSchema.CANVAS_ROW);
            int tileCol = (int) tsAttributeMap.get(TileSchema.CANVAS_COL);
            int tileMapRow = (int) tsAttributeMap.get(TileSchema.TILEMAP_ROW);
            int tileMapCol = (int) tsAttributeMap.get(TileSchema.TILEMAP_COL);
            int tileCID = (int) tsAttributeMap.get(TileSchema.TILE_CID);
            int tileIndex = tileMapRow * tileMaps.get(tileMapFileName).numCols + tileMapCol;

            String tileName = tileMapFileName + tileMapCol + tileMapRow;

            if (!definedTiles.contains(tileName)) {
                engine.defineImage(tileName, tileMapRow + tileMapCol + "", tileCID, tileMapFileName, tileIndex, "-");
                definedTiles.add(tileName);
            }

            engine.setTile(tileCol, tileRow, tileMapRow + tileMapCol + "");
        }
    }

    private class TileMap {
        private String name;
        private int pixelSize;
        private int numRows;
        private int numCols;

        public TileMap(TileMapSchema tileMapSchema) {
            Map<String, Serializable> tmsAttributesMap = tileMapSchema.getAttributesMap();
            name = (String) tmsAttributesMap.get(TileMapSchema.TILEMAP_FILE_NAME);
            pixelSize = (int) tmsAttributesMap.get(TileMapSchema.PIXEL_SIZE);
            numRows = (int) tmsAttributesMap.get(TileMapSchema.NUM_ROWS);
            numCols = (int) tmsAttributesMap.get(TileMapSchema.NUM_COLS);
        }
    }
}

@@ -1923,27 +1923,31 @@
  tileRow = (
-i
+I
 nt
+eger
 ) tsAttribut
@@ -2002,27 +2002,31 @@
  tileCol = (
-i
+I
 nt
+eger
 ) tsAttribut
@@ -2084,27 +2084,31 @@
 leMapRow = (
-i
+I
 nt
+eger
 ) tsAttribut
@@ -2167,27 +2167,31 @@
 leMapCol = (
-i
+I
 nt
+eger
 ) tsAttribut
@@ -2251,19 +2251,23 @@
 eCID = (
-i
+I
 nt
+eger
 ) tsAttr
@@ -3187,27 +3187,31 @@
 ixelSize = (
-i
+I
 nt
+eger
 ) tmsAttribu
@@ -3267,27 +3267,31 @@
  numRows = (
-i
+I
 nt
+eger
 ) tmsAttribu
@@ -3349,19 +3349,23 @@
 Cols = (
-i
+I
 nt
+eger
 ) tmsAtt

update int to integer