package me.hugmanrique.pokedata.maps;

import lombok.Getter;
import lombok.ToString;
import me.hugmanrique.pokedata.Data;
import me.hugmanrique.pokedata.roms.ROM;
import me.hugmanrique.pokedata.tiles.Tileset;
import me.hugmanrique.pokedata.utils.BitConverter;

/**
 * @author Hugmanrique
 * @since 02/07/2017
 */
@Getter
@ToString
public class MapData extends Data {
    private long width;
    private long height;

    private int borderTilePtr;
    private int mapTilesPtr;
    private int globalTilesetPtr;
    private int localTilesetPtr;

    private int borderWidth;
    private int borderHeight;

    public MapData(ROM rom) {
        width = rom.getPointer(true);
        height = rom.getPointer(true);

        borderTilePtr = rom.getPointerAsInt();

        mapTilesPtr = rom.getPointerAsInt();
        globalTilesetPtr = rom.getPointerAsInt();
        localTilesetPtr = rom.getPointerAsInt();

        System.out.println(globalTilesetPtr);
        System.out.println(localTilesetPtr);

        // Borders on Emerald are always 2x2
        if (rom.getGame().isGem()) {
            borderWidth = 2;
            borderHeight = 2;

            Tileset.LOCAL_BLOCKS = 0xA2;
        } else {
            int[] borderDims = BitConverter.toInts(rom.readBytes(2));

            borderWidth = borderDims[0];
            borderHeight = borderDims[1];
        }
    }

    public static MapData load(ROM rom, int offset) {
        rom.seek(offset);

        return new MapData(rom);
    }
}

@@ -594,24 +594,56 @@
 derHeight;%0A%0A
+    private int secondarySize;%0A%0A
     public M
@@ -943,54 +943,86 @@
     
-System.out.println(globalTilesetPtr
+borderWidth = rom.readByte();%0A        borderHeight = rom.readByte(
 );%0A
+%0A
         
 Syst
@@ -1021,43 +1021,42 @@
     
-System.out.println(localTilesetPtr)
+secondarySize = borderWidth + 0xA0
 ;%0A%0A 
@@ -1235,12 +1235,21 @@
 S = 
-0xA2
+secondarySize
 ;%0A  
@@ -1279,148 +1279,44 @@
     
-int%5B%5D borderDims = BitConverter.toInts(rom.readBytes(2));%0A%0A            borderWidth = borderDims%5B0%5D;%0A            borderHeight = borderDims%5B1%5D
+secondarySize = Tileset.LOCAL_BLOCKS
 ;%0A  

Load borders and set secondary size properly if game is gem