/* eslint-disable no-console */
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
const fs = require("fs");
const parser = require("xml2json");
const moment = require("moment");
const Mongoose = require("mongoose");
const iconv = require("iconv-lite");
const config = require("./config");

// TODO: envoyer mail lors d'une erreur

Mongoose.set("useNewUrlParser", true);
Mongoose.set("useFindAndModify", false);
Mongoose.set("useCreateIndex", true);
Mongoose.set("debug", config.mongodbDebug);

Mongoose.connect(config.mongodbUrl, config.mondeDbOptions);

const db = () => {
  const m = {};

  fs.readdirSync("./models")
    .filter(file => {
      return (
        file.indexOf(".") !== 0 &&
        file.slice(-3) === ".js" &&
        file !== "index.js"
      );
    })
    .forEach(file => {
      const model = require(`./models/${file.replace(".js", "")}`)(Mongoose);
      m[model.modelName] = model;
    });

  return m;
};

const models = db();

let foundGasStations = 0;
let done = 0;

const extractPrice = station => {
  const prices = [];

  if (station.prix) {
    for (let i = 0; i < station.prix.length; i += 1) {
      const currentPrice = station.prix[i];
      prices.push({
        gasType: currentPrice.nom,
        price: parseInt(currentPrice.valeur, 10) / 1000,
        updatedAt: moment(currentPrice.maj)
      });
    }
  }

  return prices;
};

const createOrUpdateGasStation = (station, callback) => {
  models.Stations.findOne({
    stationId: station.stationId
  })
    .then(item => {
      if (item) {
        item
          .update(station)
          .then(() => {
            callback(null);
          })
          .catch(callback);
      } else {
        // Create item
        const newItem = new models.Stations(station);
        newItem
          .save()
          .then(() => {
            callback(null);
          })
          .catch(callback);
      }
    })
    .catch(callback);
};

const next = () => {
  if (done === foundGasStations) {
    console.log(`DONE ${done} !`);
    process.exit();
  }
};

fs.readFile("public/gas-stations.xml", function(err, data) {
  if (err) {
    console.log("ERR:", err);
    process.exit();
    return false;
  }

  const json = parser.toJson(iconv.decode(data, "ISO-8859-1"));

  const rawStations = JSON.parse(json).pdv_liste.pdv;

  // console.log("STATIONS:", rawStations.length);
  // console.log("RANDOM STATION:", rawStations[12]);

  const stations = [];

  for (let i = 0; i < rawStations.length; i += 1) {
    const currentStation = rawStations[i];
    stations.push({
      stationId: currentStation.id,
      location: {
        type: "Point",
        coordinates: [
          Number(currentStation.longitude) / 100000,
          Number(currentStation.latitude) / 100000
        ]
      },
      prices: extractPrice(currentStation),
      services:
        currentStation.services && currentStation.services.service
          ? currentStation.services.service
          : [],
      postCode: currentStation.cp.toString(),
      address: currentStation.adresse,
      city: currentStation.ville
    });
  }

  foundGasStations = stations.length;

  // console.log(stations[12]);

  stations.forEach(type => {
    createOrUpdateGasStation(type, () => {
      done += 1;
      next();
    });
  });
  return true;
});
