package pathgame.rendering;

import pathgame.tilemap.Tile;
import pathgame.tilemap.TileMap;
import pathgame.tilemap.Tiles;

public class TileRenderer
{
    /** The unscaled render size of a map tile. */
    public final static float TILE_SIZE = 32;
    
    private static TileTextureProvider[] tProviders = new TileTextureProvider[8];
    
    private static void register(Tile t, TileTextureProvider provider)
    {
        if(t.getId() >= tProviders.length)
        {
            TileTextureProvider[] newTProviders = new TileTextureProvider[tProviders.length * 2];
            System.arraycopy(tProviders, 0, newTProviders, 0, tProviders.length);
            tProviders = newTProviders;
        }
        tProviders[t.getId()] = provider;
    }
    
    static
    {
        register(Tiles.GRASS, new StaticTextureProvider(TileTexture.fromTextureId(0)));
        register(Tiles.GRASS_WITH_STONE, new StaticTextureProvider(TileTexture.fromTextureId(1)));
        register(Tiles.GRASS_WITH_6_BUSHES, new StaticTextureProvider(TileTexture.fromTextureId(2)));
        register(Tiles.GRASS_WITH_3_BUSHES, new StaticTextureProvider(TileTexture.fromTextureId(3)));
        register(Tiles.GRASS_WITH_FLOWERS_1, new StaticTextureProvider(TileTexture.fromTextureId(4)));
        register(Tiles.GRASS_WITH_FLOWERS_2, new StaticTextureProvider(TileTexture.fromTextureId(21)));
        register(Tiles.GRASS_WITH_FLOWERS_3, new StaticTextureProvider(TileTexture.fromTextureId(22)));
        register(Tiles.GRASS_WITH_FLOWERS_4, new StaticTextureProvider(TileTexture.fromTextureId(23)));
        register(Tiles.GRASS_WITH_HILL, new StaticTextureProvider(TileTexture.fromTextureId(5)));
        register(Tiles.GRASS_WITH_EARTH, new StaticTextureProvider(TileTexture.fromTextureId(6)));
        register(Tiles.FOREST, new StaticTextureProvider(TileTexture.fromTextureId(7)));
        register(Tiles.SHALLOW_WATER, new StaticTextureProvider(TileTexture.fromTextureId(8)));
        register(Tiles.DEEP_WATER, new StaticTextureProvider(TileTexture.fromTextureId(9)));
        register(Tiles.HILL, new StaticTextureProvider(TileTexture.fromTextureId(10)));
        register(Tiles.MOUNTAIN, new StaticTextureProvider(TileTexture.fromTextureId(11)));
        register(Tiles.SWAMP, new StaticTextureProvider(TileTexture.fromTextureId(12)));
        register(Tiles.SWAMP_DECO, new StaticTextureProvider(TileTexture.fromTextureId(13)));
        register(Tiles.SWAMP_TREE, new StaticTextureProvider(TileTexture.fromTextureId(14)));
        register(Tiles.TOWN, new StaticTextureProvider(TileTexture.fromTextureId(16)));
        register(Tiles.TOWN_BLOCKED_1, new StaticTextureProvider(TileTexture.fromTextureId(17)));
        register(Tiles.TOWN_BLOCKED_2, new StaticTextureProvider(TileTexture.fromTextureId(18)));
        register(Tiles.TOWN_BLOCKED_3, new StaticTextureProvider(TileTexture.fromTextureId(19)));
        register(Tiles.TOWN_BLOCKED_4, new StaticTextureProvider(TileTexture.fromTextureId(20)));
        register(Tiles.PORT, new StaticTextureProvider(TileTexture.fromTextureId(15)));
        register(Tiles.HOME_TOWN, new StaticTextureProvider(TileTexture.fromTextureId(39)));
        
        register(Tiles.PATH_N, new StaticTextureProvider(TileTexture.fromTextureId(24)));
        register(Tiles.PATH_E, new StaticTextureProvider(TileTexture.fromTextureId(25)));
        register(Tiles.PATH_S, new StaticTextureProvider(TileTexture.fromTextureId(26)));
        register(Tiles.PATH_W, new StaticTextureProvider(TileTexture.fromTextureId(27)));
        register(Tiles.PATH_E_W, new StaticTextureProvider(TileTexture.fromTextureId(28)));
        register(Tiles.PATH_N_S, new StaticTextureProvider(TileTexture.fromTextureId(29)));
        register(Tiles.PATH_S_W, new StaticTextureProvider(TileTexture.fromTextureId(30)));
        register(Tiles.PATH_N_E, new StaticTextureProvider(TileTexture.fromTextureId(31)));
        register(Tiles.PATH_E_S, new StaticTextureProvider(TileTexture.fromTextureId(32)));
        register(Tiles.PATH_N_W, new StaticTextureProvider(TileTexture.fromTextureId(33)));
        register(Tiles.PATH_N_E_W, new StaticTextureProvider(TileTexture.fromTextureId(34)));
        register(Tiles.PATH_N_S_W, new StaticTextureProvider(TileTexture.fromTextureId(35)));
        register(Tiles.PATH_N_E_S, new StaticTextureProvider(TileTexture.fromTextureId(36)));
        register(Tiles.PATH_E_S_W, new StaticTextureProvider(TileTexture.fromTextureId(37)));
        register(Tiles.PATH_N_E_S_W, new StaticTextureProvider(TileTexture.fromTextureId(38)));
    }
    
    public static TileTexture getTileTexture(TileMap map, Tile t, int x, int y)
    {
        if(t == null)
        {
            return null;
        }
        int id = t.getId();
        if(id < 0 || id >= tProviders.length || tProviders[id] == null)
        {
            return null;
        }
        return tProviders[id].getTexture(map, t, x, y);
    }
}
