use super::xml::esc;
use crate::model::Meta;
pub(crate) const PAGE_WIDTH_TWIPS: u32 = 12240;
pub(crate) const PAGE_HEIGHT_TWIPS: u32 = 15840;
pub(crate) const MARGIN_TWIPS: u32 = 1440;
pub(crate) const TEXT_WIDTH_TWIPS: u32 = PAGE_WIDTH_TWIPS - 2 * MARGIN_TWIPS;
pub(crate) const TWIPS_PER_PT: f32 = 20.0;
pub(crate) const DOCUMENT_NAMESPACES: &str = "xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" \
xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" \
xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" \
xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" \
xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" \
xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\"";
pub(crate) const XML_DECL: &str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
const REL_BASE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
pub(crate) const CONTENT_TYPES: &str = "<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\
<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>\
<Default Extension=\"xml\" ContentType=\"application/xml\"/>\
<Default Extension=\"png\" ContentType=\"image/png\"/>\
<Default Extension=\"jpeg\" ContentType=\"image/jpeg\"/>\
<Override PartName=\"/word/document.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml\"/>\
<Override PartName=\"/word/styles.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml\"/>\
<Override PartName=\"/word/settings.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml\"/>\
<Override PartName=\"/docProps/core.xml\" ContentType=\"application/vnd.openxmlformats-package.core-properties+xml\"/>\
<Override PartName=\"/docProps/app.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.extended-properties+xml\"/>\
</Types>";
pub(crate) const PACKAGE_RELS: &str = "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"word/document.xml\"/>\
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\" Target=\"docProps/core.xml\"/>\
<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties\" Target=\"docProps/app.xml\"/>\
</Relationships>";
pub(crate) const APP_PROPS: &str = "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\">\
<Application>kakemono</Application></Properties>";
pub(crate) const SETTINGS: &str = "<w:settings xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" \
xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\">\
<w:compat><w:compatSetting w:name=\"compatibilityMode\" w:uri=\"http://schemas.microsoft.com/office/word\" w:val=\"15\"/></w:compat>\
<m:mathPr><m:mathFont m:val=\"Cambria Math\"/><m:dispDef/></m:mathPr>\
</w:settings>";
pub(crate) const STYLES: &str = concat!(
"<w:styles xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">",
"<w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii=\"Cambria\" w:hAnsi=\"Cambria\" w:eastAsia=\"Cambria\" w:cs=\"Times New Roman\"/>",
"<w:sz w:val=\"22\"/><w:szCs w:val=\"22\"/><w:lang w:val=\"en-US\"/></w:rPr></w:rPrDefault>",
"<w:pPrDefault><w:pPr><w:spacing w:after=\"120\" w:line=\"264\" w:lineRule=\"auto\"/></w:pPr></w:pPrDefault></w:docDefaults>",
"<w:style w:type=\"paragraph\" w:default=\"1\" w:styleId=\"Normal\"><w:name w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:jc w:val=\"both\"/></w:pPr></w:style>",
"<w:style w:type=\"character\" w:default=\"1\" w:styleId=\"DefaultParagraphFont\"><w:name w:val=\"Default Paragraph Font\"/>",
"<w:uiPriority w:val=\"1\"/><w:semiHidden/></w:style>",
"<w:style w:type=\"paragraph\" w:styleId=\"Title\"><w:name w:val=\"Title\"/><w:basedOn w:val=\"Normal\"/><w:next w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:spacing w:after=\"240\" w:line=\"240\" w:lineRule=\"auto\"/><w:jc w:val=\"center\"/></w:pPr><w:rPr><w:b/><w:sz w:val=\"36\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"Author\"><w:name w:val=\"Author\"/><w:basedOn w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:spacing w:after=\"0\"/><w:jc w:val=\"center\"/></w:pPr><w:rPr><w:sz w:val=\"20\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"AuthorName\"><w:name w:val=\"Author Name\"/><w:basedOn w:val=\"Author\"/>",
"<w:pPr><w:spacing w:before=\"120\"/></w:pPr><w:rPr><w:b/><w:sz w:val=\"22\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"Note\"><w:name w:val=\"Note\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:jc w:val=\"left\"/></w:pPr><w:rPr><w:sz w:val=\"18\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:styleId=\"Heading1\"><w:name w:val=\"heading 1\"/><w:basedOn w:val=\"Normal\"/><w:next w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:keepNext/><w:keepLines/><w:spacing w:before=\"360\" w:after=\"120\"/><w:jc w:val=\"left\"/><w:outlineLvl w:val=\"0\"/></w:pPr>",
"<w:rPr><w:b/><w:sz w:val=\"28\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:styleId=\"Heading2\"><w:name w:val=\"heading 2\"/><w:basedOn w:val=\"Normal\"/><w:next w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:keepNext/><w:keepLines/><w:spacing w:before=\"280\" w:after=\"80\"/><w:jc w:val=\"left\"/><w:outlineLvl w:val=\"1\"/></w:pPr>",
"<w:rPr><w:b/><w:sz w:val=\"24\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:styleId=\"Heading3\"><w:name w:val=\"heading 3\"/><w:basedOn w:val=\"Normal\"/><w:next w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:keepNext/><w:keepLines/><w:spacing w:before=\"200\" w:after=\"60\"/><w:jc w:val=\"left\"/><w:outlineLvl w:val=\"2\"/></w:pPr>",
"<w:rPr><w:b/><w:sz w:val=\"22\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:styleId=\"Heading4\"><w:name w:val=\"heading 4\"/><w:basedOn w:val=\"Normal\"/><w:next w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:keepNext/><w:keepLines/><w:spacing w:before=\"160\" w:after=\"40\"/><w:jc w:val=\"left\"/><w:outlineLvl w:val=\"3\"/></w:pPr>",
"<w:rPr><w:b/><w:i/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"AbstractHeading\"><w:name w:val=\"Abstract Heading\"/><w:basedOn w:val=\"Normal\"/>",
"<w:next w:val=\"Abstract\"/><w:pPr><w:keepNext/><w:spacing w:before=\"240\" w:after=\"60\"/><w:jc w:val=\"center\"/></w:pPr>",
"<w:rPr><w:b/><w:sz w:val=\"20\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"Abstract\"><w:name w:val=\"Abstract\"/><w:basedOn w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:ind w:left=\"567\" w:right=\"567\"/></w:pPr><w:rPr><w:sz w:val=\"20\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"SceneBreak\"><w:name w:val=\"Scene Break\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:jc w:val=\"center\"/></w:pPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"ListItem\"><w:name w:val=\"List Item\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:spacing w:after=\"60\"/><w:ind w:left=\"425\" w:hanging=\"425\"/></w:pPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"ListContinue\"><w:name w:val=\"List Continuation\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:spacing w:after=\"60\"/><w:ind w:left=\"425\"/></w:pPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"Figure\"><w:name w:val=\"Figure\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:spacing w:before=\"120\" w:after=\"60\" w:line=\"240\" w:lineRule=\"auto\"/><w:jc w:val=\"center\"/></w:pPr></w:style>",
"<w:style w:type=\"paragraph\" w:styleId=\"Caption\"><w:name w:val=\"caption\"/><w:basedOn w:val=\"Normal\"/><w:next w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:spacing w:before=\"60\" w:after=\"200\"/></w:pPr><w:rPr><w:sz w:val=\"18\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"Equation\"><w:name w:val=\"Equation\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:spacing w:before=\"60\" w:after=\"60\"/><w:jc w:val=\"center\"/></w:pPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"EquationNumber\"><w:name w:val=\"Equation Number\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:spacing w:after=\"0\"/><w:jc w:val=\"right\"/></w:pPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"Code\"><w:name w:val=\"Code\"/><w:basedOn w:val=\"Normal\"/><w:qFormat/>",
"<w:pPr><w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"F4F4F4\"/><w:spacing w:after=\"0\" w:line=\"240\" w:lineRule=\"auto\"/>",
"<w:jc w:val=\"left\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Consolas\" w:hAnsi=\"Consolas\" w:cs=\"Consolas\"/><w:sz w:val=\"17\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:customStyle=\"1\" w:styleId=\"TableText\"><w:name w:val=\"Table Text\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:spacing w:before=\"20\" w:after=\"20\" w:line=\"240\" w:lineRule=\"auto\"/><w:jc w:val=\"left\"/></w:pPr>",
"<w:rPr><w:sz w:val=\"18\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:styleId=\"FootnoteText\"><w:name w:val=\"footnote text\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:spacing w:after=\"80\"/></w:pPr><w:rPr><w:sz w:val=\"18\"/></w:rPr></w:style>",
"<w:style w:type=\"paragraph\" w:styleId=\"Bibliography\"><w:name w:val=\"Bibliography\"/><w:basedOn w:val=\"Normal\"/>",
"<w:pPr><w:spacing w:after=\"80\"/><w:ind w:left=\"567\" w:hanging=\"567\"/><w:jc w:val=\"left\"/></w:pPr><w:rPr><w:sz w:val=\"20\"/></w:rPr></w:style>",
"<w:style w:type=\"character\" w:styleId=\"Hyperlink\"><w:name w:val=\"Hyperlink\"/><w:basedOn w:val=\"DefaultParagraphFont\"/>",
"<w:rPr><w:color w:val=\"1F4E99\"/><w:u w:val=\"single\"/></w:rPr></w:style>",
"<w:style w:type=\"character\" w:customStyle=\"1\" w:styleId=\"CrossReference\"><w:name w:val=\"Cross Reference\"/>",
"<w:basedOn w:val=\"DefaultParagraphFont\"/><w:rPr><w:color w:val=\"1F4E99\"/></w:rPr></w:style>",
"</w:styles>",
);
pub(crate) fn core_props(title: &str, meta: &Meta) -> String {
let identifier = meta
.arxiv_id
.as_ref()
.map(|id| format!("<dc:identifier>arXiv:{}</dc:identifier>", esc(id)))
.unwrap_or_default();
format!(
"{XML_DECL}<cp:coreProperties xmlns:cp=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\" \
xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\
<dc:title>{}</dc:title>{identifier}<dc:description>Converted from PDF by kakemono</dc:description></cp:coreProperties>",
esc(title)
)
}
pub(crate) fn document_rels(images: &[(String, String)], links: &[(String, String)]) -> String {
let mut out = format!(
"{XML_DECL}<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rIdStyles\" Type=\"{REL_BASE}/styles\" Target=\"styles.xml\"/>\
<Relationship Id=\"rIdSettings\" Type=\"{REL_BASE}/settings\" Target=\"settings.xml\"/>"
);
for (id, target) in images {
out.push_str(&format!(
"<Relationship Id=\"{id}\" Type=\"{REL_BASE}/image\" Target=\"{}\"/>",
esc(target)
));
}
for (id, url) in links {
out.push_str(&format!(
"<Relationship Id=\"{id}\" Type=\"{REL_BASE}/hyperlink\" Target=\"{}\" TargetMode=\"External\"/>",
esc(url)
));
}
out.push_str("</Relationships>");
out
}
pub(crate) fn section_props() -> String {
format!(
"<w:sectPr><w:pgSz w:w=\"{PAGE_WIDTH_TWIPS}\" w:h=\"{PAGE_HEIGHT_TWIPS}\"/>\
<w:pgMar w:top=\"{MARGIN_TWIPS}\" w:right=\"{MARGIN_TWIPS}\" w:bottom=\"{MARGIN_TWIPS}\" w:left=\"{MARGIN_TWIPS}\" \
w:header=\"720\" w:footer=\"720\" w:gutter=\"0\"/></w:sectPr>"
)
}