/// 终端字符格大小
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Size {
    pub width: u16,
    pub height: u16,
}

/// 字符格坐标
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Pos {
    pub x: u16,
    pub y: u16,
}

/// 字符格矩形区域
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Rect {
    pub x: u16,
    pub y: u16,
    pub width: u16,
    pub height: u16,
}

/// 四周边距
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Insets {
    pub top: u16,
    pub right: u16,
    pub bottom: u16,
    pub left: u16,
}

impl Insets {
    pub const ZERO: Insets = Insets {
        top: 0,
        right: 0,
        bottom: 0,
        left: 0,
    };

    pub fn all(value: u16) -> Self {
        Self {
            top: value,
            right: value,
            bottom: value,
            left: value,
        }
    }
}

impl Rect {
    /// 判断坐标是否在矩形内
    pub fn contains(&self, pos: Pos) -> bool {
        pos.x >= self.x
            && pos.x < self.x.saturating_add(self.width)
            && pos.y >= self.y
            && pos.y < self.y.saturating_add(self.height)
    }

    /// 扣除 insets 后的内部区域
    pub fn inner(&self, insets: Insets) -> Rect {
        let x = self.x.saturating_add(insets.left);
        let y = self.y.saturating_add(insets.top);
        let width = self
            .width
            .saturating_sub(insets.left.saturating_add(insets.right));
        let height = self
            .height
            .saturating_sub(insets.top.saturating_add(insets.bottom));

        Rect { x, y, width, height }
    }
}
