
const colors = [
	['orange', [245, 116, 16]],
	['magenta', [186, 63, 175]],
	['light_blue', [59, 180, 219]],
	['yellow', [252, 199, 36]],
	['lime', [111, 187, 24]],
	['pink', [243, 139, 170]],
	['gray', [62, 66, 70]],
	['light_gray', [140, 140, 131]],
	['cyan', [22, 134, 145]],
	['purple', [116, 38, 169]],
	['blue', [51, 53, 155]],
	['brown', [114, 71, 40]],
	['green', [84, 109, 28]],
	['red', [156, 37, 34]],
	['black', [31, 31, 35]],
]

const white = ['white', [235, 235, 235]]
const colorsWithWhite = [...colors]
colorsWithWhite.push(white)

function addRgbString(values) {
	for (const row of values) {
		const rgb = row[1]
		row.push(`xc:rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`)
	}
}

addRgbString(colors)
addRgbString(colorsWithWhite)

const rootFolder = '../src/main/resources/assets/overdrive_that_matters/textures/'
const child_process = require('child_process')

process.stdout.setMaxListeners(900)
process.stderr.setMaxListeners(900)

async function getSize(path) {
	const identify = child_process.spawn('magick', [
		'identify',
		path,
	])

	identify.stderr.pipe(process.stderr)

	const chunks = []
	identify.stdout.on('data', (a) => chunks.push(a))

	await new Promise((resolve) => {
		identify.on('close', () => resolve())
	})

	const chunk = chunks[0].toString('utf-8')
	const size = chunk.match(/PNG ([0-9]+)x([0-9]+)/)
	const width = parseInt(size[1])
	const height = parseInt(size[2])

	return [width, height, `${width}x${height}`]
}

function splitName(name) {
	const splitted = name.split('/')
	const fileName = splitted.pop()
	const baseFolder = splitted.join('/')
	const fullBaseFolder = `${rootFolder}${baseFolder}`

	return [fileName, baseFolder, fullBaseFolder, fileName.indexOf('_white') == -1 ? colorsWithWhite : colors]
}

exports.colors = colors
exports.colorsWithWhite = colorsWithWhite
exports.rootFolder = rootFolder
exports.splitName = splitName
exports.getSize = getSize
