use crate::app::App;
use crate::ui::theme::{self, pal};
use rune::{Color, El, TextEdit, col, el, row, text};

#[derive(Clone, Copy, PartialEq)]
pub enum Kind {
    Normal,
    Primary,
    Danger,
    Ghost,
}

pub fn lbl(s: &str) -> El {
    text(s).font(theme::SMALL).color(pal().faint)
}

pub fn mono(s: impl Into<String>, color: Color) -> El {
    text(s).font(theme::SMALL).color(color)
}

fn pressable(e: El, label: &str) -> El {
    e.role("button")
        .label(label)
        .focusable(true)
        .cursor(rune::Cursor::Pointer)
        .hover(pal().hover)
}

fn base(label: &str, kind: Kind) -> El {
    let p = pal();
    let (fill, ink) = match kind {
        Kind::Primary => (p.accent, p.accent_fg),
        Kind::Normal => (p.raised, p.fg),
        Kind::Danger => (p.raised, p.danger),
        Kind::Ghost => (Color::TRANSPARENT, p.muted),
    };
    row()
        .h(theme::CONTROL)
        .px(8.0)
        .radius(4.0)
        .fill(fill)
        .center_y()
        .child(mono(label, ink).truncate_middle().min_w(0.0))
}

pub fn btn(label: &str, kind: Kind, on: Option<impl Fn(&mut App) + 'static>) -> El {
    match on {
        Some(f) => pressable(base(label, kind), label).on_press(f),
        None => base(label, kind).opacity(0.45),
    }
}

pub fn btn_tx(label: &str, kind: Kind, on: impl Fn(&mut App, &mut rune::Tx) + 'static) -> El {
    pressable(base(label, kind), label).on_press_tx(on)
}

pub fn link(label: &str, on: impl Fn(&mut App) + 'static) -> El {
    pressable(mono(label, pal().muted), label)
        .radius(4.0)
        .on_press(on)
}

pub fn tab(label: &str, on: bool) -> El {
    let p = pal();
    let r = row()
        .h_full()
        .px(12.0)
        .gap(8.0)
        .center_y()
        .child(mono(label, if on { p.strong } else { p.muted }));
    if on {
        r.fill(p.raised)
            .border_sides([2.0, 0.0, 0.0, 0.0], p.accent)
    } else {
        r.hover(p.hover)
    }
}

pub fn ph(left: El, right: El) -> El {
    row()
        .h(32.0)
        .px(12.0)
        .fill(pal().surface)
        .center_y()
        .justify_between()
        .child(left)
        .child(right)
}

pub fn section(title: &str, body: El) -> El {
    col()
        .gap(4.0)
        .child(lbl(title).px(12.0))
        .child(body.fill(pal().surface).radius(4.0).clip_rounded(4.0))
}

pub fn hd(left: &str, right: &str) -> El {
    row()
        .px(12.0)
        .pt(12.0)
        .pb(4.0)
        .gap(8.0)
        .justify_between()
        .child(lbl(left).truncate().min_w(0.0))
        .child(lbl(right))
}

pub fn item(on: bool, content: El) -> El {
    let p = pal();
    let r = row()
        .px(12.0)
        .py(2.0)
        .gap(6.0)
        .center_y()
        .min_w(0.0)
        .focusable(true)
        .child(content.grow(1.0).min_w(0.0));
    if on {
        r.fill(p.soft).border_sides([0.0, 0.0, 0.0, 2.0], p.accent)
    } else {
        r.hover(p.hover)
    }
}

pub fn seg<T: Copy + PartialEq + 'static>(
    opts: &[(T, &str)],
    cur: T,
    on: impl Fn(&mut App, T) + Clone + 'static,
) -> El {
    let p = pal();
    row()
        .h(theme::CONTROL)
        .radius(4.0)
        .border(1.0, p.line)
        .clip_rounded(4.0)
        .children(opts.iter().map(|&(v, l)| {
            let sel = v == cur;
            let on = on.clone();
            row()
                .h_full()
                .px(8.0)
                .center_y()
                .fill(if sel { p.accent } else { Color::TRANSPARENT })
                .role("radio")
                .label(l)
                .checked(sel)
                .focusable(true)
                .cursor(rune::Cursor::Pointer)
                .on_press(move |s: &mut App| on(s, v))
                .child(mono(l, if sel { p.accent_fg } else { p.fg }))
        }))
}

pub fn stat(value: &str, label: &str, color: Color) -> El {
    col()
        .pl(16.0)
        .pr(24.0)
        .h_full()
        .justify_center()
        .child(text(value).font(theme::TITLE).color(color))
        .child(mono(label, pal().muted))
}

pub fn bar(pct: f32, color: Color) -> El {
    el().h(3.0)
        .w_full()
        .fill(pal().raised)
        .child(el().h(3.0).w_frac(pct.clamp(0.0, 1.0)).fill(color))
}

pub fn input(
    edit: &TextEdit,
    placeholder: &str,
    access: impl for<'a> Fn(&'a mut App) -> &'a mut TextEdit + 'static,
) -> rune_kit::TextInput {
    rune_kit::TextInput::new(edit)
        .placeholder(placeholder)
        .text_style(theme::BODY)
        .height(theme::CONTROL)
        .on_edit(access)
}
