import MongoClient from 'mongodb';

export default class MongoSaver {

  constructor(url) {
    this.url = url;
  }

  connect() {
    let saver = this;
    return new Promise((resolve, reject) => {
      if (!saver.ready) {
        MongoClient.connect(saver.url, (err, db) => {
          if (err) {
            console.log('Unable to connect to the mongoDB server. Error:', err);
            reject(err);
          } else {
            console.log('Connection established to', this.url);
            saver.db = db;
            saver.collection = db.collection('listings');
            saver.ready = true;
            resolve();
          }
        });
      } else {
        resolve();
      }
    });
  }

  save(results) {
    let resultsForMongo = Object.keys(results).map((key) => {
      return results[key]
    });

    for(const doc of resultsForMongo) {
      this.collection.update({ url: doc.url }, doc, { upsert: true });
    }
  }

  async close() {
    try {
      //Close connection
      await this.disconnect();
    } catch (e) {
      console.log("error closing", e);
    }
  }

  async disconnect() {
    return new Promise((resolve, reject) => {
      this.db.close();
      resolve();
    });
  }

  async open() {
    await this.connect();
  }

}
