from urllib.parse import urlencode
import webbrowser
import ttkbootstrap as ttk
from ..controller import Controller
from ..geometry import geometry
from .. import config as conf
from ..license_info import license_info

class AboutTab(ttk.Frame):
    def __init__(self, app):
        self.app = app
        self.c: Controller = self.app.c
        super().__init__(
            self.app.notebook,
            padding=(geometry.whp(4), geometry.whp(4))
        )
        self.create_widgets()

    def create_widgets(self):

        lst = ["Jimlet Text-To-Speech Converter"]

        if license_info.is_evaluation():
            lst.append("Evaluation Edition")
        else:
            lst.append("Regular License")
            lst.append(f"License Key: {license_info.key}")


        lst.extend([
            f"Version: {conf.APP_VERSION}",
            f"Relelase date: {conf.APP_RELEASE_DATE}",
            "Engine profile: Hibiscus",
            "Website: https://jimlet.com\n",
            "Jimlet is a local text-to-speech conversion tool.",
            "It converts text files into audio using an offline speech engine.\n",
        ]
        )

        if license_info.is_evaluation():
            lst.append("Please read the EVALUATION-LICENSE.txt file included with")
            lst.append("this application.")
        else:
            lst.append("Please read the LICENSE.txt file included with this application.")


        about_text = "\n".join(lst)

        lbl = ttk.Label(
            self,
            text=about_text,
            justify="left",
        )
        lbl.grid(row=0, column=0, sticky="nsew")

        def open_website():

            base_url = "https://jimlet.com/"
            params = {
                "utm_source": "jimlet",
                "utm_medium": "app",
                "utm_content": "about_tab_button",
            }

            url = f"{base_url}?{urlencode(params)}"
            webbrowser.open(url)

        btn = ttk.Button(
            self,
            text="Visit website",
            command=open_website,
            padding=(geometry.whp(2), geometry.whp(1))
        )
        btn.grid(row=1, column=0, sticky="w", pady=(geometry.whp(4),0))

        self.grid_columnconfigure(0, weight=1)
