'use strict';

var chalk = require('chalk');
var moment = require('moment/min/moment-with-locales');

function calculateDate(date, period, value, subtract) {
  var dt = moment(date);
  if (subtract) {
    dt.subtract(value, period);
  } else {
    dt.add(value, period);
  }
  return dt;
}

function optDate(value, data) {
  var formatString = '(ddd) YYYY-MM-DD';
  var period = data.period || 'd';
  var date = moment(data.date);
  var newDate = calculateDate(date, period, value, data.subtract);
  var sDate = date.format(formatString);
  var sNewDate = newDate.format(formatString);
  console.log('\nInformed date: ' + chalk.yellow(sDate));
  return console.log('Calculated Date: ' + chalk.yellow(sNewDate));
}

module.exports = optDate;