// This function gets your whole document as its `body` and formats
// it as an article in the style of the IEEE.

// CHANGES MADE TO COMPLY WITH QRS
// Font changed from STIX TWO Text to Times New Roman
// Gutter changed from 12 to 18 pt
// Indent on consecutiv par removed
// Changed x margin to 0.75in
// Changed y margin to 1in
// Changed heading numbering to 1.1.1
// Changed heading styles
// Change author listing
// Change abstract heading boldness


#let ieee(
  // The paper's title.
  title: "Paper Title",

  // An array of authors. For each author you can specify a name,
  // department, organization, location, and email. Everything but
  // but the name is optional.
  authors: (),
  anon: false,

  // The paper's abstract. Can be omitted if you don't have one.
  abstract: none,

  // A list of index terms to display after the abstract.
  index-terms: (),

  // The article's paper size. Also affects the margins.
  paper-size: "us-letter",

  // The path to a bibliography file if you want to cite some external
  // works.
  bibliography-file: none,

  // The paper's content.
  body
) = {
  // Set document metdata.
  set document(title: title, author: authors.map(author => author.name))

  // Set the body font.
  set text(font: "STIX Two Text", size: 10pt)

  // Configure the page.
  set page(
    paper: paper-size,
    numbering: none,
    // The margins depend on the paper size.
    margin: if paper-size == "a4" {
      (x: 41.5pt, top: 80.51pt, bottom: 89.51pt)
    } else {
      (
        x: 0.75in,
        y: 1in,
        //x: (50pt / 216mm) * 100%,
        //top: (55pt / 279mm) * 100%,
        //bottom: (64pt / 279mm) * 100%,
      )
    }
  )

  // Configure equation numbering and spacing.
  set math.equation(numbering: "(1)")
  show math.equation: set block(spacing: 0.65em)

  // Configure lists.
  set enum(indent: 10pt, body-indent: 9pt)
  set list(indent: 10pt, body-indent: 9pt)

  // Configure headings.
  set heading(numbering: "1.")
  show heading: it => locate(loc => {
    // Find out the final number of the heading counter.
    let levels = counter(heading).at(loc)
    let deepest = if levels != () {
      levels.last()
    } else {
      1
    }

    set text(10pt, weight: 400)
    if it.level == 1 [
      #show: smallcaps
      #v(12pt, weak: true)
      #it
      #v(12pt, weak: true)
    ] else if it.level >= 2 [
      // Second-level headings are run-ins.
      #set par(first-line-indent: 0pt)
      //#set text(style: "italic")
      #v(10pt, weak: true)
      #it
      #v(10pt, weak: true)
    ]
  })

  // Display the paper's title.
  //v(3pt, weak: true)
  align(center, text(weight: "bold", size: 14pt, title))
  v(20pt, weak: true)
   

  // Make author list
  // Start with names 
  set align(center)
  let nbr_authors = authors.len()
  if not anon{
    for (i,author) in authors.enumerate(){
      if i+1 == nbr_authors{
          [#author.name\ ]
      }else{
          [#author.name, ]
      }
    }
    // Continue with affiliations
    for author in authors{
      if author.department+author.organization+author.location != ""{
      [#author.department, #author.organization, #author.location\ ]
      }
    }
    // Continue with the emails, same way as for authors
    for (i,author) in authors.enumerate(){
      if i+1 == nbr_authors{
          [#author.email\ ]
      }else{
          [#author.email, ]
      }
    }
    [\*Corresponding author]
  }else{
    [
      \ 
      \ 
      \ 
      \
      
    ]
  }
  
  set align(left)

  // Display the authors list.
  //for i in range(calc.ceil(authors.len() / 3)) {
  //  let end = calc.min((i + 1) * 3, authors.len())
  //  let is-last = authors.len() == end
  //  let slice = authors.slice(i * 3, end)
  //  grid(
  //    columns: slice.len() * (1fr,),
  //    gutter: 12pt,
  //    ..slice.map(author => align(center, {
  //      text(12pt, author.name)
  //      if "department" in author [
  //        \ #emph(author.department)
  //      ]
  //      if "organization" in author [
  //        \ #emph(author.organization)
  //      ]
  //      if "location" in author [
  //        \ #author.location
  //      ]
  //      if "email" in author [
  //        \ #link("mailto:" + author.email)
  //      ]
  //    }))
  //  )

  //  if not is-last {
  //    v(16pt, weak: true)
  //  }
  //}
  v(40pt, weak: true)

  // Start two column mode and configure paragraph properties.
  show: columns.with(2, gutter: 18pt)
  set par(justify: true, first-line-indent: 0pt)
  show par: set block(spacing: 0.65em)

  // Display abstract and index terms.
  if abstract != none [
    #set text(weight: "regular")
    #h(1em) _Abstract_---#abstract

    #if index-terms != () [
      #h(1em)_Index terms_---#index-terms.join(", ")
    ]
    #v(2pt)
  ]

  // Display the paper's contents.
  body

  // Display bibliography.
  if bibliography-file != none {
    show bibliography: set text(9pt)
    bibliography(bibliography-file, title: text(10pt)[References], style: "ieee")
  }
}
