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

include(Resources.id("mtrsteamloco:scripts/display_helper.js"));

var useEnglishText = false;

var img = Resources.readBufferedImage(Resources.id("mtr:t6b5/trafaret.png"))

let slotCfg = {
  "renderType": "exterior",
  "version": 1,
  "texSize": [1100, 200],
  "slots": [
    {
      "name": "front_main",    
      "texArea": [0, 0, 870, 200],
      "pos": [
        [[-0.3942, 2.128, 7.331], [-0.3942, 1.886, 7.331], [0.6049, 1.886, 7.331], [0.6049, 2.128, 7.331]]
      ],
      "offsets": [[0, 0, 0]]
    },
    {
      "name": "front_num",    
      "texArea": [870, 0, 230, 200],
      "pos": [
        [[-0.7358, 2.133, 7.331], [-0.7358, 1.888, 7.331], [-0.4453, 1.888, 7.331], [-0.4453, 2.133, 7.331]]
      ],
      "offsets": [[0, 0, 0]]
    }
  ]
};

let slotCfg2 = {
  "renderType": "exterior",
  "version": 1,
  "texSize": [1100, 200],
  "slots": [
    {
      "name": "front_main",    
      "texArea": [0, 0, 870, 200],
      "pos": [
        [[-1.215, 1.356, -4.811], [-1.215, 1.080, -4.811], [-1.215, 1.080, -3.865], [-1.215, 1.356, -3.865]]
      ],
      "offsets": [[0, 0, 0]]
    },
    {
      "name": "front_num",    
      "texArea": [870, 0, 230, 200],
      "pos": [
        [[-0.05302, 1.238, -7.512], [-0.05302, 1.064, -7.512], [-0.2780, 1.064, -7.512], [-0.2780, 1.238, -7.512]]
      ],
      "offsets": [[0, 0, 0]]
    }
  ]
};

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

var textFont = Resources.readFont(Resources.idRelative("mtr:font/ussr_stencil.ttf"));

const toDepot = useEnglishText ? "NO BOARDING" : "ПОСАДКИ НЕТ";

var dhBaseFront = new DisplayHelper(slotCfg);
var dhBaseSide = new DisplayHelper(slotCfg2); // using 2 different bases so i can render only side display on carts

function displCreate(ctx, state, train) {

	state.pisRateLimit = new RateLimit(3);
	state.dh = dhBaseFront.create();
	state.dh2 = dhBaseSide.create();

	updateEmuStrings(state, train);

	state.isOnRoute = train.getThisRoutePlatforms().size() != 0;
	state.emuStringUp = getFirstStation(train)
	state.emuStringDown = getEndStation(train)
	state.emuStringNum = getRouteNum(train)
  
}

function displRender(ctx, state, train) {
	if (state.pisRateLimit.shouldUpdate()) {
    updateEmuStrings(state, train);

    let g = state.dh.graphics();
    let g2 = state.dh2.graphics();
    drawEmu(state, train, g);
    drawEmu(state, train, g2);
    state.dh.upload();
    state.dh2.upload();
  }


}

function drawEmu(state, train, g) {
	const fontSize = 100;
	const fontSizeSmall = 80;
	const fontSize2 = 130;
	const fontSize2Small = 100;
	const x = 435;
	const y = 80;
	const y2 = 170;
	const y3 = 140;

	const xN = 870 + 230/2
	const yN = 145;

	if (state.isOnRoute) {
		let upStr = (state.emuStringUp + "").substring(0, 20);
		let downStr = (state.emuStringDown + "").substring(0, 20);
		let numStr = (state.emuStringNum + "").substring(0, 5);
		
		let isBig = upStr.length < 16;
		let isBig2 = downStr.length < 16;
		let isBigNum = numStr.length < 4;
	
		/*
    g.setColor(Color.WHITE);
		g.fillRect(0, 0, 1100, 200); */

    g.drawImage(img, 0, 0, null);
	
		g.setColor(Color.BLACK);
		g.setFont(textFont.deriveFont(0, isBig ? fontSize : fontSizeSmall));
		drawStringCenter(g, upStr, x, y);
		g.setFont(textFont.deriveFont(0, isBig2 ? fontSize : fontSizeSmall));
		drawStringCenter(g, downStr, x, y2);
	
		g.setFont(textFont.deriveFont(0, isBigNum ? fontSize2 : fontSize2Small));
		drawStringCenter(g, numStr, xN, yN);
	
		g.setColor(Color.BLACK);
	} else {
		g.drawImage(img, 0, 0, null);
    
		g.setFont(textFont.deriveFont(0, 115));
		g.setColor(Color.BLACK);
		drawStringCenter(g, toDepot, x, y3);
	}
}

function updateEmuStrings(state, train) {

  if (train.getThisRoutePlatforms().size() != 0) {
    state.isOnRoute = true;
    state.emuStringUp = getFirstStation(train);
    state.emuStringDown = getEndStation(train);
    state.emuStringNum = getRouteNum(train)
  } else {
    state.isOnRoute = false;
  }
}


function displDispose(ctx, state, train) {
	if (state.dh != null) state.dh.close();
	if (state.dh2 != null) state.dh.close();
}


function getEndStation(train) {
  let stationList = train.getThisRoutePlatforms();
  if (stationList[0] == undefined) return ""
  let regularEndStation = stationList[0].destinationName;
  if (regularEndStation == "") regularEndStation = ""

  let lastSt = stationList.size() - 1;
  let customEndStation = stationList[lastSt].route.platformIds[lastSt].customDestination + "";

  return customEndStation == "" ? regularEndStation : customEndStation;
}

function getFirstStation(train) {
  let stationList = train.getThisRoutePlatforms();
  if (stationList[0] == undefined) return "";
  let station = stationList[0].station;
  let customFirstSt = stationList[0].route.platformIds[0].customDestination + "";

  let regularFirstSt = station == null ? "" : station.name
  return customFirstSt == "" ? regularFirstSt : customFirstSt;
}

function getRouteNum(train) {
  let stationList = train.getThisRoutePlatforms();
  if (stationList[0] == undefined) return "";
  let ret = stationList[0].route.lightRailRouteNumber + "";
  
  return ret;
}

function drawStringCenter(g, string, x, y){
  let fontMetrics = g.getFontMetrics();
  const newX = x - fontMetrics.stringWidth(string) / 2;

  g.drawString(string, newX, y)
}