use super::*;

#[derive(Debug)]
pub struct Center {
    vertical: bool,
    horizontal: bool,
    child: Child,
}

impl Center {
    pub fn new(child: Child) -> UiRef {
        Self {
            vertical: true,
            horizontal: true,
            child,
        }
        .to_ref()
    }

    pub fn vertical(child: Child) -> UiRef {
        Self {
            vertical: true,
            horizontal: false,
            child,
        }
        .to_ref()
    }

    pub fn horizontal(child: Child) -> UiRef {
        Self {
            vertical: false,
            horizontal: true,
            child,
        }
        .to_ref()
    }
}

impl UiNode for Center {
    fn preferred_dimensions(&self) -> Vec2 {
        self.child.node.preferred_dimensions()
    }

    fn size(&self, area: Area) -> Vec2 {
        let mut size = Vec2::ZERO;

        if self.horizontal {
            size.x = area.size.x;
        }

        if self.vertical {
            size.y = area.size.y;
        }

        size
    }

    fn draw(&self, mut area: Area, ui: &UiState) -> Vec2 {
        let inner = self.child.node.size(area);
        let diff = (area.size - inner).max(Vec2::ZERO);

        if self.vertical {
            area.top_left.y += diff.y / 2.0;
        }

        if self.horizontal {
            area.top_left.x += diff.x / 2.0;
        }

        self.child.node.draw(area, ui)
    }
}

impl UiRef {
    pub fn center(self) -> UiRef {
        Center::new(self)
    }
}
