from typing import Any
import customtkinter as ctk


class MainFrame(ctk.CTkFrame):   
    def __init__(self, master: Any, **kwargs):
        super().__init__(master, **kwargs)
        self.controller = None
        # Configure the main frame to stretch
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)  # Left column should stretch
        self.grid_columnconfigure(1, weight=0)  # Middle column (button) should not stretch
        self.grid_columnconfigure(2, weight=1)  # Right column should stretch
        
        # Left frame
        self.frame1 = ctk.CTkFrame(self)
        self.frame1.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
        self.frame1.grid_rowconfigure(3, weight=1)
        self.frame1.grid_columnconfigure(0, weight=1)
        
        self.entry_url = ctk.CTkEntry(self.frame1, placeholder_text='Enter the article link', height=50)
        self.entry_url.grid(row=0, column=0,columnspan=2, padx=10, pady=10, sticky="ew")
        
        # Middle button
        self.check_button = ctk.CTkButton(self.frame1, text="Check", width=60, height=50, command=self.check_button_event)
        self.check_button.grid(row=0, column=2,columnspan=1, padx=10, pady=10, sticky="e")
        
        # Input Checkbox 
        self.input_textbox = ctk.CTkTextbox(self.frame1, height=125)
        self.input_textbox.grid(row=1, column=0,columnspan=3, padx=10, pady=10, sticky="nsew")
        
        # Frame for Result and Confidence labels
        self.label_frame = ctk.CTkFrame(self.frame1, fg_color="#333333")
        self.label_frame.grid(row=2, column=0, columnspan=3, padx=10, pady=10, sticky="ew")
        self.label_frame.grid_columnconfigure(0, weight=1)
        self.label_frame.grid_columnconfigure(1, weight=1)
        
        # Result label
        self.result_label = ctk.CTkLabel(self.label_frame, text="", height=50, fg_color="#333333", corner_radius=5)
        self.result_label.grid(row=0, column=0, padx=(0, 5), pady=0, sticky="ew")
        
        # Confidence label
        self.confidence_label = ctk.CTkLabel(self.label_frame, text="", height=50, fg_color="#333333", corner_radius=5)
        self.confidence_label.grid(row=0, column=1, padx=(5, 0), pady=0, sticky="ew")
        
        # Ensure equal width for both labels
        self.label_frame.grid_columnconfigure(0, weight=1, minsize=200)  
        self.label_frame.grid_columnconfigure(1, weight=1, minsize=200)  
        
        self.output_textbox = ctk.CTkTextbox(self.frame1, height=175, state="disabled")
        self.output_textbox.grid(row=3, column=0, columnspan=3, padx=10, pady=10, sticky="nsew")
        
        # Right scrollable view
        self.scrollview = ctk.CTkScrollableFrame(self)
        self.scrollview.grid(row=0, column=2, padx=10, pady=10, sticky="nsew")
        
        # Add header
        self.header = ctk.CTkLabel(self.scrollview, text="Leaderboard", font=("Arial", 24, "bold"))
        self.header.pack(pady=10, padx=10, anchor="w")
        
        # Container for provider entries
        self.provider_container = ctk.CTkFrame(self.scrollview)
        self.provider_container.pack(fill="both", expand=True)
        
    def set_controller(self, controller):
        self.controller = controller
        
    def check_button_event(self):
        if self.controller:
            self.controller.press_check_button()
