'use strict';

var fs = require('fs');
var tmp = require('tmp');
var Acho = require('acho');
var path = require('path');
var Download = require('download');
var logger = new Acho({color: true});

tmp.dir(function _tempDirCreated(err, tmpFolder, cleanup) {
  if (err) return logger.error(err);

  logger.info('Created temporal folder in ' + tmpFolder);
  logger.info('Downloading file...');

  new Download({mode: '755', extract: true})
  .get('https://github.com/twitter/twemoji/archive/gh-pages.zip')
  .dest(tmpFolder)
  .run(function(err) {

    if (err) return logger.error(err);
    logger.info('File downloaded and extracted succesful.');

    var folder = fs.readdirSync(tmpFolder)[0];
    var emojiDirectory = path.resolve(tmpFolder, folder, 'assets');

    logger.info('Read the directory ' + emojiDirectory);

    fs.readdir(emojiDirectory, function(err, files) {
      logger.info('Generating emoji keywords');
      if (err) return logger.error(err);
      var result = files.map(function(file) {
        file = file.split('.');
        return file[0];
      });

      var data = JSON.stringify(result, null, 2);
      fs.writeFile('emojis.json', data, function(err) {
        if (err) throw err;
        logger.success('File saved!');
      });
      // cleanup(); disable to prevent remove other tmp files of the system.
    });
  });
});
