use crate::ProfilePic;
use egui::{Frame, Label, RichText};
use egui_extras::Size;
use nostrdb::ProfileRecord;
use notedeck::tokens::{PFP_LG, PFP_XL, SPACING_MD};

use notedeck::{
    name::get_display_name, profile::get_profile_url, tr, Images, Localization, MediaJobSender,
    NotedeckTextStyle,
};

use super::{about_section_widget, banner, display_name_widget};

pub struct ProfilePreview<'a, 'cache> {
    profile: &'a ProfileRecord<'a>,
    cache: &'cache mut Images,
    jobs: &'cache MediaJobSender,
    banner_height: Size,
}

impl<'a, 'cache> ProfilePreview<'a, 'cache> {
    pub fn new(
        profile: &'a ProfileRecord<'a>,
        cache: &'cache mut Images,
        jobs: &'cache MediaJobSender,
    ) -> Self {
        let banner_height = Size::exact(PFP_XL);
        ProfilePreview {
            profile,
            jobs,
            cache,
            banner_height,
        }
    }

    pub fn banner_height(&mut self, size: Size) {
        self.banner_height = size;
    }

    fn body(self, ui: &mut egui::Ui) {
        let padding = SPACING_MD;
        crate::padding(padding, ui, |ui| {
            let mut pfp_rect = ui.available_rect_before_wrap();
            let size = PFP_XL;
            pfp_rect.set_width(size);
            pfp_rect.set_height(size);
            let pfp_rect = pfp_rect.translate(egui::vec2(0.0, -(padding + 2.0 + (size / 2.0))));

            ui.put(
                pfp_rect,
                &mut ProfilePic::new(self.cache, self.jobs, get_profile_url(Some(self.profile)))
                    .size(size)
                    .border(ProfilePic::border_stroke(ui)),
            );
            ui.add(display_name_widget(
                &get_display_name(Some(self.profile)),
                false,
            ));
            ui.add(about_section_widget(Some(self.profile)));
        });
    }
}

impl egui::Widget for ProfilePreview<'_, '_> {
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        ui.vertical(|ui| {
            banner(
                ui,
                self.cache,
                self.jobs,
                self.profile.record().profile().and_then(|p| p.banner()),
                PFP_XL,
            );

            self.body(ui);
        })
        .response
    }
}

pub struct SimpleProfilePreview<'a, 'cache> {
    profile: Option<&'a ProfileRecord<'a>>,
    pub i18n: &'cache mut Localization,
    cache: &'cache mut Images,
    jobs: &'cache MediaJobSender,
    is_nsec: bool,
}

impl<'a, 'cache> SimpleProfilePreview<'a, 'cache> {
    pub fn new(
        profile: Option<&'a ProfileRecord<'a>>,
        cache: &'cache mut Images,
        jobs: &'cache MediaJobSender,
        i18n: &'cache mut Localization,
        is_nsec: bool,
    ) -> Self {
        SimpleProfilePreview {
            profile,
            cache,
            is_nsec,
            i18n,
            jobs,
        }
    }
}

impl egui::Widget for SimpleProfilePreview<'_, '_> {
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        Frame::new()
            .show(ui, |ui| {
                ui.add(
                    &mut ProfilePic::new(self.cache, self.jobs, get_profile_url(self.profile))
                        .size(PFP_LG),
                );
                ui.vertical(|ui| {
                    ui.add(display_name_widget(&get_display_name(self.profile), true));
                    if !self.is_nsec {
                        ui.add(
                            Label::new(
                                RichText::new(tr!(
                                    self.i18n,
                                    "Read only",
                                    "Label for read-only profile mode"
                                ))
                                .size(notedeck::fonts::get_font_size(
                                    ui.ctx(),
                                    &NotedeckTextStyle::Tiny,
                                ))
                                .color(ui.visuals().warn_fg_color),
                            )
                            .selectable(false),
                        );
                    }
                });
            })
            .response
    }
}
