use std::io;

use libterm::{Color, Term};

#[derive(Clone, Copy)]
pub struct Style {
    pub foreground: Color,
    pub background: Color,
    pub bold: bool,
}

impl Default for Style {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl Style {
    pub const DEFAULT: Self = Self {
        foreground: Color::White,
        background: Color::Black,
        bold: false,
    };

    pub fn apply(&self, term: &mut Term) -> Result<(), io::Error> {
        self.apply_delta(&Self::DEFAULT, term)
    }

    pub fn apply_delta(&self, original: &Self, term: &mut Term) -> Result<(), io::Error> {
        // TODO libterm Color PartialEq
        if self.foreground as u32 != original.foreground as u32 {
            // Apply foreground
            term.set_foreground(self.foreground)?;
        }
        if self.background as u32 != original.background as u32 {
            // Apply background
            term.set_background(self.background)?;
        }
        if self.bold != original.bold {
            // Apply bold
            term.set_bright(self.bold)?;
        }
        Ok(())
    }
}
