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 the first carriage
const slotCfg = {
  "version": 1,
  "texSize": [1408, 128],
  "slots": [
    {
      "name": "display_front",
      "texArea": [0, 0, 1408, 128],
      "pos": [
        [
          [ -0.875, 2.210, 3.975  ],  // Top left
          [ -0.875, 1.940, 4.000  ],  // Bottom left
          [  0.875, 1.940, 4.000  ],  // Bottom right
          [  0.875, 2.210, 3.975  ]   // Top right
        ]
      ],
      
      "offsets": [
        [0.0, 0.0, 1.91]
      ]
    }
  ]
};

// Displays for middle carriage
const slotMiddledCfg = {
  "version": 1,
  "texSize": [640, 128],
  "slots": [
    {
      "name": "display_side",
      "texArea": [0, 0, 640, 128],
      "pos": [
        [
          [  1.19, 2.100,  0.285  ],  // Top left
          [  1.19, 1.945,  0.285  ],  // Bottom left
          [  1.19, 1.945, -0.285  ],  // Bottom right
          [  1.19, 2.100, -0.285  ]   // Top right
        ]
      ],
      
      "offsets": [
        [0.0, 0.0, 1.07]
      ]
    },
    {
      "name": "display_side",
      "texArea": [0, 0, 640, 128],
      "pos": [
        [
          [ -1.18, 2.100, -0.285  ],  // Top left
          [ -1.18, 1.945, -0.285  ],  // Bottom left
          [ -1.18, 1.945,  0.285  ],  // Bottom right
          [ -1.18, 2.100,  0.285  ]   // Top right
        ]
      ],
      
      "offsets": [
        [0.0, 0.0, 1.07]
      ]
    }
  ]
};

// Displays for the last carriage
const slotReversedCfg = {
  "version": 1,
  "texSize": [1408, 128],
  "slots": [
    {
      "name": "display_back",
      "texArea": [0, 0, 1408, 128],
      "pos": [
        [
          [  0.875, 2.210, -3.975  ],  // Top left
          [  0.875, 1.940, -4.000  ],  // Bottom left
          [ -0.875, 1.940, -4.000  ],  // Bottom right
          [ -0.875, 2.210, -3.975  ]   // Top right
        ]
      ],
      
      "offsets": [
        [0.0, 0.0, -1.91]
      ]
    }
  ]
};

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

const displayFront = getFontDisplay(false);
const displaySide = getFontDisplay(true);
function render(ctx, state, train) {
  if (state.pisRateLimit.shouldUpdate()) {
    let g;
    
    [
      { dh: state.dh, graphicsName: "display_front"},
      { dh: state.dhMiddled, graphicsName: "display_side"},
      { dh: state.dhReversed, graphicsName: "display_back"},
    ].forEach((element) => {
        let dh = element.dh;
        if (dh === state.dhMiddled) {
          g = dh.graphicsFor(element.graphicsName);
  
          // Set background
          g.setColor(Color.decode("#141414"));
          g.fillRect(0, 0, 640, 128);
      
          // Destination
          let destinationText = getDestinationWithoutAccents(train).toUpperCase();
          let fm = g.getFontMetrics();
          let textWidth = fm.stringWidth(destinationText);
          let areaWidth = 640;
          let x = (areaWidth - textWidth) / 2;
  
          g.setColor(Color.decode("#ED1C24"));
          g.setFont(displaySide.deriveFont(Font.PLAIN, 64));
          g.drawString(destinationText, x, 88)
  
          // Update stuff
          dh.upload();
        } else {
          g = dh.graphicsFor(element.graphicsName);
  
          // Set background
          g.setColor(Color.decode("#251E1B"));
          g.fillRect(0, 0, 1408, 128);
      
          // Destination
          let destinationText = getRouteNumber(train, true) + getDestinationWithoutAccents(train).toUpperCase();
          let fm = g.getFontMetrics();
          let textWidth = fm.stringWidth(destinationText);
          let areaWidth = 1408;
          let x = (areaWidth - textWidth) / 2;
  
          g.setColor(Color.decode("#ED1C24"));
          g.setFont(displayFront.deriveFont(Font.PLAIN, 112));
          g.drawString(destinationText, x, 117)
  
          // Update stuff
          dh.upload();
        }
      }
    );
  };

  for (let i = 0; i < train.trainCars(); i++) {
    if (i === 0) {
      ctx.drawCarModel(state.dh.model, i, null);
    } else if (i === train.trainCars() - 1) {
      ctx.drawCarModel(state.dhReversed.model, i, null);
    } else {
      ctx.drawCarModel(state.dhMiddled.model, i, null);
    }
  };
};