// @flow

import jsYaml from 'js-yaml'
import type {BuildingType} from '../types'
import {email} from 'react-native-communications'
import dedent from 'dedent'

export function submitReport(current: BuildingType, suggestion: BuildingType) {
  const before = stringifyBuilding(current)
  const after = stringifyBuilding(suggestion)

  const body = makeIssueBody(before, after)

  return email(
    ['allaboutolaf@stolaf.edu'],
    [],
    [],
    `[building] Suggestion for ${current.name}`,
    body,
  )
}

function makeIssueBody(before: string, after: string): string {
  return dedent`
    Please do not change anything below this line.

    ------------

    \`\`\`yaml
    ${before}
    \`\`\`

    \`\`\`yaml
    ${after}
    \`\`\`
  `
}

function stringifyBuilding(building: BuildingType): string {
  let res = ''
  let prev = null
  let data = jsYaml.safeDump(building, {flowLevel: 4}).split('\n')
  for (let line of data) {
    if (['schedule:', 'breakSchedule:'].includes(line)) {
      res += `\n\n${line}`
    } else if (line.startsWith('  - title:') && prev !== 'schedule:') {
      res += `\n\n${line}`
    } else {
      res += `\n${line}`
    }
    prev = line
  }
  return res
}

@@ -340,29 +340,29 @@
  body = make
-Issue
+Email
 Body(before,
@@ -367,16 +367,30 @@
 e, after
+, current.name
 )%0A%0A  ret
@@ -511,24 +511,25 @@
 ody,%0A  )%0A%7D%0A%0A
+%0A
 function mak
@@ -533,13 +533,13 @@
 make
-Issue
+Email
 Body
@@ -561,16 +561,31 @@
 , after:
+ string, title:
  string)
@@ -612,16 +612,74 @@
 dedent%60%0A
+    Hi! Thanks for letting us know about an hour change.%0A%0A
     Plea
@@ -740,16 +740,183 @@
 ------%0A%0A
+    Project maintainers: $%7BmakeIssueLink(before, after, title)%7D%0A%0A    $%7BmakeBody(before, after)%7D%0A  %60%0A%7D%0A%0Afunction makeBody(before, after) %7B%0A  return dedent%60%0A    Before:%0A
     %5C%60%5C%60
@@ -948,16 +948,27 @@
 %5C%60%5C%60%5C%60%0A%0A
+    After:%0A
     %5C%60%5C%60
@@ -997,32 +997,343 @@
   %5C%60%5C%60%5C%60%0A  %60%0A%7D%0A%0A
+function makeIssueLink(before: string, after: string, title: string): string %7B%0A  const q = querystring.stringify(%7B%0A    'labels%5B%5D': 'data/building',%0A    title: %60Building hours update for $%7Btitle%7D%60,%0A    body: makeBody(before, after),%0A  %7D)%0A  return %60https://github.com/StoDevX/AAO-React-Native/issues/new?$%7Bq%7D%60%0A%7D%0A%0A
 function stringi

add a handy link to create an issue from a report email