import { initializeEditor } from "../../../../model/game/initializeEditor.js";
import { randomFromArray } from "../../../src/core/math/random/randomFromArray.js";
import { randomIntegerBetween } from "../../../src/core/math/random/randomIntegerBetween.js";
import { seededRandom } from "../../../src/core/math/random/seededRandom.js";
import { TextureAssetLoader } from "../../../src/engine/asset/loaders/texture/TextureAssetLoader.js";
import Name from "../../../src/engine/ecs/name/Name.js";
import { EntityNode } from "../../../src/engine/ecs/parent/EntityNode.js";
import { Transform } from "../../../src/engine/ecs/transform/Transform.js";
import { EngineHarness } from "../../../src/engine/EngineHarness.js";
import { enableEditor } from "../../enableEditor.js";


/**
 *
 * @param {function} rng
 */
function randomNode(rng = Math.random) {
    const node = new EntityNode();


    node.entity.add(new Transform());
    const ENGLISH_FIRST_NAMES = [
        "Alex", "Anthony","Adam",
        "Barbara", "Bartholomew","Benjamin",
        "Charles", "Christian", "Cindy", "Catherine",
        "Duncan", "David", "Donna", "Dylan",
        "Ethan", "Edward", "Edgar", "Elizabeth",
        "Fletcher", "Francis", "Fae",
        "George", "Gavin", "Gabriel", "Gideon", "Griffin", "Grace", "Gordon",
        "Hugh", "Harry", "Hilla",
        "Ian", "Inge", "Ivy", "Isabella",
        "John", "Jeff", "Joanna","Jack",
        "Kevin", "Kelsey", "Kara", "Kade", "Kenneth", "Kylie", "Kaleb", "Khloe", "Keith", "Katie",
        "Lewis", "Liam",
        "Mathew", "Mark", "Mona",
        "Nickolas", "Nigel", "Natasha",
        "Otto", "Owen", "Olivia", "Oliver", "Odette", "Ophelia", "Orion",
        "Peter", "Paola",
        "Raphael", "Ralf", "Robert", "Richard",
        "Steward", "Simon", "Samantha",
        "Trevor", "Tylor", "Terry",
        "Uwe", "Uriel", "Uma", "Ulysses",
        "Vladimir", "Vera",
        "Wolfgang", "William", "Wyatt", "Willow", "Wesley", "Wren",
        "Xylo", "Xavier", "Xander",
    ];

    const ENGLISH_SURNAMES = [
        "Angel", "Aaron", "Abbott", "Abrams", "Ainsley","Anderson","Allen",
        "Baker", "Bank", "Black", "Bale","Brown","Bennet","Bailey",
        "Carter", "Chandler","Cooper","Clarke","Cook",
        "Drake", "Dalton", "Davison", "Dawson", "Day","Davis",
        "Ellis", "Eastwood", "Erickson", "Eaton", "Ellison","Evans","Edwards",
        "Farley", "Fisc", "Fry", "Falcon","Farmer","Fletcher",
        "Gabin", "Green", "Gray", "Gibson", "Grant", "Gordon", "Gill","Gold","Goldring","Golding",
        "Hall", "Harris", "Hill", "Hughes", "Harrison", "Hunt", "Holmes",
        "Ingram", "Irwin", "Irving", "Ives",
        "Johnson","Jones","Jackson",
        "Kelvin","King",
        "Lewis", "Law","Lee",
        "Morris","Mills","Martin","Moore","Morgan","Mitchel",
        "Phillips","Parker","Price",
        "Robinson","Roberts","Richardson",
        "Smith","Scott",
        "Tailor","Taylor","Thomas","Thompson","Turner",
        "White","Wilson","Williams","Wright","Wood","Ward","Watson",
        "Young",
    ];

    const first_name = randomFromArray(rng, ENGLISH_FIRST_NAMES);
    const last_name = randomFromArray(rng, ENGLISH_SURNAMES);

    node.entity.add(new Name(`${first_name} ${last_name}`))

    return node;
}

/**
 *
 * @param {Engine} engine
 * @returns {Promise<void>}
 */
async function main(engine) {

    await EngineHarness.buildBasics({
        engine,
        showFps: false
    });


    const random = seededRandom();

    const density = 0.2;

    const nodes = [];

    for (let i = 0; i < 50; i++) {
        nodes.push(randomNode(random));
    }

    while (nodes.length > 4) {
        const child_index = randomIntegerBetween(random, 0, nodes.length - 1);

        const [child] = nodes.splice(child_index, 1);

        const parent = randomFromArray(random, nodes);

        parent.addChild(child);
    }

    for (let i = 0; i < nodes.length; i++) {

        const node = nodes[i];

        node.build(engine.entityManager.dataset);

    }


    enableEditor(engine, initializeEditor).enable();
}

new EngineHarness().initialize({
    configuration(config, engine) {
        config.addLoader('texture', new TextureAssetLoader())
    }
}).then(main)
