import urllib.request
import os
import os
import re
import base64
from pathlib import Path
from typing import Union, Any
import numpy as np

from pathlib import Path

class MimicText:

    def __init__(self):
        self.modelFolderUrl = 'https://speedpresta.s3.us-east-1.amazonaws.com/mimicx'
        self.modelNameBase = 'mimicx_'
        self.module = 'mimictext'
        self.module_dir = os.path.dirname(__file__)
        self.algorithms_directory = self.module_dir
        self.model = None
        self.dataType = None

    def set_model(self, algorithm):

        # Model Grammar Corrector
        if algorithm == "grammar_corrector":

            try: 
                from .. import mimicx_grammar_corrector
            except:
                import mimicx_grammar_corrector
    
            self.model =  mimicx_grammar_corrector.MimicxGrammarCorrector()

        # Model Text Sentiment
        if algorithm == "text_sentiment":

            try: 
                from .. import mimicx_text_sentiment
            except:
                import mimicx_text_sentiment
    
            self.model =  mimicx_text_sentiment.MimicxTextSentiment()


        # Model Nlp Intent Classifier
        if algorithm == "nlp_intent_classifier":

            try: 
                from .. import mimicx_nlp_intent_classifier
            except:
                import mimicx_nlp_intent_classifier
    
            self.model =  mimicx_nlp_intent_classifier.MimicxNlpIntentClassifier()


        # Model Wariflow Industry Classifier
        if algorithm == "wariflow_industry_classifier":

            try: 
                from .. import mimicx_wariflow_industry_classifier
            except:
                import mimicx_wariflow_industry_classifier
    
            self.model =  mimicx_wariflow_industry_classifier.MimicxIndustryClassifier()


    def load_model_file(self, model):
        import platform
        
        # Check and Create Algorithms Directory
        if not os.path.exists(self.algorithms_directory):
            os.makedirs(self.algorithms_directory)

        # Determine file extension based on platform
        if platform.system() == "Windows":
            extension = '.pyd'
        else:
            extension = '.so'
        
        file_path = os.path.join(self.algorithms_directory, self.modelNameBase + model + extension)
        
        if os.path.exists(file_path):
            self.set_model(model)
        else:
            url = self.modelFolderUrl + '/' + self.module + '/' + self.modelNameBase + model + extension
            urllib.request.urlretrieve(url, file_path)
            self.set_model(model)

    def load(self, algorithm):
        self.load_model_file(algorithm)
        try:
            self.load_model_file(algorithm)
        except:
            return 'The model could not be loaded correctly. Please ensure it is named properly and check that it exists.'


######### Grammar Corrector
    def correct_text(self, text):

        if not self.model:
            return 'No model specified!'
        
        return self.model.correct_text(text)
  
######### Text Sentiment

    def analyze_sentiment(self, text):

        if not self.model:
            return 'No model specified!'
        
        return self.model.analyze_sentiment(text)
    

    def get_sentiment_label(self, score):

        if not self.model:
            return 'No model specified!'
        
        return self.model.get_sentiment_label(score)
    

######### Nlp Intent Classifier

    def train_intent_classifier(self, dataset):

        if not self.model:
            return 'No model specified!'
        
        return self.model.train(dataset)
    
    def predict_intent_with_confidence(self, text, threshold=0.3):

        if not self.model:
            return 'No model specified!'
        
        return self.model.predict_with_confidence(text, threshold)
    
    def predict_intent(self, text):

        if not self.model:
            return 'No model specified!'
        
        return self.model.predict_intent(text) 

    def get_intent_vocabulary(self):

        if not self.model:
            return 'No model specified!'
        
        return self.model.get_vocabulary()
    
    def get_intents(self):

        if not self.model:
            return 'No model specified!'
        
        return self.model.get_intents()
    


######### Wariflow Industry Classifier

    def predict_industry(self, text):

        if not self.model:
            return 'No model specified!'
        
        return self.model.predict_industry(text)




