# ==================================
# Immatriculation by Zakharov
#
# Update : BARANGER Emmanuel 03/2025
# Update : BARANGER Emmanuel 07/2025
# Update : BARANGER Emmanuel 12/2025
# ==================================

var refresh_immat = func {
  var immat = props.globals.getNode("/sim/multiplay/generic/string[0]",1).getValue();
  var immat_size = size(immat);
  if (immat_size != 0) {
    immat = string.uc(immat);
  }
  for (var i = 0; i < 6; i += 1) {
    if (i >= immat_size) {
      glyph = -1;
    } elsif (string.isupper(immat[i])) {
      glyph = immat[i] - `A`;
    } elsif (string.isdigit(immat[i])) {
      glyph = immat[i] - `0` + 26;
    } else {
      glyph = 36;
    }
    props.globals.getNode("/sim/multiplay/generic/int["~i~"]", 1).setValue(glyph+1);
  }
}

setlistener("/sim/signals/fdm-initialized", func {

  var immat = props.globals.getNode("/sim/multiplay/generic/string[0]",1);

  if (props.globals.getNode("/sim/multiplay/generic/string[0]") == nil) {

    var callsign = props.globals.getNode("/sim/multiplay/callsign").getValue();
    if (callsign != "callsign") {
      immat.setValue(callsign);
    }
  } else {
    immat.setValue("F-HLBL");
  }
  refresh_immat();
  setlistener("/sim/multiplay/generic/string[0]", refresh_immat, 0);

},0);
