importPackage(java.awt);
importPackage(java.awt.geom);
include(Resources.id("mtrsteamloco:scripts/display_helper.js"));
include(Resources.id("mtr:jcibravo/scripts/main.js"));

///////////////////////////////// DISPLAY /////////////////////////////////
// Displays for even carriages
const slotCfg = {
  "version": 1,
  "texSize": [1792, 1024],
  "slots": [
    {
      "name": "display_front",
      "texArea": [0, 512, 1792, 274],
      "pos": [
        [
          [ -0.95, 2.325, 4.00  ],  // Top left
          [ -0.95, 2.000, 4.00  ],  // Bottom left
          [  0.95, 2.000, 4.00  ],  // Bottom right
          [  0.95, 2.325, 4.00  ]   // Top right
        ]
      ],
      
      "offsets": [
        [0.0, 0.0, 1.85]
      ]
    },
    {
      "name": "display_front_side",
      "texArea": [0, 512, 1792, 274],
      "pos": [
        [
          [ 1.25, 2.250, -2.825 ],  // Top left
          [ 1.25, 2.075, -2.825 ],  // Bottom left
          [ 1.25, 2.075, -4.000 ],  // Bottom right
          [ 1.25, 2.250, -4.000 ]   // Top right
        ]
      ],
      
      "offsets": [
        [0.0, 0.0, -1.71]
      ]
    }
  ]
};

// Displays for odd carriages
const slotReversedCfg = {
  "version": 1,
  "texSize": [1792, 1024],
  "slots": [
    {
      "name": "display_back",
      "texArea": [0, 512, 1792, 274],
      "pos": [
        [
          [  0.95, 2.325, -4.00  ],  // Top left
          [  0.95, 2.000, -4.00  ],  // Bottom left
          [ -0.95, 2.000, -4.00  ],  // Bottom right
          [ -0.95, 2.325, -4.00  ]   // Top right
        ]
      ],
      
      "offsets": [
        [0.0, 0.0, -1.85]
      ]
    },
    {
      "name": "display_back_side",
      "texArea": [0, 512, 1792, 274],
      "pos": [
        [
          [ -1.25, 2.250, 2.825 ],  // Top left
          [ -1.25, 2.075, 2.825 ],  // Bottom left
          [ -1.25, 2.075, 4.000 ],  // Bottom right
          [ -1.25, 2.250, 4.000 ]   // Top right
        ]
      ],
      
      "offsets": [
        [0.0, 0.0, 1.71]
      ]
    }
  ]
};

const dhBase = new DisplayHelper(slotCfg);
const dhReversedBase = new DisplayHelper(slotReversedCfg);
function create(ctx, state, train) {
  state.pisRateLimit = new RateLimit(0.05);
  state.dh = dhBase.create();
  state.dhReversed = dhReversedBase.create();
  //debugVehicle(train);
};
 
function push(ctx, state, train) {
  state.dh.close();
  state.dhReversed.close();
};

const titilliumBold = getFontTitilliumBold()
function render(ctx, state, train) {
  if (state.pisRateLimit.shouldUpdate()) {
    let g;
    
    [
      { dh: state.dh, graphicsName: "display_front"},
      { dh: state.dh, graphicsName: "display_front_side"},
      { dh: state.dhReversed, graphicsName: "display_back"},
      { dh: state.dhReversed, graphicsName: "display_back_side"},
    ].forEach((element) => {
        let dh = element.dh;
        g = dh.graphicsFor(element.graphicsName);

        // Set background
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 2048, 274);
    
        // Square + text
        g.setFont(titilliumBold.deriveFont(Font.PLAIN, 190));
        g.setColor(Color.decode(getRouteColor(train)));
        g.fillRoundRect(50, 20, 220, 220, 75, 85);
        g.setColor(Color.WHITE);
        g.drawString(getRouteChar(train), 110, 190);
    
        // Destination
        g.setColor(Color.BLACK);
        g.setFont(titilliumBold.deriveFont(Font.PLAIN, 180));
        g.drawString(getDestination(train), 330, 190);

        // Update stuff
        dh.upload();
      }
    );
  };

  for (let i = 0; i < train.trainCars(); i++) {
    if (i % 2 !== 0) {
      //Odd car number
      ctx.drawCarModel(state.dhReversed.model, i, null);
    } else {
      //Even car number
      ctx.drawCarModel(state.dh.model, i, null);
    }
  }
};