use crate::model::{Alignment, PageBreakMode};

use super::{LayoutPage, PageCustomProperties};

pub(super) fn apply_page_break_before(page_break: Option<PageBreakMode>, pages: &mut Vec<LayoutPage>, y: &mut f32, page_margins_top: f32) {
    match page_break {
        Some(PageBreakMode::Before) => {
            if !pages.last().unwrap().items.is_empty() {
                pages.push(LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() });
                *y = page_margins_top;
            }
        }
        Some(PageBreakMode::BeforeOdd) => {
            if pages.len() % 2 == 0 {
                pages.push(LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() });
                *y = page_margins_top;
            }
        }
        Some(PageBreakMode::BeforeEven) => {
            if pages.len() % 2 == 1 {
                pages.push(LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() });
                *y = page_margins_top;
            }
        }
        _ => {}
    }
}

pub(super) fn apply_page_break_after(page_break: Option<PageBreakMode>, pages: &mut Vec<LayoutPage>, y: &mut f32, page_margins_top: f32) {
    match page_break {
        Some(PageBreakMode::After) => {
            pages.push(LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() });
            *y = page_margins_top;
        }
        Some(PageBreakMode::AfterOdd) => {
            pages.push(LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() });
            *y = page_margins_top;
            if pages.len() % 2 == 0 {
                pages.push(LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() });
                *y = page_margins_top;
            }
        }
        Some(PageBreakMode::AfterEven) => {
            pages.push(LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() });
            *y = page_margins_top;
            if pages.len() % 2 == 1 {
                pages.push(LayoutPage { items: Vec::new(), background: None, watermark: None, header: None, footer: None, custom_properties: PageCustomProperties::default() });
                *y = page_margins_top;
            }
        }
        _ => {}
    }
}

pub(super) fn align_block_x(left: f32, available_width: f32, width: f32, alignment: Alignment) -> f32 {
    match alignment {
        Alignment::Left | Alignment::Justify => left,
        Alignment::Center => left + (available_width - width) / 2.0,
        Alignment::Right => left + available_width - width,
    }
}
