import os

# Some basic resource management
# Required folder structure:
# - project_root
#       - out               results
#       - embeddings        VSMs
#       - corpora           training and test data
#       - lexicons          lexicon lists

class ResourceManager:
    def __init__(self, root):
        self.root = root
        self.out = os.path.join('out')
        self.vsm_folder = os.path.join(self.root, 'embeddings')
        self.vsm_imaginedModel_folder = os.path.join(self.vsm_folder, 'imagined')
        self.corpora = os.path.join(self.root, 'corpora')
        self.lexicons = os.path.join(self.root, 'lexicons')
        self.datasets = os.path.join(self.root, 'datasets')
        self.salsa = os.path.join(self.root, 'raw', 'salsa')
        self.framenet = os.path.join(self.root, 'raw', 'framenet')
        self.wasr = os.path.join(self.root, 'raw', 'wasr')        
        self.fnwk = os.path.join(self.root, 'raw', 'fnwk')        
        self.raw_embeddings = os.path.join(self.root, 'raw', 'embeddings')
        self.statistics = os.path.join(self.out, 'statistics')
        self.experiments = os.path.join(self.out, 'experiments')
        self.evaluations = os.path.join(self.out, 'evaluations')
        self.plots = os.path.join(self.out, 'plots')
        self.models = os.path.join(self.out, 'models')

    def get_corpus(self, corpus_name):
        return list(os.path.join(self.corpora, corpus_name+x) for x in ['.all.lemma.tags', '.frame.elements'])

    def get_frame_annotations_file(self, corpus_name):
        return os.path.join(self.corpora, corpus_name + '.frame.elements')

    def get_sentences_file(self, corpus_name):
        return os.path.join(self.corpora, corpus_name + '.all.lemma.tags')
    
    def get_babelfied_synset_annotations_file(self, corpus_name):
        return os.path.join(self.corpora, corpus_name + '.babelfied')

    def get_lexicon(self, lexicon_name):
        return os.path.join(self.lexicons, lexicon_name) if lexicon_name is not None else None

    def get_vsm(self, vsm_name):
        return os.path.join(self.vsm_folder, vsm_name) if vsm_name is not None else None
    
    def get_imaginedModel_for_vsm(self, imaginedModel_name):
        return os.path.join(self.vsm_imaginedModel_folder, imaginedModel_name) if imaginedModel_name is not None else None

    def get_dataset(self, corpus_name, embedding):
        dataset_name = '{0}_{1}.pkl.xz'.format(corpus_name, os.path.splitext(embedding)[0])
        return os.path.join(self.datasets, dataset_name) if dataset_name is not None else None
    
    def get_multimodal_dataset(self, corpus_name, list_embeddingNames):
        embeddings_vsm_name_concatenatedString = ""
        for embeddingName in list_embeddingNames:
            embeddings_vsm_name_concatenatedString += os.path.splitext(embeddingName)[0]
        dataset_name = '{0}_{1}.pkl.xz'.format(corpus_name, embeddings_vsm_name_concatenatedString)
        return os.path.join(self.datasets, dataset_name) if dataset_name is not None else None

    def get_salsa(self, salsa_artifact):
        return os.path.join(self.salsa, salsa_artifact)

    def get_framenet(self, *framenet_artifacts):
        return os.path.join(self.framenet, *framenet_artifacts)

    def get_wasr(self, wasr_artifact):
        return os.path.join(self.wasr, wasr_artifact)

    def get_fnwk(self, fnwk_artifact):
        return os.path.join(self.fnwk, fnwk_artifact)        

    def get_raw_embeddings(self, embedding_name):
        return os.path.join(self.raw_embeddings, embedding_name)

    def get_statistics(self, statistics_name):
        return os.path.join(self.statistics, statistics_name)

    def get_experiment(self, experiment_name):
        return os.path.join(self.experiments, experiment_name)

    def get_evaluation(self, evaluation_name):
        return os.path.join(self.evaluations, evaluation_name) 

    def get_plot(self, plot_name):
        return os.path.join(self.plots, plot_name) 

    def get_model(self, model_name):
        return os.path.join(self.models, model_name)         

    def get_folders(self):
        return [self.out, self.vsm_folder, self.corpora, self.lexicons, self.datasets, self.salsa, 
        self.framenet, self.wasr, self.fnwk, self.raw_embeddings, self.statistics, self.experiments,
        self.evaluations, self.plots, self.models]

    def create_folder_structure(self):
        folders = self.get_folders()
        for folder in folders:        
            os.makedirs(folder, exist_ok=True)        