const yaml = require('js-yaml')
const ora = require('ora')

const { writeFile } = require('../helper')
const {
  category,
  baseConfig,
  pageUrl,
  siteName,
  stringField,
  markdownField,
  listField,
  objectField
} = require('./helper')

const PATH_DESTINATION = './public/admin/config.yml'

const configTree = {
  ...baseConfig(),
  collections: [
    category({
      meta: {
        name: 'euromat-theses',
        label: '[Euromat] Theses',
        description: 'The theses selection pages for the EUROMAT.',
        folder: 'src/app/euromat/content/theses'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('backBtn', 'Label for back button'),
        stringField('positive', 'Label for positive'),
        stringField('neutral', 'Label for neutral'),
        stringField('negative', 'Label for negative'),
        stringField('skipped', 'Label for skipping thesis')
      ]
    }),
    category({
      meta: {
        name: 'euromat-emphasis',
        label: '[Euromat] Emphasis',
        description: 'The emphasis page which comes after the theses.',
        folder: 'src/app/euromat/content/emphasis'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('headline', 'Headline'),
        stringField('content', 'Description under headline'),
        stringField('skip', 'Skip emphasis page'),
        stringField('button', '[Label] Button to continue')
      ]
    }),
    category({
      meta: {
        name: 'euromat-results',
        label: '[Euromat] Results',
        description: 'The EUROMAT result page.',
        folder: 'src/app/euromat/content/results'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('headline', 'Headline for results'),
        stringField('entry', 'Description under headline'),
        stringField('hint', 'A short hint for users to know they can click the parties'),
        stringField('nationalParty', 'Text before national party logo'),
        stringField('thanks', 'Final words'),
        stringField('startoverBtn', '[Label] Button to start over'),
        stringField('indexBtn', '[Label] Button to go back to index')
      ]
    }),
    category({
      meta: {
        name: 'introduction',
        label: '[Page] Introduction',
        description: 'The introduction page of Euromat.',
        folder: 'src/app/intro/content'
      },
      fields: [
        siteName(),
        stringField('headline', 'Headline'),
        markdownField('content', 'Content'),
        stringField('button', '[Label] Button')
      ]
    }),
    category({
      meta: {
        name: 'party',
        label: '[Page] Party',
        description: 'This is an informational page comparing the voters results with a political parties position.',
        folder: 'src/app/party/content'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('subtitle', 'Subtitle'),
        stringField('backButtonLabel', '[Label] Back button'),
        stringField('legendLabel', '[Label] Legend'),
        stringField('partyAnswer', 'Answer'),
        stringField('tableHeading', 'Table Heading'),
        stringField('tableUser', 'Table User Description')
      ]
    }),
    category({
      meta: {
        name: 'about',
        label: '[Page] About us',
        description: 'The about us page.',
        folder: 'src/app/about/content'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('headline', 'Headline'),
        stringField('content', 'Content'),
        stringField('devDesign', 'Headline for development and design')
      ]
    }),
    category({
      meta: {
        name: 'faq',
        label: '[Page] FAQ',
        description: 'The FAQ page.',
        folder: 'src/app/faq/content'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('headline', 'Headline'),
        listField('questions', 'Questions', {
          list: [
            stringField('title', 'Title'),
            markdownField('answer', 'Answer')
          ]
        })
      ]
    }),
    category({
      meta: {
        name: 'contact',
        label: '[Page] Contact',
        description: 'The general contact page.',
        folder: 'src/app/contact/content'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('headline', 'Headline'),
        markdownField('content', 'Content')
      ]
    }),
    category({
      meta: {
        name: 'partner',
        label: '[Page] Partner',
        description: 'The partner page.',
        folder: 'src/app/partner/content'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('headline', 'Headline'),
        markdownField('content', 'Content'),
        listField('partners', 'Partner', {
          list: [
            stringField('name', 'Name of partner'),
            stringField('url', 'Link to partner'),
            markdownField('description', 'Description')
          ]
        }),
        listField('communcations', 'Communcations Partner', {
          list: [
            stringField('name', 'Name of partner'),
            stringField('url', 'Link to partner'),
            markdownField('description', 'Description')
          ]
        })
      ]
    }),
    category({
      meta: {
        name: 'privacy',
        label: '[Page] Data Privacy',
        description: 'The data privacy page.',
        folder: 'src/app/privacy/content'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('headline', 'Headline'),
        listField('topics', 'Topics', {
          list: [
            stringField('title', 'Title'),
            markdownField('content', 'Content')
          ]
        })
      ]
    }),
    category({
      meta: {
        name: 'imprint',
        label: '[Page] Imprint',
        description: 'Subpage for legal notice.',
        folder: 'src/app/imprint/content'
      },
      fields: [
        pageUrl(),
        siteName(),
        stringField('headline', 'Headline'),
        objectField('editorialStaff', 'Legal notice for editorial staff', {
          list: [
            stringField('addressLabel', 'Headline for address'),
            markdownField('address', 'Address'),
            stringField('representedLabel', 'Headline for represented by'),
            markdownField('represented', 'Represented by'),
            stringField('emailLabel', 'Headline for email'),
            stringField('email', 'Email address'),
            stringField('phoneLabel', 'Headline for phone'),
            stringField('phone', 'Phone number'),
            stringField('registerLabel', 'Headline for register entry'),
            markdownField('register', 'Register entry')
          ]
        }),
        objectField('netzbegruenung', 'Legal notice for Netzbegruenung', {
          list: [
            stringField('addressLabel', 'Headline for address'),
            markdownField('address', 'Address'),
            stringField('representedLabel', 'Headline for represented by'),
            markdownField('represented', 'Represented by'),
            stringField('emailLabel', 'Headline for email'),
            stringField('email', 'Email address'),
            stringField('phoneLabel', 'Headline for phone'),
            stringField('phone', 'Phone number'),
            stringField('registerLabel', 'Headline for register entry'),
            markdownField('register', 'Register entry')
          ]
        }),
        objectField('responsible', 'Responsible for website content', {
          hint: 'A short description who is responsible for the website content',
          list: [
            stringField('headline', 'Headline'),
            markdownField('text', 'Content'),
            stringField('mail', '[Label] Contact')
          ]
        }),
        listField('content', 'Content', {
          list: [
            stringField('title', 'Title'),
            markdownField('content', 'Content')
          ]
        })
      ]
    }),
    category({
      meta: {
        name: 'fourzerofour',
        label: '[Page] 404',
        description: 'Error page when the user (accidentally) reaches a nonexistent page.',
        folder: 'src/app/404/content'
      },
      fields: [
        siteName(),
        stringField('headline', 'Headline'),
        markdownField('content', 'Content'),
        stringField('button', '[Label] Button')
      ]
    }),
    category({
      meta: {
        name: 'settings',
        label: 'Settings',
        description: 'Overall settings for all pages (e.g. menu labels, social media, ...)',
        folder: 'src/data/meta'
      },
      fields: [
        siteName(),
        objectField('topMenu', 'Top Navigation', {
          hint: 'The main navigation for the website.',
          list: [
            stringField('index', '[Label] Introduction'),
            stringField('faq', '[Label] FAQ'),
            stringField('about', '[Label] About us'),
            stringField('partner', '[Label] Partner'),
            stringField('contact', '[Label] Contact')
          ]
        }),
        objectField('footerMenu', 'Footer Navigation', {
          hint: 'The footer navigation for the website.',
          list: [
            stringField('imprint', '[Label] Imprint'),
            stringField('privacy', '[Label] Data privacy')
          ]
        })
      ]
    })
  ]
}

;(async () => {
  const spinner = ora(`Building 'config.yml' for Netlify CMS admin.`).start()
  try {
    await writeFile(PATH_DESTINATION, yaml.safeDump(configTree))
    spinner.succeed(`'config.yml' has been successfully created!`)
  } catch (error) {
    spinner.fail(`There was an error creating 'config.yml': ${error.message}`)
    throw new Error(error)
  }
})()
