from dlubal.api import rfem, common
import app.helpers.print_helper as print_helper

def main():
    # Connect to the RFEM application
    with rfem.Application() as rfem_app:
        # --- Design Configuration ---
        concrete_uls_config: rfem.concrete_design_objects.ConcreteDesignUlsConfiguration = rfem_app.get_object(
            obj=rfem.concrete_design_objects.ConcreteDesignUlsConfiguration(no=1)
        )
        # print(f"\CONCRETE_ULS_CONFIG:\n{concrete_uls_config}")

        settings_surface_ec2 = concrete_uls_config.settings_surface_ec2
        print_helper.header1(f"settings_surface_ec2:\n{settings_surface_ec2}")

        # Create empty SettingsSurfaceEc2TreeTable and set only the values that we want to change
        settings_surface_ec2_updated = rfem.concrete_design_objects.ConcreteDesignUlsConfiguration.SettingsSurfaceEc2TreeTable()
        # settings_surface_ec2_updated = settings_surface_ec2
        # ***************************************************************
        chapter = 'surface_internal_forces'
        key1 = 'surface_internal_forces_no_optimization'
        design_path = [
            chapter,
            key1,
        ]
        design_value = False
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )
        key1 = 'surface_internal_forces_optimization'
        design_path = [
            chapter,
            key1,
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )
        chapter = 'limits_of_reinforcement_areas'
        key1 = 'minimum_longitudinal_reinforcement_acc_to_standard'
        design_path = [
            chapter,
            key1,
        ]
        design_value=common.get_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = True
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )
        key2 = 'minimum_longitudinal_reinforcement_for_plates'
        design_path = [
            chapter,
            key1,
            key2,
        ]
        design_value = common.get_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = False
        # print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )
        key2 = 'minimum_longitudinal_reinforcement_for_walls'
        design_path = [
            chapter,
            key1,
            key2,
        ]
        design_value = common.get_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = True
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )

        key3='direction_of_main_compression_reinforcement'
        key4 = 'reinforcement_direction_with_the_main_compression_force'
        design_path = [
            chapter,
            key1,
            key2,
            key3,
            key4
        ]
        design_value=common.get_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = True
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )
        key4 = 'defined_in_reinforcement_direction'
        design_path = [
            chapter,
            key1,
            key2,
            key3,
            key4
        ]
        design_value = common.get_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = False

        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )

        key1 = 'maximum_longitudinal_reinforcement_acc_to_standard'
        design_path = [
            chapter,
            key1,
        ]
        design_value=common.get_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = True
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )
        key2 = 'maximum_longitudinal_reinforcement_for_plates'
        design_path = [
            chapter,
            key1,
            key2,
        ]
        design_value = False
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )
        key2 = 'maximum_longitudinal_reinforcement_for_walls'
        design_path = [
            chapter,
            key1,
            key2,
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_surface_ec2_updated,
            path=design_path,
            value=design_value
        )


        # ***************************************************************
        settings_main_ec2 = concrete_uls_config.settings_main_ec2
        # print_helper.header1(f"settings_main_ec2:\n{settings_main_ec2}")

        # Create empty SettingsMainEc2TreeTable and set only the values that we want to change
        settings_main_ec2_updated = rfem.concrete_design_objects.ConcreteDesignUlsConfiguration.SettingsMainEc2TreeTable()

        # ***************************************************************
        chapter='fatigue_design_check'
        design_path = [
            chapter,
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_main_ec2_updated,
            path=design_path,
            value=design_value
        )
        key1 = 'fatigue_design_simplified_design_method'
        design_path = [
            chapter,
            key1
        ]
        design_value = False
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_main_ec2_updated,
            path=design_path,
            value=design_value
        )
        key1 = 'fatigue_design_method_of_damage_equivalent_stress_range'
        design_path = [
            chapter,
            key1
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_main_ec2_updated,
            path=design_path,
            value=design_value
        )
        key2 = 'fatigue_design_modification_of_the_design_check_of_the_equivalent_steel_stress_range'
        design_path = [
            chapter,
            key1,
            key2
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_main_ec2_updated,
            path=design_path,
            value=design_value
        )
        key3 = 'fatigue_design_use_correction_factor_for_calculated_equivalent_damage_stress_range'
        design_path = [
            chapter,
            key1,
            key2,
            key3
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_main_ec2_updated,
            path=design_path,
            value=design_value
        )
        key3 = 'fatigue_design_use_one_interval_with_defined_number_of_cycles_for_a_stress_range'
        design_path = [
            chapter,
            key1,
            key2,
            key3
        ]
        design_value = False
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_main_ec2_updated,
            path=design_path,
            value=design_value
        )
        key4 = 'n' # int=1000000 fatigue_design_use_one_interval_with_defined_number_of_cycles_for_a_stress_range
        design_path = [
            chapter,
            key1,
            key2,
            key3,
            key4
        ]
        design_value=900000
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_main_ec2_updated,
            path=design_path,
            value=design_value
        )

        key1 = 't_0'  # double=2419200 days in minutes - Start time of cyclic loading on concrete in days
        design_path = [
            chapter,
            key1
        ]
        design_value = 2000000
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_main_ec2_updated,
            path=design_path,
            value=design_value
        )

        # Apply the updated configuration to the model
        rfem_app.update_object(
            obj=rfem.concrete_design_objects.ConcreteDesignUlsConfiguration(
                no=1,
                settings_surface_ec2=settings_surface_ec2_updated,
                settings_main_ec2=settings_main_ec2_updated
            )
        )

if __name__ == '__main__':
    main()