use super::{TextMesh, TextProperties};
use nalgebra_glm::Vec2;

#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum HudAnchor {
    TopLeft,
    TopCenter,
    TopRight,
    CenterLeft,
    #[default]
    Center,
    CenterRight,
    BottomLeft,
    BottomCenter,
    BottomRight,
}

#[derive(Clone, Debug)]
pub struct HudText {
    pub text_index: usize,
    pub properties: TextProperties,
    pub font_index: usize,
    pub position: Vec2,
    pub anchor: HudAnchor,
    pub dirty: bool,
    pub cached_mesh: Option<TextMesh>,
}

impl Default for HudText {
    fn default() -> Self {
        Self {
            text_index: 0,
            properties: TextProperties::default(),
            font_index: 0,
            position: Vec2::zeros(),
            anchor: HudAnchor::default(),
            dirty: true,
            cached_mesh: None,
        }
    }
}

impl HudText {
    pub fn new(text_index: usize) -> Self {
        Self {
            text_index,
            properties: TextProperties::default(),
            font_index: 0,
            position: Vec2::zeros(),
            anchor: HudAnchor::default(),
            dirty: true,
            cached_mesh: None,
        }
    }

    pub fn with_properties(mut self, properties: TextProperties) -> Self {
        self.properties = properties;
        self.dirty = true;
        self
    }

    pub fn with_font(mut self, font_index: usize) -> Self {
        self.font_index = font_index;
        self.dirty = true;
        self
    }

    pub fn with_position(mut self, position: Vec2) -> Self {
        self.position = position;
        self
    }

    pub fn with_anchor(mut self, anchor: HudAnchor) -> Self {
        self.anchor = anchor;
        self
    }

    pub fn set_text_index(&mut self, text_index: usize) {
        self.text_index = text_index;
        self.dirty = true;
    }

    pub fn set_position(&mut self, position: Vec2) {
        self.position = position;
    }

    pub fn set_anchor(&mut self, anchor: HudAnchor) {
        self.anchor = anchor;
    }

    pub fn set_font_index(&mut self, font_index: usize) {
        self.font_index = font_index;
        self.dirty = true;
    }

    pub fn set_properties(&mut self, properties: TextProperties) {
        self.properties = properties;
        self.dirty = true;
    }

    pub fn get_bounds(&self) -> Option<Vec2> {
        self.cached_mesh.as_ref().map(|mesh| mesh.bounds)
    }
}
