from utils.webTextExtractor import WebTextExtractor

class TextData:
    def __init__(self, url: str = "") -> None:
        self.url = url
        self.text = ""
        self.result = ""
        self.confidence = None
        self._extractor = None

    def set_url(self, url: str) -> None:
        self.url = url
        self.text = ""  # Reset text when URL changes
        self._extractor = None  # Reset extractor when URL changes

    def text_from_url(self)-> bool:
        if not self.url:
            print("No url")
            return True

        if not self.text:
            print("Extrahiere Text von URL...")
            self._extractor = WebTextExtractor(self.url)
            self._extractor.fetch_content()
            self._extractor.extract_text()
            self.text = self._extractor.get_text()
            return False
    
    def get_output(self):
        
        if self.confidence != None:
            output = f"Prediction: {self.result}" + f"   Confidence: {self.confidence:.4f}"
            print(output)
            return output