use common::CoopIcon;
use gpui::{
    App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, IntoElement,
    ParentElement, Render, SharedString, Styled, Window, div, svg,
};
use gpui_component::button::{Button, ButtonVariants};
use gpui_component::dock::{DockPlacement, Panel, PanelEvent};
use gpui_component::{ActiveTheme, Icon, Sizable, StyledExt, h_flex, v_flex};
use state::NostrRegistry;

use crate::panels::profile;
use crate::workspace::Workspace;

pub fn init(window: &mut Window, cx: &mut App) -> Entity<GreeterPanel> {
    cx.new(|cx| GreeterPanel::new(window, cx))
}

pub struct GreeterPanel {
    focus_handle: FocusHandle,
}

impl GreeterPanel {
    fn new(_window: &mut Window, cx: &mut App) -> Self {
        Self {
            focus_handle: cx.focus_handle(),
        }
    }

    fn add_profile_panel(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let nostr = NostrRegistry::global(cx);
        let signer = nostr.read(cx).signer();

        if let Some(public_key) = signer.public_key() {
            cx.spawn_in(window, async move |_this, cx| {
                cx.update(|window, cx| {
                    Workspace::add_panel(
                        profile::init(public_key, window, cx),
                        DockPlacement::Right,
                        window,
                        cx,
                    );
                })
                .ok();
            })
            .detach();
        }
    }
}

impl Panel for GreeterPanel {
    fn panel_name(&self) -> &'static str {
        "Greeter"
    }

    fn title(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        div().child(
            svg()
                .path("brand/coop.svg")
                .size_4()
                .text_color(cx.theme().muted_foreground),
        )
    }
}

impl EventEmitter<PanelEvent> for GreeterPanel {}

impl Focusable for GreeterPanel {
    fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
        self.focus_handle.clone()
    }
}

impl Render for GreeterPanel {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        const TITLE: &str = "Welcome to Coop!";
        const DESCRIPTION: &str = "Chat Freely, Stay Private on Nostr.";

        h_flex()
            .size_full()
            .items_center()
            .justify_center()
            .p_2()
            .child(
                v_flex()
                    .h_full()
                    .w_112()
                    .gap_6()
                    .items_center()
                    .justify_center()
                    .child(
                        h_flex()
                            .mb_4()
                            .gap_2()
                            .w_full()
                            .child(
                                svg()
                                    .path("brand/coop.svg")
                                    .size_12()
                                    .text_color(cx.theme().muted),
                            )
                            .child(
                                v_flex()
                                    .child(div().font_semibold().child(SharedString::from(TITLE)))
                                    .child(
                                        div()
                                            .text_xs()
                                            .text_color(cx.theme().muted_foreground)
                                            .child(SharedString::from(DESCRIPTION)),
                                    ),
                            ),
                    )
                    .child(
                        v_flex()
                            .gap_2()
                            .w_full()
                            .child(
                                h_flex()
                                    .gap_2()
                                    .w_full()
                                    .text_xs()
                                    .font_semibold()
                                    .text_color(cx.theme().muted_foreground)
                                    .child(SharedString::from("Get Started"))
                                    .child(div().flex_1().h_px().bg(cx.theme().border)),
                            )
                            .child(
                                v_flex()
                                    .gap_2()
                                    .w_full()
                                    .child(
                                        Button::new("profile")
                                            .icon(Icon::new(CoopIcon::Profile))
                                            .label("Update profile")
                                            .ghost()
                                            .small()
                                            .justify_start()
                                            .on_click(cx.listener(move |this, _ev, window, cx| {
                                                this.add_profile_panel(window, cx)
                                            })),
                                    )
                                    .child(
                                        Button::new("invite")
                                            .icon(Icon::new(CoopIcon::Invite))
                                            .label("Invite friends")
                                            .ghost()
                                            .small()
                                            .justify_start(),
                                    ),
                            ),
                    ),
            )
    }
}
