'use strict';

const Promise = require('pinkie-promise');
const loading = require('loading-indicator');
const got = require('got');
const sexagesimal = require('sexagesimal');
const headers = {
  'user-agent': 'https://github.com/rafaelrinaldi/whereami'
};

const formatToDefault = data => `${data.latitude},${data.longitude}`;

const formatToJson = data => {
  return JSON.stringify({
    latitude: data.latitude,
    longitude: data.longitude
  });
};

const formatToSexagesimal = data => {
  const latitude = sexagesimal.format(data.latitude, 'lat');
  const longitude = sexagesimal.format(data.longitude, 'lon');

  return `${latitude},${longitude}`;
};

const formatToHuman = data => {
  if (!data.region_name || !data.region_name.trim().length) {
    return Promise.reject(new Error('unable to retrieve region'));
  }

  return `${data.region_name}`;
};

const formatOutput = (data, options) => {
  if (options.raw) {
    return JSON.stringify(data);
  }

  if (options.format === 'sexagesimal') {
    return formatToSexagesimal(data);
  } else if (options.format === 'json') {
    return formatToJson(data);
  } else if (options.format === 'human') {
    return formatToHuman(data);
  }

  return formatToDefault(data);
};

const whereami = options => {
  const interval = loading.start();

  return got('freegeoip.net/json/', {headers})
    .then(response => {
      return formatOutput(JSON.parse(response.body), options);
    })
    // Actually logs the output
    .then((response => {
      loading.stop(interval);
      console.log(response);
      return response;
    }))
    .catch(error => {
      loading.stop(interval);
      // The standard for error messages is lowercase, minor tweak to improve output
      const message = error.message.replace(/^\w/, string => string.toUpperCase());
      console.log(message);
      return error;
    });
};

module.exports = whereami;

@@ -690,170 +690,256 @@
 %7B%0A  
-if (!data.region_name %7C%7C !data.region_name.trim().length) %7B%0A    return Promise.reject(new Error('unable to retrieve region'));%0A  %7D%0A%0A  return %60$%7Bdata.region_name%7D%60
+const country = data.country_name;%0A  const region = data.region_name;%0A  const city = data.city;%0A%0A  return %5Bdata.city, data.region_name, data.country_name%5D%0A    .filter(field =%3E field && field.trim().length)%0A    .map(output =%3E output)%0A    .join(', ')
 ;%0A%7D;

add human format