/* eslint-disable import/no-absolute-path */
import getPrivateFile from '/imports/modules/server/get-private-file';
import { Meteor } from 'meteor/meteor';
import tbuffer from '@turf/buffer';
import tweet from '/imports/modules/server/twitter';
import countFires from './countFires';

const autonomies = tbuffer(JSON.parse(getPrivateFile('data/es-atlas-autonomies.json')), 0);
const portugal = tbuffer(JSON.parse(getPrivateFile('data/pt-atlas.json')), 0);

const europe = tbuffer(JSON.parse(getPrivateFile('data/europe-atlas.json')), 0);

const stringsToRemoveIberia = [
  ['Ciudad Autónoma de', ''],
  ['País Vasco/', ''],
  ['Pais Vasco', 'Euskadi'],
  ['Comunidad Foral de', ''],
  ['Región de', ''],
  ['Valencia', 'CValenciana'],
  ['Comunidad de Madrid', 'CMadrid'],
  ['León', 'CyLeón'],
  ['Leon', 'CyLeón'],
  ['Castilla-', ''],
  ['Cataluña/', ''],
  ['Cataluña', 'Catalunya'],
  ['La Mancha', 'CLMancha'],
  ['Baleares', 'IllesBalears'],
  ['Galicia', 'Galiza'],
  ['Principado de', ''],
  [' ', '']
];

const composeEuropeTweet = (num, stats) => {
  if (num <= 0) {
    return '';
  }
  const keys = Object.keys(stats);
  const numZones = keys.length;
  let text = '';
  keys.map((key, index) => { // , index
    const zoneFires = stats[key];
    const zoneFiresText = zoneFires > 1 ? index === 0 ? zoneFires : `${zoneFires}` : index === 0 ? 'One' : '1';
    const what = index > 0 ? ' ' : zoneFires > 1 ? ' active #wildfires ' : ' active #wildfire ';
    const subText = `${zoneFiresText}${what}in #${key}`;
    const delimiter = index === numZones - 1 ? ' and' : ',';
    const subTexts = index === 0 ? subText : `${delimiter} ${subText}`;
    text += subTexts;
    return '';
  });
  return `${text}`;
};

const composeIberiaTweet = (num, stats) => {
  if (num <= 0) {
    return '';
  }
  const keys = Object.keys(stats);
  const numZones = keys.length;
  let text = '';
  keys.map((key, index) => { // , index
    const zoneFires = stats[key];
    const zoneFiresText = zoneFires > 1 ? index === 0 ? zoneFires : `otros ${zoneFires}` : index === 0 ? 'Un' : 'otro';
    const what = index > 0 ? ' ' : zoneFires > 1 ? ' #fuegos activos ' : ' #fuego activo ';
    const subText = `${zoneFiresText}${what}en #${key}`;
    const delimiter = index === numZones - 1 ? ' y' : ',';
    const subTexts = index === 0 ? subText : `${delimiter} ${subText}`;
    text += subTexts;
    return '';
  });
  return `${text}`;
};

const tweetHeaders = ['🔥'];
const tweetFooters = ['Más info en: https://fuegos.comunes.org/fires'];

const tweetEuropeFires = () => {
  const resultEu = countFires(europe, [[' ', '']]);

  if (resultEu.total > 0) {
    const tweetText = `${tweetHeaders[0].trim()} ${composeEuropeTweet(resultEu.total, resultEu.fires)}. More info: https://fires.comunes.org/fires`;
    if (Meteor.isDevelopment) {
      console.log(tweetText);
    } else {
      tweet(tweetText, 'en');
    }
    return tweetText;
  }
};

const tweetIberiaFires = () => {
  const resultEs = countFires(autonomies, stringsToRemoveIberia);
  const resultPt = countFires(portugal, stringsToRemoveIberia);

  const fires = Object.assign(resultEs.fires, resultPt.fires);
  const total = resultEs.total + resultPt.total;

  // console.log(`Total fires ${total}, ${JSON.stringify(fires)}`);
  if (total > 0) {
    const tweetText = `${tweetHeaders[0].trim()} ${composeIberiaTweet(total, fires)}. ${tweetFooters[0]}`;
    if (Meteor.isDevelopment) {
      console.log(tweetText);
    } else {
      tweet(tweetText, 'es');
    }
    return tweetText;
  }
  return '';
};

if (Meteor.isDevelopment) {
  tweetIberiaFires();
  tweetEuropeFires();
}

export { composeIberiaTweet, tweetHeaders, tweetFooters, tweetIberiaFires, tweetEuropeFires };
