from .logger import Logger
import os

__logger = Logger()

def import_configuration(device_object, json_file_to_import):
    if(not hasattr(device_object, "importConfiguration")):
        __logger.notify("This device doesn't support import and export of configurations")
        return

    data_to_import = ""
    with open(json_file_to_import, "r", encoding="utf-8") as f:
        data_to_import = f.read()

    status = device_object.importConfiguration(data_to_import)
    if(status > 0):
        __logger.notify(f"Configuration from {os.path.abspath(json_file_to_import)} imported successfully and applied to the device.")
    else:
        __logger.error("Import Error; Some parameters may have been already applied.")

def export_configuration(device_object, json_file_to_export):
    if(not hasattr(device_object, "exportConfiguration")):
        __logger.notify("This device doesn't support import and export of configurations")
        return

    data_to_export = device_object.exportConfiguration()

    if(data_to_export == ""):
        __logger.error("Error during export")
    else:
        with open(json_file_to_export, "w") as f:
            f.write(data_to_export)
        __logger.notify(f"Configuration exported to {os.path.abspath(json_file_to_export)}")