'use strict'

const fs = require('fs')
const path = require('path')

const rimraf = require('rimraf')
const chalk = require('chalk')

const console = require('../helpers/color-console')
const sfs = require('../helpers/sfs')
const {installWatcher, installDevelopmentWatcher} = require('../helpers/watcher')

const {build, createAndGetBuildFolder} = require('./builder')
const loader = require('./loader')

const {assetsToCopy} = require('../assets')
const config = require('../config')

// -------------------------------------------------------------
// Module.
// -------------------------------------------------------------

function generate (entryPoint) {
  console.log(`Starting directory: ${chalk.green(entryPoint)}`)

  const pages = findData(entryPoint)

  if (countDataFiles(pages) === 0) {
    console.log('')
    console.warn('No data files found!')
    return
  }

  generateHTML(pages)
  exportAssets()

  console.log('')
  console.log('Done! 👌')

  if (config.commands.build.watch) {
    const port = config.commands.build.port
    showHeader(`Watching and serving files from http://localhost:${port}/`)
    startWatcher(entryPoint, () => generateHTML(findData(entryPoint)))
  }
}

function findData (entryPoint) {
  showHeader('Finding data')

  const pages = {products: []}

  // Inspect given path for data files and get all correct ones.
  let files = sfs.findAllFiles(entryPoint, { maxDepth: 1, ignoredFolders: ['build', 'node_modules'] })

  files.filter(isDataFile).forEach(file => {
    try {
      let presskit = loader.loadDataFile(file)
      if (!presskit) return

      console.log(`- ${presskit.type}: "${presskit.title}" ${chalk.dim(file)}`)

      if (presskit.type === 'company') {
        if (pages.company) {
          console.warn('Multiple companies detected. This is not supported yet, only the last will be used!')
        }

        pages.company = { path: entryPoint, presskit: presskit }
      } else {
        pages.products.push({ path: file, presskit: presskit })
      }
    } catch (err) {
      console.error(err)
    }
  })

  return pages
}

function generateHTML (pages) {
  showHeader('Generating HTML')

  const productsList = []

  let analytics = ''
  if (pages.company && pages.company.presskit.analytics) {
    analytics = pages.company.presskit.analytics
  }

  for (const product of pages.products) {
    const outputPath = build(
      product.path,
      product.presskit,
      {
        company: pages.company.presskit,
        analytics
      }
    )

    productsList.push({path: outputPath, title: product.presskit.title})
  }

  // Company should be the last one, listing all the products
  if (pages.company) {
    // Notice that we provide a ref to all the products here.
    build(
      pages.company.path,
      pages.company.presskit,
      {
        products: productsList,
        analytics
      }
    )
  }
}

function exportAssets () {
  showHeader('Exporting assets')

  // Copy assets (CSS and stuff)
  copyMandatoryFiles()
}

// Print a console UI header text.
function showHeader (text) {
  console.log('')
  console.log(chalk.magenta(text + '…'))
}

// Add css and any mandatory files specified to the build directory
function copyMandatoryFiles () {
  let buildDir = createAndGetBuildFolder()

  for (let f of assetsToCopy) {
    let filepath = path.resolve(f)
    let filename = path.basename(filepath)
    let dir = path.basename(path.dirname(filepath))

    let targetDir = path.join(buildDir, dir)
    sfs.createDir(targetDir)

    let targetPath = path.join(targetDir, filename)

    console.log('- ' + targetPath)
    fs.writeFileSync(targetPath, fs.readFileSync(filepath))
  }
}

// Count the number of products/company in the presskit.
function countDataFiles (pages) {
  let count = 0
  count += (pages.company ? 1 : 0)
  count += pages.products.length

  return count
}

// Is a file qualifying to be a data file? ie. `data.xml`.
// We don't check the content here, just the name.
function isDataFile (filename) {
  var ext = path.extname(filename)
  filename = path.basename(filename, ext)

  return filename === 'data' && ext === '.xml'
}

// Start watching files and assets.
function startWatcher (entryPoint, callback) {
  if (config.commands.build.dev) {
    // Delete the assets (CSS/etc.) before.
    // They might not be there, but we must ensure that they are properly
    // deleted.
    //
    // This is necessary, otherwise, remaining copied CSS files will have
    // an higher priority than the ones provided by the server (from
    // the `assets/` folder).
    rimraf(path.join(createAndGetBuildFolder(), 'css'), () => {
      installDevelopmentWatcher(entryPoint, callback)
    })
  } else {
    installWatcher(entryPoint, callback)
  }
}

// -------------------------------------------------------------
// Exports.
// -------------------------------------------------------------

module.exports = {
  generate
}

@@ -1666,16 +1666,517 @@
 le)%7D%60)%0A%0A
+      // Legacy support%0A      // If this is a %22game%22 file at top level, it's probably a company%0A      // Display a warning%0A      if (path.dirname(file) === entryPoint && presskit.type !== 'company') %7B%0A        console.warn('The top level data.xml should start with a %3Ccompany%3E and not a %3Cgame%3E tag! ')%0A        console.warn('If this is a legacy presskit() file, please check the Migration guide (https://github.com/pixelnest/presskit.html#migration-guide).')%0A%0A        presskit.type = 'company'%0A      %7D%0A%0A
       if

Add retro-compatibility for presskit() root data.xml files