const numOfDigits = 4;
const takeNumFromID = true;
var rawModelsNums = ModelManager.loadPartedRawModel(Resources.manager(), Resources.idRelative("mtr:number/nums.obj"), null);
var modelNum = uploadNumModels(rawModelsNums);

function numCreate(train, state) {
	let id = train.id() % Math.pow(10, numOfDigits) / 2 + Math.pow(10, numOfDigits) / 2;
	state.id = id;

	state.customNumSet = false

}


function numRender(ctx, train, state, matrices, offset, carInd) {
	let id = state.id === undefined ? 0 : state.id;
	if (!state.customNumSet && train.siding() != null) {
		let customNum = Math.abs(parseInt(train.siding().name));
		if (!isNaN(customNum)) {
			id = customNum;
		}
	}

	
	let idStr = id.toString().padStart(numOfDigits, '0');

	let firstOffset = offset * 0.5 * (- numOfDigits + 1);

	// matrices.pushPose();
	for (var i = 0; i < numOfDigits; i++) {
		matrices.translate(firstOffset + i * offset, 0, 0);
		ctx.drawCarModel(modelNum["" + idStr[i]], carInd, matrices);
		matrices.translate(-(firstOffset + i * offset), 0, 0);
	}
	// matrices.popPose();
}

function numRenderWithIndex(ctx, train, state, matrices, offset, carInd) {
	let id = state.id === undefined ? 0 : state.id;

	if (!state.customNumSet && train.siding() != null) {
		let customNum = Math.abs(parseInt(train.siding().name));
		if (!isNaN(customNum)) {
			id = customNum;
		}
	}

	id = (id + carInd) % Math.pow(10, numOfDigits)

	
	let idStr = id.toString().padStart(numOfDigits, '0');

	let firstOffset = offset * 0.5 * (- numOfDigits + 1);

	// matrices.pushPose();
	for (var i = 0; i < numOfDigits; i++) {
		matrices.translate(firstOffset + i * offset, 0, 0);
		ctx.drawCarModel(modelNum["" + idStr[i]], carInd, matrices);
		matrices.translate(-(firstOffset + i * offset), 0, 0);
	}
	// matrices.popPose();
}


function uploadNumModels(rawModels) { 
  result = {};
  for (it = rawModels.entrySet().iterator(); it.hasNext(); ) {
    entry = it.next();

    let jsStringKey = "" + entry.getKey(); 
    entry.getValue().applyUVMirror(false, true);
    result["" + jsStringKey] = ModelManager.uploadVertArrays(entry.getValue());
  }
  return result;
}