importPackage(java.awt);
importPackage(java.awt.geom);

//include(Resources.idRelative("mtr:lib/harrys_lib.js"));

//var numFont = Resources.readFont(Resources.idRelative("mtr:font/bertadrug.ttf"));

/*var rawModels = ModelManager.loadPartedRawModel(Resources.manager(), Resources.idRelative("mtr:71-605_scripted/nums.obj"), null);
var modelNum = uploadPartedModels(rawModels, true, true);*/


function dtnumCreate(train, state) {
	state.pisTextures = {}
	state.numModels = {}
	for (var i = 0; i < train.trainCars(); i++) {
		let pisTexture = new GraphicsTexture(300, 200);
		let numModel = models["nums"].copyForMaterialChanges();

		numModel.replaceAllTexture(pisTexture.identifier);

		state.pisTextures["" + i] = pisTexture;
		state.numModels["" + i] = numModel;
	}

	state.pisRateLimitNum = new RateLimit(0.5);
}

function dtnumRender(train, state) {
	if (state.pisRateLimitNum.shouldUpdate()) {
    	for (var i = 0; i < train.trainCars(); i++) {
    		let g = state.pisTextures["" + i].graphics;

    		g.setComposite(AlphaComposite.Clear);
			g.fillRect(0, 0, 300, 200);
			g.setComposite(AlphaComposite.SrcOver);

			g.setColor(Color.WHITE);
			let num = (state.number + i) % 10000;
			let numStr = num + "";
			numStr = numStr.padStart(4, '0')

			g.setFont(numFont.deriveFont(0, 90));
  			drawStringCenter(g, numStr, 150, 130);

			state.pisTextures["" + i].upload();
    	}
	}
}

function dtnumDispose(train, state) {
	for (var i = 0; i < train.trainCars(); i++) {
		if (state.pisTextures["" + i] != undefined) state.pisTextures["" + i].close();
	}
}
