import logging
from tkinter import TclError, Text
import sounddevice as sd
import ttkbootstrap as ttk
from ..controller import Controller
from ..geometry import geometry
from ..defaults import TEXTBOX_TEXT

logger = logging.getLogger(__name__)

class TextboxTab(ttk.Frame):

    def __init__(self, app):
        self.app = app
        self.c: Controller = app.c

        super().__init__(
            self.app.notebook,
            padding=(geometry.whp(4), geometry.whp(4))
        )

        self.create_widgets()

    def create_widgets(self):

        self.content = ttk.Frame(self)
        self.content.grid(row=0, column=0, sticky="nsew")

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.content.grid_rowconfigure(0, weight=1)
        self.content.grid_columnconfigure(0, weight=1)


        self.text = Text(
            self.content,
            wrap="word",
            bd=0
        )

        self.text.grid(row=0, column=0, sticky="nsew")

        self.text.insert("1.0", TEXTBOX_TEXT.strip())

        self.text.bind("<Control-a>", self.select_all)
        self.text.bind("<Control-A>", self.select_all)  
        self.text.bind("<<Paste>>", self.paste)

        self.text.tag_configure(
            "body",
            lmargin1=10,
            lmargin2=10,
            spacing1=10,   
            spacing2=10,   
            spacing3=10    
        )

        self.text.tag_add("body", "1.0", "end")

        self.text.tag_add("body", "insert")




        bottom_row = ttk.Frame(self.content, style="BottomContainer.TFrame")
        bottom_row.grid(
            row=1,
            column=0,
            sticky="ew",
            pady=(geometry.whp(4), 0)
        )

        bottom_row.grid_columnconfigure(3, weight=1)

        BTN_WIDTH = 10

        btn_play = ttk.Button(
            bottom_row,
            text="Play",
            style="Action.TButton",
            width=BTN_WIDTH,
            padding=(0, geometry.whp(1)),
            command=self.on_play
        )
        btn_play.grid(row=0, column=0, padx=(0, geometry.whp(3)), sticky="w")

        btn_convert = ttk.Button(
            bottom_row,
            text="Convert",
            width=BTN_WIDTH,
            padding=(0, geometry.whp(1)),
            command=self.on_convert
        )
        btn_convert.grid(row=0, column=1, padx=(0, geometry.whp(3)), sticky="w")

        btn_clear = ttk.Button(
            bottom_row,
            text="Clear",
            width=BTN_WIDTH,
            padding=(0, geometry.whp(1)),
            bootstyle="outline",
            command=self.on_clear
        )
        btn_clear.grid(row=0, column=2, sticky="w")


    def on_play(self):

        text = self.text.get("1.0", "end-1c").strip()
        if not text:
            return
        self.c.play_text(text)

    def on_convert(self):
        text = self.text.get("1.0", "end-1c").strip()
        if not text:
            return
        self.c.convert_and_save(text)

    def on_clear(self):
        sd.stop()
        self.text.delete("1.0", "end")

    def select_all(self, _):
        self.text.tag_add("sel", "1.0", "end-1c")
        self.text.mark_set("insert", "1.0")
        return "break"   

    def paste(self, _):
        try:
            clipboard_text = self.text.clipboard_get()
        except TclError:
            return "break"

        if self.text.tag_ranges("sel"):
            self.text.delete("sel.first", "sel.last")

        self.text.insert("insert", clipboard_text, "body")
        return "break"
