import tkinter as tk
from tkinter import ttk, scrolledtext
import datetime

class ChatPanel(ttk.Frame):
    def __init__(self, parent, send_callback):
        super().__init__(parent)
        
        self.send_callback = send_callback
        
        self.chat_history = scrolledtext.ScrolledText(self, wrap=tk.WORD, state=tk.DISABLED)
        self.chat_history.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        
        self.chat_history.tag_configure("user", foreground="blue")
        self.chat_history.tag_configure("assistant", foreground="green")
        self.chat_history.tag_configure("system", foreground="gray")
        self.chat_history.tag_configure("time", foreground="gray", font=("Arial", 8))
        
        control_frame = ttk.Frame(self)
        control_frame.pack(fill=tk.X, padx=5, pady=(0, 5))
        
        model_frame = ttk.Frame(control_frame)
        model_frame.pack(side=tk.LEFT, padx=(0, 5), fill=tk.Y)
        
        ttk.Label(model_frame, text="Choose model:").pack(side=tk.LEFT, padx=(0, 5))
        
        self.model_choice = tk.StringVar(value="claude-3-7-sonnet-20250219")
        model_options = ["claude-3-7-sonnet-20250219", "Pro/deepseek-ai/DeepSeek-R1", "gpt-4o"]
        
        model_menu = ttk.Combobox(model_frame, textvariable=self.model_choice, values=model_options, width=25)
        model_menu.pack(side=tk.LEFT)
        
        input_frame = ttk.Frame(self)
        input_frame.pack(fill=tk.X, padx=5, pady=5)
        
        self.message_input = scrolledtext.ScrolledText(input_frame, wrap=tk.WORD, height=4)
        self.message_input.pack(fill=tk.X, expand=True, side=tk.LEFT, padx=(0, 5))
        self.message_input.bind("<Return>", self.on_enter)
        self.message_input.bind("<Shift-Return>", self.on_shift_enter)
        
        send_button = ttk.Button(input_frame, text="send", command=self.send_message, style="Rounded.TButton")
        send_button.pack(side=tk.RIGHT)
        
        self.add_system_message("Welcome to use LLM-assisted interactive drawing application!\nPlease draw on the canvas, or enter text here to interact with LLM.")
    
    def add_message(self, role, message):
        self.chat_history.config(state=tk.NORMAL)
        
        timestamp = datetime.datetime.now().strftime("%H:%M:%S")
        self.chat_history.insert(tk.END, f"[{timestamp}] ", "time")
        
        if role == "user":
            self.chat_history.insert(tk.END, "You: ", "user")
        elif role == "assistant":
            self.chat_history.insert(tk.END, "AI: ", "assistant")
        elif role == "system":
            self.chat_history.insert(tk.END, "System: ", "system")
        
        self.chat_history.insert(tk.END, f"{message}\n\n")
        
        self.chat_history.see(tk.END)
        self.chat_history.config(state=tk.DISABLED)
    
    def add_system_message(self, message):
        self.add_message("system", message)
    
    def send_message(self):
        message = self.message_input.get("1.0", tk.END).strip()
        if message:
            self.message_input.delete("1.0", tk.END)
            
            self.send_callback(message)
    
    def on_enter(self, event):
        self.send_message()
        return "break"
    
    def on_shift_enter(self, event):
        return None 