/**
   * A first guess value of center of gravity location of the whole aircraft.
   *
   * @author Lorenzo Attanasio
   * @param aircraft
   */
  public void calculateBalance(Aircraft aircraft) {

    _xCoGMeanAtOEM =
        (aircraft.get_wing().get_X0().getEstimatedValue()
            + 0.25 * aircraft.get_wing().get_meanAerodChordActual().getEstimatedValue());
    set_xCoGMaxAftAtOEM((_xCoGMeanAtOEM * (1 - 0.1)));
  }
  /**
   * Evaluate center of gravity location of each component.
   *
   * @param aircraft
   * @param conditions
   * @param methodsMap
   */
  public void calculateBalance(
      Aircraft aircraft,
      OperatingConditions conditions,
      Map<ComponentEnum, List<MethodEnum>> methodsMap) {

    aircraft.get_fuselage().calculateCG(aircraft, conditions);

    aircraft.get_wing().calculateCG(aircraft, conditions);
    aircraft.get_HTail().calculateCG(aircraft, conditions);
    aircraft.get_VTail().calculateCG(aircraft, conditions);

    aircraft.get_theNacelles().calculateCG();

    aircraft.get_theFuelTank().calculateCG(aircraft, conditions);

    aircraft.get_landingGear().calculateCG(aircraft, conditions);

    // --- END OF STRUCTURE MASS-----------------------------------

    aircraft.get_powerPlant().calculateCG();

    calculateTotalCG(aircraft);

    set_xCoGMeanAtOEM(
        aircraft.get_wing().get_X0().getEstimatedValue()
            + 0.25 * aircraft.get_wing().get_meanAerodChordActual().getEstimatedValue());
    set_xCoGMaxAftAtOEM(get_xCoGMeanAtOEM() * (1 - 0.1));
    set_xCoGMaxForAtOEM(get_xCoGMeanAtOEM() * (1 + 0.1));
  }
  /**
   * Evaluate overall CG
   *
   * @param aircraft
   */
  public void calculateTotalCG(Aircraft aircraft) {

    // Structural CG
    _cgStructure = new CenterOfGravity();

    _cgList.add(aircraft.get_fuselage().get_cg());
    _cgList.add(aircraft.get_wing().get_cg());
    _cgList.add(aircraft.get_HTail().get_cg());
    _cgList.add(aircraft.get_VTail().get_cg());
    _cgList.add(aircraft.get_landingGear().get_cg());
    _cgList.addAll(aircraft.get_theNacelles().get_cgList());

    System.out.println("\n \nCG COMPONENTS LOCATION IN BRF");
    System.out.println("fuselage --> " + _cgList.get(0).get_xBRF());
    System.out.println("wing --> " + _cgList.get(1).get_xBRF());
    System.out.println("HTail --> " + _cgList.get(2).get_xBRF());
    System.out.println("VTail --> " + _cgList.get(3).get_xBRF());
    System.out.println("Landing gear --> " + _cgList.get(4).get_xBRF());
    for (int i = 0; i < aircraft.get_theNacelles().get_cgList().size(); i++) {
      System.out.println("Nacelle  " + i + " --> " + _cgList.get(i + 5).get_xBRF());
    }

    //		 _cgList.forEach(p-> System.out.println(p.get_xBRF()));

    double prod = 0., sum = 0.;
    for (int i = 0; i < _cgList.size(); i++) {

      prod +=
          _cgList.get(i).get_xBRF().getEstimatedValue()
              * aircraft.get_weights().get_massStructureList().get(i).getEstimatedValue();
      sum += aircraft.get_weights().get_massStructureList().get(i).getEstimatedValue();
    }

    _cgStructure.set_xBRF(Amount.valueOf(prod / sum, SI.METER));

    _cgStructure.calculateCGinMAC(
        aircraft.get_wing().get_xLEMacActualBRF(),
        aircraft.get_wing().get_xLEMacActualBRF(),
        Amount.valueOf(0., SI.METER),
        aircraft.get_wing().get_meanAerodChordActual());

    // Structure + engines CG
    _cgStructureAndPower = new CenterOfGravity();

    System.out.println(
        "fuel tank --> " + aircraft.get_theFuelTank().get_cg().get_xBRF().getEstimatedValue());
    double cgPowerPlantContribute = 0.0;

    for (int i = 0; i < aircraft.get_powerPlant().get_engineNumber(); i++) {
      cgPowerPlantContribute =
          cgPowerPlantContribute
              + (aircraft.get_powerPlant().get_cgList().get(i).get_xBRF().getEstimatedValue()
                  * aircraft
                      .get_powerPlant()
                      .get_engineList()
                      .get(i)
                      .get_totalMass()
                      .getEstimatedValue());
      System.out.println(
          "Engine " + i + " --> " + aircraft.get_powerPlant().get_cgList().get(i).get_xBRF());
    }
    _cgStructureAndPower.set_xBRF(
        Amount.valueOf(
            (cgPowerPlantContribute
                    + aircraft.get_weights().get_structuralMass().getEstimatedValue()
                        * get_cgStructure().get_xBRF().getEstimatedValue())
                / (aircraft.get_weights().get_structuralMass().getEstimatedValue()
                    + aircraft.get_powerPlant().get_totalMass().getEstimatedValue()),
            SI.METER));

    get_cgStructureAndPower()
        .calculateCGinMAC(
            aircraft.get_wing().get_xLEMacActualBRF(),
            aircraft.get_wing().get_xLEMacActualBRF(),
            Amount.valueOf(0., SI.METER),
            aircraft.get_wing().get_meanAerodChordActual());

    // MZFW CG location
    _cgMZFM = new CenterOfGravity();

    get_cgMZFM()
        .set_xBRF(
            Amount.valueOf(
                (get_cgStructureAndPower().get_xBRF().getEstimatedValue()
                            * aircraft.get_weights().get_OEM().getEstimatedValue()
                        + aircraft.get_configuration().get_seatsCoG().getEstimatedValue()
                            * aircraft.get_weights().get_paxMassMax().getEstimatedValue())
                    / (aircraft.get_weights().get_paxMassMax().getEstimatedValue()
                        + aircraft.get_weights().get_OEM().getEstimatedValue()),
                SI.METER));

    get_cgMZFM()
        .calculateCGinMAC(
            aircraft.get_wing().get_xLEMacActualBRF(),
            aircraft.get_wing().get_xLEMacActualBRF(),
            Amount.valueOf(0., SI.METER),
            aircraft.get_wing().get_meanAerodChordActual());

    // MTOM CG location
    _cgMTOM = new CenterOfGravity();

    _cgMTOM.set_xBRF(
        Amount.valueOf(
            (_cgMZFM.get_xBRF().getEstimatedValue()
                        * aircraft.get_weights().get_MZFM().getEstimatedValue()
                    + aircraft.get_theFuelTank().get_fuelMass().getEstimatedValue()
                        * aircraft.get_theFuelTank().get_cg().get_xBRF().getEstimatedValue())
                / aircraft.get_weights().get_MTOM().getEstimatedValue(),
            SI.METER));

    _cgMTOM.calculateCGinMAC(
        aircraft.get_wing().get_xLEMacActualBRF(),
        aircraft.get_wing().get_xLEMacActualBRF(),
        Amount.valueOf(0., SI.METER),
        aircraft.get_wing().get_meanAerodChordActual());
  }