import unittest

import openmdao.api as om
from parameterized import parameterized

from aviary.subsystems.mass.flops_based.air_conditioning import (
    AltAirCondMass, TransportAirCondMass)
from aviary.subsystems.propulsion.propulsion_premission import PropulsionPreMission
from aviary.utils.test_utils.variable_test import assert_match_varnames
from aviary.validation_cases.validation_tests import (Version,
                                                      flops_validation_test,
                                                      get_flops_case_names,
                                                      get_flops_inputs,
                                                      print_case)
from aviary.variable_info.variables import Aircraft, Mission


class TransportAirCondMassTest(unittest.TestCase):

    def setUp(self):
        self.prob = om.Problem()

    @parameterized.expand(get_flops_case_names(),
                          name_func=print_case)
    def test_case(self, case_name):

        prob = self.prob

        prob.model.add_subsystem(
            "air_cond",
            TransportAirCondMass(aviary_options=get_flops_inputs(case_name)),
            promotes_inputs=['*'],
            promotes_outputs=['*'],
        )

        prob.setup(check=False, force_alloc_complex=True)

        flops_validation_test(
            prob,
            case_name,
            input_keys=[Aircraft.AirConditioning.MASS_SCALER,
                        Aircraft.Avionics.MASS,
                        Aircraft.Fuselage.MAX_HEIGHT,
                        Aircraft.Fuselage.PLANFORM_AREA],
            output_keys=Aircraft.AirConditioning.MASS,
            aviary_option_keys=[Aircraft.CrewPayload.NUM_PASSENGERS],
            version=Version.TRANSPORT,
            tol=3.0e-4,
            atol=1e-11)

    def test_IO(self):
        assert_match_varnames(self.prob.model)


class AltAirCondMassTest(unittest.TestCase):
    '''
    Tests alternate air conditioning mass calculation.
    '''

    def setUp(self):
        self.prob = om.Problem()

    @parameterized.expand(get_flops_case_names(),
                          name_func=print_case)
    def test_case(self, case_name):

        prob = self.prob

        prob.model.add_subsystem(
            'air_cond',
            AltAirCondMass(aviary_options=get_flops_inputs(case_name)),
            promotes_inputs=['*'],
            promotes_outputs=['*'],
        )

        prob.setup(check=False, force_alloc_complex=True)

        flops_validation_test(
            prob,
            case_name,
            input_keys=Aircraft.AirConditioning.MASS_SCALER,
            output_keys=Aircraft.AirConditioning.MASS,
            aviary_option_keys=Aircraft.CrewPayload.NUM_PASSENGERS,
            version=Version.ALTERNATE)

    def test_IO(self):
        assert_match_varnames(self.prob.model)


if __name__ == '__main__':
    unittest.main()
