const repeat = require('lodash/repeat')
const toSnakeCase = require('lodash/snakeCase')
const PALETTE = require('../dist/colors.meta.json')

const paletteColors = PALETTE.colors.map(colorArray => {
  return colorArray.map(formatShade)
})

printDocument(paletteColors)

function formatColors(colorArrays, indent = 0) {
  const whitespace = repeat(' ', indent)
  const prefix = value => whitespace + value

  return colorArrays.map(colorArray => {
    return colorArray.map(prefix).join('\n')
  }).join('\n\n')
}

function formatShade(colorObject) {
  return `<color name="${toSnakeCase(colorObject.name.toLowerCase())}">${colorObject.value}</color>`
}

function printDocument(colorArrays) {
  const xml = [
    '<?xml version="1.0" encoding="utf-8"?>',
    '<resources',
    '    xmlns:tools="http://schemas.android.com/tools"',
    '    tools:ignore="UnusedResources">',
    '',
    `    <!-- Generated by Color Studio v${PALETTE.version} -->`,
    formatColors(colorArrays, 4),
    '</resources>',
  ].join('\n')

  console.log(xml)
}
