const express = require('express');
const app = express();
const cron = require('node-cron');
const {MongoClient} = require('mongodb');

const uri = "mongodb+srv://Nikola:Nikola@cluster0.jvqzh.mongodb.net/admin?authSource=admin&replicaSet=atlas-7exvp3-shard-0&w=majority&readPreference=primary&appname=MongoDB%20Compass&retryWrites=true&ssl=true";
const client = new MongoClient(uri);
client.connect();
createListing =  async function (client, data){
  const result = await client.db("scraper").collection("scraping").insertOne(data);
  console.log(`New scraping created with the following id: ${result.insertedId}`);
}
var apartments = require('./apartments.js');
var houses = require('./houses.js');

app.set('json spaces', 2);

const axios = require('axios');
const cheerio = require('cheerio');
const { map } = require('cheerio/lib/api/traversing');

const url = 'https://www.apartments.com/two-west-chicago-il/jqn1nf6/';
app.get('/', (req, res) => {
  axios(url).then(response => {
    const html = response.data;
    const $ = cheerio.load(html);

    var data = apartments.apartment($);
    res.json(data);
  });
});


app.get('/houses/*', (req, res) => {
  var url = req.params[0];
  axios(url).then(response => {
    const html = response.data;
    const $ = cheerio.load(html);

    var data = houses.house($);
    res.json(data);
  });
});


app.get('/filters/*', async (req, res) => {
  var url = req.params[0];
  const filterPage = await axios(url);
  const html = filterPage.data;
  const $ = cheerio.load(html);

  const propertyLins = $('#placardContainer .property-link').map(function () {
    return $(this).attr('href');
  }).get();

  var properties = [];
  for (const link of propertyLins){
    var response = await axios(link);

    var property = apartments.apartment(cheerio.load(response.data));
    properties.push(property);
  }

  res.json(properties);
});
app.get('/apartments/*', (req, res) => {
  var url = req.params[0];
  axios(url).then(response => {
    const html = response.data;
    const $ = cheerio.load(html);

    var data = apartments.apartment($);

    createListing(client, data);
    res.json(data);


  });
});
const port = 3333;
var task = cron.schedule('* * * * *', function() {
  console.log(`Runned job...`)
});
  var options = {
    host: 'http://localhost',
    port:port,
    path: '/apartments/https://www.apartments.com/essex-on-the-park-chicago-il/begd58b/',
    method: 'GET'
  };
  
  http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      console.log('BODY: ' + chunk);
    });
  }).end();

  


task.start()

task.stop();

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
});


