import { Rectangle } from "../../../../pixi/pixi.mjs";
import { PRNG } from "../../../GlobalVariables/GlobalVariables";
import { PointInt2D } from "../../../Utils/Math.utils";
import { selectRandomValueFromArray } from "../../../Utils/PRNG.utils";
import { TerrainTile } from "../../Tiles/TerrainTile";
import { TileValue } from "../Generator/TerrainGenerator";

const TERRAIN_TILE_WATER = 0;
const TERRAIN_TILE_SAND = 1;
const TERRAIN_TILE_GRASS = 2;
const TERRAIN_TILE_STONE = 3;
const TERRAIN_TILE_SOIL = 4;

const TERRAIN_TILE_WATER_Z = 1;
const TERRAIN_TILE_SAND_Z = 0;
const TERRAIN_TILE_GRASS_Z = 4;
const TERRAIN_TILE_STONE_Z = 2;
const TERRAIN_TILE_SOIL_Z = 3;

const TERRAIN_TILE_WATER_FRAMES = [new Rectangle(1, 1, 32, 32)];
const TERRAIN_TILE_SAND_FRAMES = [new Rectangle(1, 35, 32, 32), new Rectangle(35, 35, 32, 32), new Rectangle(69, 35, 32, 32), new Rectangle(103, 35, 32, 32)];
const TERRAIN_TILE_GRASS_FRAMES = [new Rectangle(1, 69, 32, 32), new Rectangle(35, 69, 32, 32), new Rectangle(69, 69, 32, 32)];
const TERRAIN_TILE_STONE_FRAMES = [new Rectangle(1, 103, 32, 32)];
const TERRAIN_TILE_SOIL_FRAMES = [new Rectangle(1, 137, 32, 32)];

const terrainNavigationCostList = {
    0: 10, //water
    1: 1.7, //sand
    2: 1, //grass
    3: 1.5, //stone
    4: 1.6 //soil
};

/**
 *
 * @param {TileValue} value
 * @param {PointInt2D} position
 * @returns {TerrainTile | null}
 */
export function createTerrainTileFromTileValue(value, position) {
    let t = new TerrainTile(false, position);
    t.spriteSheetPath = "assets/images/world/world_terrain_atlas_overlay.png";
    if (value.noiseValue < 0.3) {
        t.frame = selectRandomValueFromArray(TERRAIN_TILE_WATER_FRAMES, value.noiseValue);
        t.tileId = TERRAIN_TILE_WATER;
        t.zOrder = TERRAIN_TILE_WATER_Z;
        t.navigationCost = terrainNavigationCostList[TERRAIN_TILE_WATER];
    } else if (value.noiseValue < 0.35)
    {
        t.frame = selectRandomValueFromArray(TERRAIN_TILE_SAND_FRAMES, value.noiseValue);
        t.tileId = TERRAIN_TILE_SAND;
        t.zOrder = TERRAIN_TILE_SAND_Z;
        t.navigationCost = terrainNavigationCostList[TERRAIN_TILE_SAND];
    } else if (value.noiseValue < 0.8)
    {
        if(value.temperatureValue >= 0.95 && value.noiseValue >= 0.75)
        {
            t.frame = selectRandomValueFromArray(TERRAIN_TILE_SOIL_FRAMES, value.noiseValue);
            t.tileId = TERRAIN_TILE_SOIL;
            t.zOrder = TERRAIN_TILE_SOIL_Z;
            t.navigationCost = terrainNavigationCostList[TERRAIN_TILE_SOIL];
        }
        else
        {
            t.frame = selectRandomValueFromArray(TERRAIN_TILE_GRASS_FRAMES, value.noiseValue);
            t.tileId = TERRAIN_TILE_GRASS;
            t.zOrder = TERRAIN_TILE_GRASS_Z;
            t.navigationCost = terrainNavigationCostList[TERRAIN_TILE_GRASS];
        }
    }
    else
    {
        t.frame = selectRandomValueFromArray(TERRAIN_TILE_STONE_FRAMES, value.noiseValue);
        t.tileId = TERRAIN_TILE_STONE;
        t.zOrder = TERRAIN_TILE_STONE_Z;
        t.navigationCost = terrainNavigationCostList[TERRAIN_TILE_STONE];
    }
    return t;
}
