import sys

sys.coinit_flags = 0

import flet
from flet import (
    AppBar,
    FLET_APP_HIDDEN,
    ElevatedButton,
    Page,
    Text,
    View,
    colors,
    TextAlign,
    Theme,
    TextTheme,
    TextStyle,
    FontWeight,
)
import win32com
import sys, os, inspect

sys.path.append(
    os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe()) + "\\..\\..\\..")
    )
)

from IFikatia import IFikatia

from sayfalar.ana_sayfa import FikoDilimleAnaSayfa
import pythoncom


def main(page: Page):
    page.window_width = 350  # window's width is 200 px
    page.window_height = 510  # window's height is 200 px
    page.window_resizable = True
    page.window_minimizable = False
    page.window_maximizable = False
    page.window_always_on_top = True
    # page.window_skip_task_bar = True
    page.window_center()
    page.title = "FikoYT MDF Kütükçüsü v0.0.1"
    page.fonts = {
        "Kanit": "https://raw.githubusercontent.com/google/fonts/master/ofl/kanit/Kanit-Bold.ttf",
        "Aleo Bold Italic": "https://raw.githubusercontent.com/google/fonts/master/ofl/aleo/Aleo-BoldItalic.ttf",
    }
    page.theme = Theme(
        use_material3=True,
        visual_density="adaptivePlatformDensity",
        font_family="Roboto Bold Italic",
        text_theme=TextTheme(
            body_medium=TextStyle(
                size=10,
                color=colors.BLACK54,
                weight=FontWeight.W_500,
                font_family="Roboto Bold Italic",
            ),
            body_small=TextStyle(
                size=9,
                color=colors.BLACK54,
                weight=FontWeight.W_500,
                font_family="Roboto Bold Italic",
            ),
        ),
        # primary_text_theme=TextTheme(
        #     body_medium=TextStyle(
        #         size=9,
        #         color=colors.BLACK54,
        #         weight=FontWeight.W_500,
        #         font_family="Roboto Bold Italic",
        #     )
        # ),
    )

    pythoncom.CoInitialize()
    fikatia = IFikatia(win32com.client.Dispatch("Catia.Application"))
    fikatia.marshalHazirla()
    fikatia.baslik = page.title

    print("Initial route:", page.route)

    def route_change(e):
        print("Route change:", e.route)
        page.views.clear()
        page.views.append(FikoDilimleAnaSayfa(mainPage=page))
        if page.route == "/settings" or page.route == "/settings/mail":
            page.views.append(
                View(
                    "/settings",
                    [
                        AppBar(title=Text("Settings"), bgcolor=colors.SURFACE_VARIANT),
                        Text(
                            "Settings!", style="bodyMedium", text_align=TextAlign.CENTER
                        ),
                        ElevatedButton(
                            "Go to mail settings", on_click=open_mail_settings
                        ),
                    ],
                )
            )
        if page.route == "/settings/mail":
            page.views.append(
                View(
                    "/settings/mail",
                    [
                        AppBar(
                            title=Text("Mail Settings"), bgcolor=colors.SURFACE_VARIANT
                        ),
                        Text("Mail settings!"),
                    ],
                )
            )
        page.update()

    def view_pop(e):
        print("View pop:", e.view)
        page.views.pop()
        top_view = page.views[-1]
        page.go(top_view.route)

    page.on_route_change = route_change
    page.on_view_pop = view_pop

    def open_mail_settings(e):
        page.go("/settings/mail")

    def open_settings(e):
        page.go("/settings")

    page.go(page.route)


flet.app(target=main, view="flet_app")
