#import "theme.typ": *

#let format-cover-date(date, fallback) = {
  if date != none {
    date.display("[year].[month repr:numerical].[day]")
  } else if type(fallback) == str {
    fallback
  } else {
    str(fallback)
  }
}

#let cover-title-lines(title) = {
  let trimmed = title.trim()
  if trimmed.contains("\n") {
    let parts = trimmed.split("\n").filter(p => p.trim().len() > 0)
    if parts.len() >= 2 {
      return (parts.at(0).trim(), parts.slice(1).join(" ").trim())
    }
  }

  let marker = "智能"
  let m = trimmed.match(regex(marker))
  if m == none {
    (trimmed, none)
  } else {
    let end = m.end
    let line-2 = trimmed.slice(end)
    if line-2.len() > 0 {
      (trimmed.slice(0, end), line-2)
    } else {
      (trimmed, none)
    }
  }
}

#let cover-title-block(line-1, line-2) = {
  block(breakable: false, spacing: 0pt)[
    #text(
      font: heading-font,
      size: size-cover-title,
      weight: "bold",
      fill: cover-panel-title-color,
      hyphenate: false,
      top-edge: "cap-height",
      bottom-edge: "baseline",
    )[#line-1]
    #if line-2 != none [
      #move(dy: cover-panel-title-pull)[
        #text(
          font: heading-font,
          size: size-cover-title-line2,
          weight: "bold",
          fill: cover-panel-title-color,
          hyphenate: false,
          top-edge: "cap-height",
          bottom-edge: "baseline",
        )[#line-2]
      ]
    ]
  ]
}

#let cover-logo() = {
  let mark = cover-logo-size * 0.64
  let offset = cover-logo-size * 0.36
  box(width: cover-logo-size, height: cover-logo-size)[
    #place(left + top)[
      #rect(width: mark, height: mark, fill: color-primary)
    ]
    #place(left + top, dx: offset, dy: offset)[
      #rect(width: mark, height: mark, fill: none, stroke: 1.5pt + color-primary)
    ]
  ]
}

#let cover-quote-mark() = {
  text(
    font: heading-font,
    size: cover-quote-size,
    fill: cover-quote-color,
  )[”]
}

#let cover-panel-footer(meta, date-text) = {
  block(width: 100%, breakable: false)[
    #text(font: heading-font, size: cover-panel-author-size, weight: "bold", fill: cover-panel-title-color)[
      #meta.company
    ]
    #v(cover-panel-text-gap * 0.25)
    #text(font: body-font, size: cover-panel-role-size, fill: cover-panel-subtitle-color)[
      技术报告 · #meta.version
    ]
    #v(cover-panel-role-gap-before-line)
    #block(width: 100%, breakable: false)[
      #place(right + top)[
        #cover-quote-mark()
      ]
      #block(width: 100%, inset: (right: cover-quote-clearance))[
        #line(length: cover-panel-footer-line-length, stroke: cover-panel-footer-rule-stroke + cover-panel-footer-line-color)
        #v(cover-panel-meta-gap-after-line)
        #grid(
          columns: (auto, auto, auto),
          column-gutter: cover-panel-meta-column-gap,
          align: (left, center + horizon, left),
          [
            #text(font: body-font, size: cover-panel-meta-label, fill: cover-panel-meta-color)[
              报告版本
            ]
            #v(tw(0.5))
            #text(font: body-font, size: cover-panel-meta-size, fill: cover-panel-title-color)[
              #meta.version
            ]
          ],
          box(width: cover-panel-meta-divider-width, height: tw(8), fill: cover-panel-footer-line-color),
          [
            #text(font: body-font, size: cover-panel-meta-label, fill: cover-panel-meta-color)[
              发布日期
            ]
            #v(tw(0.5))
            #text(font: body-font, size: cover-panel-meta-size, fill: cover-panel-title-color)[
              #date-text
            ]
          ],
        )
      ]
    ]
  ]
}

#let cover-panel-bottom(meta, date-text) = {
  cover-panel-footer(meta, date-text)
}

#let cover-below-panel(meta, date-text) = {
  block(height: cover-band-height)[
    #pad(left: cover-footer-inset)[
      #align(horizon)[
        #grid(
          columns: (auto, auto, auto),
          column-gutter: cover-footer-column-gap,
          align: bottom,
          [
            #text(font: heading-font, size: cover-footer-label, weight: "bold", fill: cover-footer-heading)[
              单位
            ]
            #v(cover-footer-line-gap)
            #text(font: body-font, size: cover-footer-body, fill: cover-footer-text)[
              #meta.company
            ]
            #v(cover-footer-line-gap-tight)
            #text(font: body-font, size: cover-footer-note, fill: cover-text-muted)[
              智能矿石分选 · 技术方案与工程实施
            ]
          ],
          box(width: 1pt, height: cover-footer-divider-height, fill: cover-footer-rule),
          [
            #text(font: heading-font, size: cover-footer-label, weight: "bold", fill: cover-footer-heading)[
              信息
            ]
            #v(cover-footer-line-gap)
            #text(font: body-font, size: cover-footer-body, fill: cover-footer-text)[
              文档类型　技术报告
            ]
            #v(cover-footer-line-gap-tight)
            #text(font: body-font, size: cover-footer-note, fill: cover-text-muted)[
              #meta.brand · #meta.version · #date-text
            ]
          ],
        )
      ]
    ]
  ]
}

#let cover-page(meta, date: none) = {
  let date-text = format-cover-date(date, meta.date)
  let (title-line-1, title-line-2) = cover-title-lines(meta.title)

  page(
    paper: "a4",
    margin: page-margin-cover,
    fill: cover-bg,
    header: none,
    footer: none,
  )[
    #block(width: 100%, height: cover-body-height)[
      #block(height: cover-band-height)[
        #pad(left: cover-content-inset)[
          #align(horizon)[
            #grid(
              columns: (auto, 1fr),
              column-gutter: tw(3),
              align: horizon,
              cover-logo(),
              [
                #text(font: heading-font, size: cover-brand-size, weight: "bold", fill: cover-text)[
                  #meta.brand
                ]
                #v(cover-footer-line-gap)
                #text(font: body-font, size: cover-tagline-size, fill: cover-text-muted)[
                  #meta.company
                ]
              ],
            )
          ]
        ]
      ]

      #v(cover-header-gap)

      #pad(right: cover-panel-right-gap)[
        #block(width: 100%, height: cover-panel-height, fill: cover-panel-fill)[
          #pad(
            left: cover-content-inset,
            right: cover-margin-right,
            top: cover-panel-inset-y,
            bottom: cover-panel-inset-bottom-y,
          )[
            #block(width: 100%, height: 100%)[
              #place(top + left)[
                #block(width: 100%)[
                  #text(
                    font: heading-font,
                    size: cover-panel-kicker,
                    weight: "medium",
                    fill: cover-panel-kicker-color,
                    tracking: 0.08em,
                  )[TECHNICAL REPORT · 技术报告]

                  #v(cover-panel-title-outer-gap)
                  #cover-title-block(title-line-1, title-line-2)

                  #v(cover-panel-title-outer-gap)
                  #text(
                    font: heading-font,
                    size: size-cover-subtitle,
                    fill: cover-panel-subtitle-color,
                    tracking: 0.04em,
                  )[INTELLIGENT ORE SORTING · 智能矿石分选]
                ]
              ]

              #place(left + horizon)[
                #line(
                  length: cover-panel-rule-width,
                  stroke: cover-panel-accent-stroke + cover-panel-divider-color,
                )
              ]

              #place(bottom + left)[
                #block(width: 100%, breakable: false)[
                  #cover-panel-bottom(meta, date-text)
                ]
              ]
            ]
          ]
        ]
      ]

      #v(cover-below-gap)
      #cover-below-panel(meta, date-text)
    ]
  ]
}
