use crate::app::App;
use crate::app::tabs::Tab;
use crate::ui::theme::{self, pal};
use crate::ui::widgets::{mono, tab};
use crate::vm::Vm;
use crate::{editor, exec, paths, settings, workflow, workspace};
use rune::{Cx, El, col, row, text};
use rune_kit::Kit;

pub const SIDE: f32 = 260.0;

pub fn frame(s: &App, k: &Kit, _cx: &mut Cx<App>, layer: &mut Vec<El>) -> El {
    let active = s.tab().clone();
    let side = match &active {
        Tab::Welcome | Tab::Settings => None,
        Tab::Prompt(stem) => Some(workspace::sidebar::view(s, Some(stem))),
        Tab::Workflow(stem) | Tab::Task(stem) => Some(workflow::steps::view(s, stem, false)),
        Tab::Run(stem) => Some(workflow::steps::view(s, stem, true)),
    };
    let main = match &active {
        Tab::Welcome => workspace::welcome::view(s),
        Tab::Settings => settings::view::view(s, k),
        Tab::Prompt(stem) => editor::view::view(s, stem, k, layer),
        Tab::Workflow(stem) => workflow::review::view(s, stem, k),
        Tab::Task(stem) => workflow::task_view::view(s, stem, k),
        Tab::Run(stem) => exec::view::view(s, stem),
    };
    let tabs = s.tabs.iter().enumerate().map(|(i, t)| {
        let close = text("×")
            .font(theme::SMALL)
            .color(pal().faint)
            .role("button")
            .label("Close tab")
            .focusable(true)
            .cursor(rune::Cursor::Pointer)
            .on_press(move |s: &mut App| s.close(i));
        tab(&t.label(s), i == s.active)
            .key(format!("tab-{i}"))
            .focusable(true)
            .cursor(rune::Cursor::Pointer)
            .on_press(move |s: &mut App| s.active = i)
            .child(close)
    });
    shell(tabs.collect(), side, main, status(s))
}

pub fn shell(tabs: Vec<El>, side: Option<El>, main: El, status: El) -> El {
    let p = pal();
    let brand = row()
        .w(SIDE)
        .h_full()
        .px(12.0)
        .center_y()
        .child(text("▲ cast").font(theme::MONO).color(p.accent));
    let mut body = row().grow(1.0).min_h(0.0);
    if let Some(side) = side {
        body = body.child(side.w(SIDE).h_full().fill(p.surface).clip_rounded(0.0));
    }
    col()
        .w_full()
        .h_full()
        .child(
            row()
                .h(36.0)
                .w_full()
                .fill(p.surface)
                .child(brand)
                .children(tabs),
        )
        .child(
            body.child(
                col()
                    .grow(1.0)
                    .min_w(0.0)
                    .h_full()
                    .child(main.grow(1.0).min_h(0.0)),
            ),
        )
        .child(
            row()
                .h(28.0)
                .w_full()
                .px(12.0)
                .gap(16.0)
                .fill(p.surface)
                .center_y()
                .child(status),
        )
}

fn status(s: &App) -> El {
    let p = pal();
    let vm = match &s.vm {
        Vm::Off => mono("○ VM off", p.muted),
        Vm::Booting => mono("◐ VM booting", p.accent),
        Vm::Ready(_) => mono("● VM running", p.ok),
        Vm::Failed(e) => mono(format!("✕ VM {e}"), p.danger),
    };
    let mut left = row().gap(16.0).center_y().child(match &s.ws {
        Some(ws) => mono(paths::tilde(&ws.dir), p.fg),
        None => mono("No directory open", p.muted),
    });
    left = left.child(vm);
    if let Some(n) = &s.notice {
        left = left.child(
            mono(format!("✕ {n}"), p.danger)
                .role("button")
                .label("Dismiss")
                .focusable(true)
                .cursor(rune::Cursor::Pointer)
                .on_press(|s: &mut App| s.notice = None),
        );
    }
    left
}
