package cz.jzitnik.game.generation;

import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.SteveData;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.sprites.Steve;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Generation {
    public static void generateWorld(Game game) {
        var world = game.getWorld();
        initializeWorld(world);

        Block steveBlock = new Block("steve", SpriteLoader.SPRITES.STEVE);
        steveBlock.setSpriteState(Steve.SteveState.FIRST);
        steveBlock.setGhost(true);
        steveBlock.setData(new SteveData(true));
        Block steveBlock2 = new Block("steve", SpriteLoader.SPRITES.STEVE);
        steveBlock2.setSpriteState(Steve.SteveState.SECOND);
        steveBlock2.setGhost(true);
        steveBlock2.setMob(true);
        steveBlock2.setData(new SteveData(false));

        int[] terrainHeight = PopulateWorld.generateTerrain();

        game.getInventory().addItem(ItemBlockSupplier.getItem("lava_bucket"));

        game.getPlayer().setPlayerBlock1(steveBlock);
        game.getPlayer().setPlayerBlock2(steveBlock2);

        PopulateWorld.populateWorld(world, terrainHeight);
        Trees.plantTrees(world, terrainHeight);

        // Spawn player at a valid starting point
        world[terrainHeight[256] - 1][256].add(steveBlock2);
        world[terrainHeight[256] - 2][256].add(steveBlock);
    }

    private static void initializeWorld(List<Block>[][] world) {
        for (int i = 0; i < 256; i++) {
            for (int j = 0; j < 512; j++) {
                world[i][j] = new CopyOnWriteArrayList<>();
            }
        }
    }
}
