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


def main():
    with rfem.Application() as rfem_app:
        # Check subscription info
        print(rfem_app.get_subscription_info())

        # Steel Design Configuration
        steel_uls_config: rfem.steel_design_objects.SteelDesignUlsConfiguration = rfem_app.get_object(
            obj=rfem.steel_design_objects.SteelDesignUlsConfiguration(no=1)
        )
        # Get existing SettingsEc2TreeTable from ULS Configuration to get Table structure only
        # print_helper.header1(f"\steel_uls_config:\n{steel_uls_config}")

        # Create empty SettingsEc2TreeTable and set only the values that we want to change
        print_helper.header1_top(f'steel_uls_config: no={steel_uls_config.no} | {steel_uls_config.name}')
        settings_ec3 = steel_uls_config.settings_ec3
        print_helper.header1_top(f'settings_ec3')
        # settings_ec3 = rfem.steel_design_objects.SteelDesignUlsConfiguration.SettingsEc3TreeTable()
        # print(f'settings_ec3=\n{str(steel_uls_config.settings_ec3)}')
        print(f'steel_uls_config=\n{str(steel_uls_config)}')
        chapter = 'general_options'
        key1 = 'perform_stability_analysis'
        design_path = [
            chapter,
            key1,
        ]
        # design_value=common.get_tree_value(
        #     tree=settings_ec3,
        #     path=design_path,
        # )
        # print(f"{design_path}: {design_value}")
        design_value = False
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_ec3,
            path=design_path,
            value=design_value
        )
        key1 = 'perform_advanced_plastic_design'
        design_path = [
            chapter,
            key1,
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_ec3,
            path=design_path,
            value=design_value
        )
        chapter = 'options'
        key1 = 'options_elastic_design_root'
        key2 = 'options_elastic_design'
        design_path = [
            chapter,
            key1,
            key2,
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_ec3,
            path=design_path,
            value=design_value
        )
        key2 = 'options_use_verification_for_elastic_design'
        design_path = [
            chapter,
            key1,
            key2
        ]
        design_value = False
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_ec3,
            path=design_path,
            value=design_value
        )

        chapter = 'use_advanced_plastic_design_for'
        key1 = 'pd_section_class_1_and_2_only'
        design_path = [
            chapter,
            key1,
        ]
        design_value=common.get_tree_value(
            tree=settings_ec3,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = True
        common.set_tree_value(
            tree=settings_ec3,
            path=design_path,
            value=design_value
        )
        key1 = 'pd_all_section_classes'
        design_path = [
            chapter,
            key1,
        ]
        design_value = False
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_ec3,
            path=design_path,
            value=design_value
        )
        chapter = 'method_of_advanced_plastic_design'
        key1 = 'pd_method_pifm_with_redistribution'
        design_path = [
            chapter,
            key1,
        ]
        design_value = True
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=settings_ec3,
            path=design_path,
            value=design_value
        )

        surfaces_settings_ec3 = steel_uls_config.surfaces_settings_ec3
        print_helper.header1_top(f'surfaces_settings_ec3')
        # surfaces_settings_ec3 = rfem.steel_design_objects.SteelDesignUlsConfiguration.SurfacesSettingsEc3TreeTable()
        # print(f'surfaces_settings_ec3=\n{surfaces_settings_ec3}')
        chapter = 'limit_values_for_special_cases'
        key1 = 'eta_sigma_lim'
        design_path = [
            chapter,
            key1,
        ]
        design_value = common.get_tree_value(
            tree=surfaces_settings_ec3,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = 0.002 # default 0.001
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=surfaces_settings_ec3,
            path=design_path,
            value=design_value
        )

        standard_params_ec3=steel_uls_config.standard_parameters_tree
        print_helper.header1_top(f'standard_params_ec3')
        # print_helper.header1_top(f'standard_params_ec3=\n{standard_params_ec3}')

        chapter = 'stainless_steel_acc_to_en_1993_1_4'
        key1 = '5_ultimate_limit_state_uls'
        key2 = '5_6_shear_resistance'
        key3 = 'eta'
        design_path = [
            chapter,
            key1,
            key2,
            key3
        ]
        design_value=common.get_tree_value(
            tree=standard_params_ec3,
            path=design_path,
        )
        print(f"{design_path}: {design_value}")
        design_value = 1.5
        print(f"{design_path}: {design_value}")
        common.set_tree_value(
            tree=standard_params_ec3,
            path=design_path,
            value=design_value
        )
        # Apply the updated configuration to the model
        steel_uls_config.ClearField('standard_parameters')
        steel_uls_config.ClearField('standard_parameters_tree')
        rfem_app.update_object(
            obj=rfem.steel_design_objects.SteelDesignUlsConfiguration(
                no=1,
                settings_ec3=settings_ec3,
                # standard_parameters_tree=standard_params_ec3,
                surfaces_settings_ec3=surfaces_settings_ec3,
            )
        )

if __name__ == '__main__':
    main()