import { readFileSync } from "fs";
import { checkInstructions, simulate } from "./src/asteracer/simulation";
import { parseInstructions, parseMap } from "./src/asteracer/parse_map";

const worldFileName = process.argv[2];
const instructionsFileName = process.argv[3];

if (!worldFileName || !instructionsFileName) {
    console.log("Použití: npm run app [název souboru mapy] [název souboru s instrukcemi]");
    process.exit();
}

const worldFileText = readFileSync(worldFileName, { encoding: 'utf8', flag: 'r' });
const instructionsFileText = readFileSync(instructionsFileName, { encoding: 'utf8', flag: 'r' });

const world = parseMap(worldFileText);
const instructions = parseInstructions(instructionsFileText);

try {
    checkInstructions(instructions);
} catch (e) {
    console.error(e.message);
    process.exit();
}

const simulationResult = await simulate(world, instructions);

if (simulationResult.messages) {
    for (const msg of simulationResult.messages) {
        console.log(msg);
    }
} else {
    console.log("Závod úspěšně dokončen!");
}