use super::{Element, Paragraph};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Serialize)]
pub struct Table {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub caption: Option<Paragraph>,
    pub width: i32,
    pub rows: Vec<TableRow>,
}

#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq, Serialize)]
pub enum TableRowType {
    Header,
    Body,
    Footer,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Serialize)]
pub struct TableRow {
    pub row_type: TableRowType,
    pub cells: Vec<TableCell>,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Serialize)]
pub struct TableCell {
    #[serde(default = "default_size")]
    #[serde(skip_serializing_if = "is_default_size")]
    pub width: i32,
    #[serde(default = "default_size")]
    #[serde(skip_serializing_if = "is_default_size")]
    pub height: i32,
    pub element: Element,
    #[serde(default)]
    #[serde(skip_serializing_if = "is_default_header")]
    pub is_header: bool,
}

fn default_size() -> i32 {
    1
}

fn is_default_size(size: &i32) -> bool {
    *size == 1
}

fn is_default_header(header: &bool) -> bool {
    !*header
}
