from views.mainScreen import MainFrame
from models.data import TextData
from Ai.interence import VeraMindInference
class MainFrameController:
    
    def __init__(self,frame:MainFrame) -> None:
        self.frame = frame
        
        
    def get_textdata(self) -> TextData:
        text_data = TextData()
        text_data.url = self.frame.entry_url.get()
        if text_data.text_from_url():
            text_data.text = self.frame.input_textbox.get("0.0", "end")
        
        return text_data
    
    def press_check_button(self):
        text_data = self.get_textdata()
        print(f"text:{text_data.text}")
        self.prediction(text_data)
        self.frame.output_textbox.configure(state="normal")
        self.frame.output_textbox.delete("0.0", "end")
        self.frame.output_textbox.insert("0.0",f"{text_data.get_output()}")
        self.frame.output_textbox.configure(state="disabled")
        
    def prediction(self, text_data:TextData) -> TextData:
        inference = VeraMindInference('VeraMind-Mini')
        result = inference.predict(text_data.text)
        text_data.confidence = result["confidence"]
        text_data.isfake_news = result["result"] 
        print(f"Prediction: {'Real' if text_data.isfake_news == 1 else 'Fake'}")
        print(f"Confidence: {text_data.confidence}")
        return text_data