
// генерирует код для форм блоков из json моделей

const fs = require('fs')
const root_main = './src/main/resources/assets/overdrive_that_matters/models/block/'
const root_datagen = './src/data/resources/assets/overdrive_that_matters/models/block/'

const args = process.argv.slice(2)

if (args.length == 0) {
	console.error('No model(s) names specified.')
	process.exit(2)
}

const handle = fs.openSync('./shapegen_output.java', 'w')

for (const model of args) {
	fs.writeSync(handle, 'public static final BlockShape ' + model.toUpperCase() + ' = new BlockShape(\n')
	
	let readData

	try {
		readData = fs.readFileSync(root_main + model + '.json', {encoding: 'utf-8'})
	} catch(err1) {
		try {
			readData = fs.readFileSync(root_datagen + model + '.json', {encoding: 'utf-8'})
		} catch(err2) {
			console.error('Unable to read model file in either of locations')
			console.error(err1)
			console.error(err2)
			process.exit(1)
		}
	}

	const obj = JSON.parse(readData)
	let first = true

	for (const elementID in obj.elements) {
		const element = obj.elements[elementID]

		if (element.rotation && element.rotation.angle != 0)
		    continue;

		const from = element.from
		const to = element.to

		if (first) {
			first = false
		} else {
			fs.writeSync(handle, ',\n')
		}

		fs.writeSync(handle, `\tnew SimpleCuboid(${from[0] / 16}d, ${from[1] / 16}d, ${from[2] / 16}d, ${to[0] / 16}d, ${to[1] / 16}d, ${to[2] / 16}d)`)
	}
	
	console.log(`Parsed and generated shape of ${model}.json`)

	fs.writeSync(handle, '\n);\n\n')
}

fs.closeSync(handle)
