use std::ops::Range;
use unicode_width::UnicodeWidthStr;
use crate::{Canvas, Generator, Glyph, Ink, Meta, Role, Size, ink};
pub const SEPARATOR: &str = " · ";
const MIN_HEIGHT: u16 = 6;
const PHENOMENON_LEVEL: f64 = 170.0;
const MECHANISM_LEVEL: f64 = 110.0;
pub const READOUT_LEVEL: u8 = 90;
const DIM_FROM_S: f64 = 8.0;
const DIM_OVER_S: f64 = 2.0;
const READOUT_GAP: usize = 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Form {
Phenomenon,
Full,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Title {
pub phenomenon: &'static str,
pub mechanism: &'static str,
}
impl Title {
pub fn new(title: &'static str, caption: Option<&'static str>) -> Self {
let (phenomenon, mechanism) = title.split_once(SEPARATOR).unwrap_or((title, ""));
Self {
phenomenon,
mechanism: caption.unwrap_or(mechanism),
}
}
pub fn of(g: &(impl Generator + ?Sized)) -> Self {
Self::new(g.meta().title, g.caption())
}
fn parts(&self, form: Form) -> [&'static str; 3] {
match form {
Form::Full if !self.mechanism.is_empty() => {
[self.phenomenon, SEPARATOR, self.mechanism]
}
_ => [self.phenomenon, "", ""],
}
}
pub fn width(&self, form: Form) -> usize {
self.parts(form).iter().map(|s| s.width()).sum()
}
pub fn chars(&self, form: Form) -> impl Iterator<Item = char> + use<> {
let [a, b, c] = self.parts(form);
a.chars().chain(b.chars()).chain(c.chars())
}
pub fn fit(&self, size: Size) -> Option<Form> {
if size.height() < MIN_HEIGHT {
return None;
}
let limit = usize::from(size.width()) * 3 / 5;
[Form::Full, Form::Phenomenon]
.into_iter()
.find(|&form| self.width(form) + 2 <= limit)
}
pub fn full_fits_from(&self) -> usize {
(5 * (self.width(Form::Full) + 2)).div_ceil(3)
}
}
pub const fn ascii_of(c: char) -> Option<char> {
match c {
' '..='~' => Some(c),
'·' | '–' => Some('-'),
'á' => Some('a'),
'é' => Some('e'),
'ö' => Some('o'),
_ => None,
}
}
pub fn span(size: Size, title: &Title) -> Option<(i32, Range<i32>)> {
let form = title.fit(size)?;
let end = i32::try_from(title.width(form) + 2).unwrap_or(i32::MAX);
Some((i32::from(size.height()) - 1, 0..end))
}
pub fn readout_room(size: Size, title: &Title) -> usize {
let w = usize::from(size.width());
let before = span(size, title).map_or(1, |(_, cols)| cols.len() - 1 + READOUT_GAP);
w.saturating_sub(before + 1).min(w / 2)
}
pub fn stable_readout_room(size: Size, meta: &Meta) -> usize {
let titles = || {
std::iter::once(None)
.chain(meta.captions.iter().copied().map(Some))
.map(|c| Title::new(meta.title, c))
};
let settled = titles().map(|t| t.full_fits_from()).max().unwrap_or(0);
let settled = u16::try_from(settled).unwrap_or(u16::MAX).max(size.width());
(size.width()..=settled)
.map(|w| {
let at = Size::new(w, size.height());
titles().map(|t| readout_room(at, &t)).min().unwrap_or(0)
})
.min()
.unwrap_or(0)
}
pub fn draw(canvas: &mut Canvas, title: &Title, age: f64) {
let Some(form) = title.fit(canvas.size()) else {
return;
};
let age = if age.is_finite() { age } else { 0.0 };
let fade = ((age - DIM_FROM_S) / DIM_OVER_S).clamp(0.0, 1.0);
let dim = f64::from(READOUT_LEVEL);
let ink_at = |base: f64| Ink::new(Role::Text, ink::level(base + (dim - base) * fade));
let (phenomenon, rest) = (ink_at(PHENOMENON_LEVEL), ink_at(MECHANISM_LEVEL));
let y = i32::from(canvas.size().height()) - 1;
let gap = Glyph::ascii(' ');
canvas.put(0, y, gap, Ink::BLANK);
let mut x = 1;
for (part, ink) in title.parts(form).into_iter().zip([phenomenon, rest, rest]) {
for c in part.chars() {
canvas.put(x, y, Glyph::new(c, ascii_of(c).unwrap_or('?')), ink);
x += 1;
}
}
canvas.put(x, y, gap, Ink::BLANK);
}