var controlpng = Resources.id("mtr:m3_i/control/e233_control.png");
var partpng = Resources.id("mtr:m3_i/control/parts_233.png");
var partpng2 = Resources.id("mtr:m3_i/control/e233_part2.png");
var controlId = Resources.id("mtr:m3_i/m3_19_i_cab_forwards_control.mqo");

var modelArray = [];
var rawModelArray = [];
var uploadArray = ["panel","panel_1-1","panel_1-2","panel_2-1","panel_2-2"];
var dynamicModelHolder = new DynamicModelHolder();
var dynamicnotch = new DynamicModelHolder();
var dynamicnotch_con = new DynamicModelHolder();
var dynamicnotchlever = new DynamicModelHolder();
var bc_display = new DynamicModelHolder();
var dmh_atc_needle = new DynamicModelHolder();
var dmh_digit = new DynamicModelHolder();
var dmh_digit2 = new DynamicModelHolder();
var dmh_digit3 = new DynamicModelHolder();
var dmh_reverser = new DynamicModelHolder();

var controlmodelparts = ModelManager.loadModelParts(controlId, false);
for (var key of controlmodelparts.values()) {
  console.log(key);
  key.setAllRenderType("interior");
  if(SCRIPT_INPUT.Direction == "backwards")key.applyRotation(Vector3f(0, 1, 0), 180);
}

//Model without changes
for (let index = 0; index < uploadArray.length; index++) {
  let element = uploadArray[index];
  let controlModel = ModelManager.upload(controlmodelparts.get(element));
  modelArray.push(controlModel);
}
//Model that needs to be changed

var digit_1_ = [
		controlmodelparts.get("1_0"),
		controlmodelparts.get("1_1"),
		controlmodelparts.get("1_2"),
		controlmodelparts.get("1_3"),
		controlmodelparts.get("1_4"),
		controlmodelparts.get("1_5"),
		controlmodelparts.get("1_6"),
		controlmodelparts.get("1_7"),
		controlmodelparts.get("1_8"),
		controlmodelparts.get("1_9")
	];
var digit_2_ = [
		controlmodelparts.get("1_19"),
		controlmodelparts.get("1_10"),
		controlmodelparts.get("1_11"),
		controlmodelparts.get("1_12"),
		controlmodelparts.get("1_13"),
		controlmodelparts.get("1_14"),
		controlmodelparts.get("1_15"),
		controlmodelparts.get("1_16"),
		controlmodelparts.get("1_17"),
		controlmodelparts.get("1_18")
	];

var digit_3_ = [
		controlmodelparts.get("1_29"),
		controlmodelparts.get("1_20"),
		controlmodelparts.get("1_21"),
		controlmodelparts.get("1_22"),
		controlmodelparts.get("1_23"),
		controlmodelparts.get("1_24"),
		controlmodelparts.get("1_25"),
		controlmodelparts.get("1_26"),
		controlmodelparts.get("1_27"),
		controlmodelparts.get("1_28")
	]


function create(ctx, state, vehicle) { 

}

function render(ctx, state, vehicle) {
      //console.log("Speed:", vehicle.getSpeedKmh());

      //Notch model
      let notch = vehicle.getNotchLevel();
      switch (notch) {
        case 7:
          dynamicnotchlever.uploadLater(controlmodelparts.get("P5"));
          break;
        case 6:
          dynamicnotchlever.uploadLater(controlmodelparts.get("P5"));
          break;
        case 5:
          dynamicnotchlever.uploadLater(controlmodelparts.get("P5"));
          break;
        case 4:
          dynamicnotchlever.uploadLater(controlmodelparts.get("P4"));
          break;
        case 3:
          dynamicnotchlever.uploadLater(controlmodelparts.get("P3"));
          break;
        case 2:
          dynamicnotchlever.uploadLater(controlmodelparts.get("P2"));
          break;
        case 1:
          dynamicnotchlever.uploadLater(controlmodelparts.get("P1"));
          break;
        case 0:
          dynamicnotchlever.uploadLater(controlmodelparts.get("N"));
          break;
        case -1:
          dynamicnotchlever.uploadLater(controlmodelparts.get("B1"));
          break;
        case -2:
          dynamicnotchlever.uploadLater(controlmodelparts.get("B2"));
          break;
        case -3:
          dynamicnotchlever.uploadLater(controlmodelparts.get("B3"));
          break;
        case -4:
          dynamicnotchlever.uploadLater(controlmodelparts.get("B4"));
          break;
        case -5:
          dynamicnotchlever.uploadLater(controlmodelparts.get("B5"));
          break;
        case -6:
          dynamicnotchlever.uploadLater(controlmodelparts.get("B6"));
          break;
        case -7:
          dynamicnotchlever.uploadLater(controlmodelparts.get("B7"));
          break;
        case -8:
          dynamicnotchlever.uploadLater(controlmodelparts.get("EB"));
          break;
      }

      //Reverser
      if(vehicle.isReversed()){
        dmh_reverser.uploadLater(controlmodelparts.get("B"));
      }else{
        dmh_reverser.uploadLater(controlmodelparts.get("F"));
      }

      //Notch+bc Display
      switch (notch) {
        case -1:
          dynamicnotch.uploadLater(controlmodelparts.get("levelB1"));
          bc_display.uploadLater(controlmodelparts.get("bc_B1"));
          break;
        case -2:
          dynamicnotch.uploadLater(controlmodelparts.get("levelB2"));
          dynamicnotch_con.uploadLater(controlmodelparts.get("levelB2_con"));
          bc_display.uploadLater(controlmodelparts.get("bc_B2"));
          break;
        case -3:
          dynamicnotch.uploadLater(controlmodelparts.get("levelB3"));
          dynamicnotch_con.uploadLater(controlmodelparts.get("levelB3_con"));
          bc_display.uploadLater(controlmodelparts.get("bc_B3"));
          break;
        case -4:
          dynamicnotch.uploadLater(controlmodelparts.get("levelB4"));
          dynamicnotch_con.uploadLater(controlmodelparts.get("levelB4_con"));
          bc_display.uploadLater(controlmodelparts.get("bc_B4"));
          break;
        case -5:
          dynamicnotch.uploadLater(controlmodelparts.get("levelB5"));
          dynamicnotch_con.uploadLater(controlmodelparts.get("levelB5_con"));
          bc_display.uploadLater(controlmodelparts.get("bc_B5"));
          break;
        case -6:
          dynamicnotch.uploadLater(controlmodelparts.get("levelB6"));
          dynamicnotch_con.uploadLater(controlmodelparts.get("levelB6_con"));
          bc_display.uploadLater(controlmodelparts.get("bc_B6"));
          break;
        case -7:
          dynamicnotch.uploadLater(controlmodelparts.get("levelB7"));
          dynamicnotch_con.uploadLater(controlmodelparts.get("levelB7_con"));
          bc_display.uploadLater(controlmodelparts.get("bc_B7"));
          break;
        case -8:
          dynamicnotch.uploadLater(controlmodelparts.get("levelEB"));
          bc_display.uploadLater(controlmodelparts.get("bc_EB"));
          break;
        default:
          bc_display.uploadLater(controlmodelparts.get("bc_N"));
        }
      
      //Speed Display
      let speed = vehicle.getSpeedKmh();
      let nowSpeed = (speed >= 0) ? Math.round(speed) : Math.round(-speed - 1);
      let a_1 = parseInt(nowSpeed).toString();
      if(nowSpeed >= 0)dmh_digit.uploadLater(digit_1_[parseInt(a_1.charAt(a_1.length - 1).toString())]);
      if(nowSpeed >= 10)dmh_digit2.uploadLater(digit_2_[parseInt(a_1.charAt(a_1.length - 2).toString())]);
      if(nowSpeed >= 100)dmh_digit3.uploadLater(digit_3_[parseInt(a_1.charAt(a_1.length - 3).toString())]);
      //Speed Needle Mechanism
      
      let rotateAngle = (speed >= 0)?(Math.round(speed) / 140) * 248 : (Math.round(-speed) / 140) * 248;      
      let needleRotated = controlmodelparts.get("speed_needle").copy();
      let needleaxis = [0.893, 1.032, 10.5398];
      let rotateAxis = Vector3f(0, 0, -1);
      if(SCRIPT_INPUT.Direction == "backwards")needleaxis = [-0.893, 1.032, -10.5398];
      if(SCRIPT_INPUT.Direction == "backwards")rotateAxis = Vector3f(0, 0, 1);
      needleRotated.applyTranslation(needleaxis[0]*-1, needleaxis[1]*-1, needleaxis[2]*-1);
      needleRotated.applyRotation(rotateAxis, rotateAngle);
      needleRotated.applyTranslation(needleaxis[0], needleaxis[1], needleaxis[2]);           
      dynamicModelHolder.uploadLater(needleRotated);

      //ATC speed Mechanism
      let targetspeed = vehicle.getMtrVehicle().vehicleExtraData.getSpeedTarget()*1000*3.6;
      if(targetspeed == -3600)targetspeed = 0;
      let rotateATCAngle = (targetspeed >= 0)?(Math.round(targetspeed) / 140) * 248 : (Math.round(-targetspeed) / 140) * 248;      
      let atc_needleRotated = controlmodelparts.get("ATC_needle").copy();
      atc_needleRotated.applyTranslation(needleaxis[0]*-1, needleaxis[1]*-1, needleaxis[2]*-1);
      atc_needleRotated.applyRotation(rotateAxis, rotateATCAngle);
      atc_needleRotated.applyTranslation(needleaxis[0], needleaxis[1], needleaxis[2]);           
      dmh_atc_needle.uploadLater(atc_needleRotated);

      //render mechanism
      for(let carIndex of ctx.getMyCars()) {
          let renderManager = ctx.getCarRenderManager(carIndex);
          modelArray.forEach(element => {
            renderManager.drawModel(element, null); // Draw the model out
            renderManager.drawModel(dynamicModelHolder, null); // Draw the model out
            renderManager.drawModel(bc_display, null); // Draw the model out
            renderManager.drawModel(dmh_atc_needle, null); // Draw the model out
            if(nowSpeed >= 0)renderManager.drawModel(dmh_digit, null);
            if(nowSpeed >= 10)renderManager.drawModel(dmh_digit2, null);
            if(nowSpeed >= 100)renderManager.drawModel(dmh_digit3, null);
            if (notch <= -1 && notch >= -8){
              renderManager.drawModel(dynamicnotch, null); // Draw the model out
            };
            if (notch <= -2 && notch >= -7){
              renderManager.drawModel(dynamicnotch_con, null); // Draw the model out
            };
            if (notch <= 7 && notch >= -8){
              renderManager.drawModel(dynamicnotchlever, null); // Draw the model out
            };
            renderManager.drawModel(dmh_reverser, null); // Draw the model out
          });
      }
    
}

function dispose(ctx, state, vehicle) {

};



