"""Estimation of power systems center(s) of gravity."""

#  This file is part of FAST-OAD_CS23 : A framework for rapid Overall Aircraft Design
#  Copyright (C) 2022  ONERA & ISAE-SUPAERO
#  FAST is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.

import fastoad.api as oad
import numpy as np
from openmdao.core.explicitcomponent import ExplicitComponent

from ..constants import SUBMODEL_POWER_SYSTEMS_CG


@oad.RegisterSubmodel(
    SUBMODEL_POWER_SYSTEMS_CG, "fastga.submodel.weight.cg.system.power_system.legacy"
)
class ComputePowerSystemsCG(ExplicitComponent):
    # TODO: Document equations. Cite sources
    """Power systems center(s) of gravity estimation."""

    # pylint: disable=missing-function-docstring
    # Overriding OpenMDAO setup
    def setup(self):
        self.add_input("data:geometry:fuselage:length", val=np.nan, units="m")
        self.add_input("data:geometry:fuselage:front_length", val=np.nan, units="m")
        self.add_input("data:geometry:fuselage:rear_length", val=np.nan, units="m")

        self.add_output("data:weight:systems:power:electric_systems:CG:x", units="m")
        self.add_output("data:weight:systems:power:hydraulic_systems:CG:x", units="m")

        self.declare_partials(
            of="*",
            wrt=["data:geometry:fuselage:length", "data:geometry:fuselage:front_length"],
            val=0.5,
        )
        self.declare_partials(
            of="*",
            wrt=["data:geometry:fuselage:rear_length"],
            val=-0.5,
        )

    # pylint: disable=missing-function-docstring, unused-argument
    # Overriding OpenMDAO compute, not all arguments are used
    def compute(self, inputs, outputs, discrete_inputs=None, discrete_outputs=None):
        fus_length = inputs["data:geometry:fuselage:length"]
        lav = inputs["data:geometry:fuselage:front_length"]
        lar = inputs["data:geometry:fuselage:rear_length"]

        # Electric system gravity center, formula based on the fact that on a Cirrus SR22,
        # one battery is in front of the firewall while the other one is behind the pressure
        # bulkhead
        x_cg_c12 = lav + 0.5 * (fus_length - (lav + lar))
        # Hydraulic system gravity center
        x_cg_c13 = lav + 0.5 * (fus_length - (lav + lar))

        outputs["data:weight:systems:power:electric_systems:CG:x"] = x_cg_c12
        outputs["data:weight:systems:power:hydraulic_systems:CG:x"] = x_cg_c13
