from .neural import training_settings as ts
import os
import sys

## this class checks if all needed parameters are provided in the project settings file
## if not it updates the file with default parameters or prompts to provide them
## if all needed parameters are provided it does nothing


class params_wraper:
    def __init__(self, params_path):
        super().__init__()

        self.default_params_path = os.path.join(
            os.path.dirname(__file__), "default_options.yaml"
        )
        self.default_params = ts.read_settings(self.default_params_path)
        self.params_provided_path = params_path
        self.params_provided = ts.read_settings(self.params_provided_path)
        self.to_save = True
        if self.params_provided is None:
            self.params_provided = {}
        self.updated_provided_params = self.update_params(
            self.params_provided, self.default_params
        )

        if self.to_save == True:
            print(f"saving updated provided parameters to {self.params_provided_path}")
            ts.save_training_settings(
                self.updated_provided_params, self.params_provided_path
            )

    def __repr__(self):
        return self.updated_provided_params

    def update_params(self, params_provided, default_params):

        obligatory_keys = [
            "project_name",
            "project_path",
            "separator",
            "input_data",
            "output_path_full",
            "output_path_hyper_hypo_indeter",
            "output_path_indeter_indeter",
            "output_path_only_hyper_hypo",
        ]
        n = 0
        for aparam in default_params.items():

            if aparam[0] not in params_provided.keys():
                if aparam[0] in obligatory_keys:
                    print(f"Obligatory parameters key not provided - '{aparam[0]}'")
                    sys.exit()

                if n == 0:
                    print("Updating provided parameters with absent keys")
                n = n + 1
                print(aparam)
                params_provided[aparam[0]] = aparam[1]
        if n == 0:
            print("All needed keys in parameters provided - no need to update")
            self.to_save = False

        return params_provided
