var portServer = 8001;                                                                                      
var securePort = 8020;
var express = require('express');
var app = express();
var fs = require('fs');
var request = require('request');
var bodyParser = require("body-parser");
var moment = require('moment');
/*var fetch = require("node-fetch");*/
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var cron = require('node-cron');
var http = require('http');
var https = require('https');
var MongoClient = require('mongodb').MongoClient;
var sanitize = require('mongo-sanitize');
var cheerio = require("cheerio"); //jQuery Substitute
var puppeteer = require("puppeteer");
const { createWorker } = require("tesseract.js");
 
 
// const pug = require('pug');
const { exec } = require('child_process');
const path = require('path');
 
app.set('view engine', "pug")
 
// app.use("/images", express.static(path.join(__dirname, '/images')));
app.use("/images", express.static(path.join(__dirname, "/savedImages")));
app.use("/static", express.static(path.join(__dirname, "/static")));

const baseFilePath = "./savedImages";

app.get('/', function (req, res) {
       res.send('Hello World');
});

app.get("/upload", function(req, res){
    res.sendFile(path.join( __dirname, "views/Upload.html"));
});

app.get("/takeScreenshot", async function(req, res){
    let url = req.query["url"];
    let name = req.query["name"];

    puppeteer.launch({
        headless: "new",
        defaultViewport: {
          width: 1280,
          height: 2000,
        },
      })
      .then(async (browser) => {
        const page = await browser.newPage();
        await page.goto(url);
        await page.screenshot({ path: `${baseFilePath}/${name}.png` });
        await browser.close();
      });
      res.send("204 no content");
      res.end();
});

app.get("/getValueFromImage", async function(req, res){
    /* 
    Sometimes grabs some random characters that are not on the screen.  This is especially obvious
    when grabing the price off of LiquidationDelivered.com.  The rectangle has to be pretty tight on the price
    in order to avoid grabbing random characters.  Need to investigate because that could be a huge issue.
    */
    console.log("Pulling info from image");
    let name = req.query["name"]; 
    let top = req.query["top"]; //374
    let left = req.query["left"]; //596
    let width = req.query["width"];//75
    let height = req.query["height"];//30

    console.log("Loading Image");
    const img = fs.readFileSync(`${baseFilePath}/${name}.png`);

    const worker = await createWorker("eng");
    const rectangle = { left: left, top: top, width: width, height: height };
    console.log(JSON.stringify(rectangle));
    (async () => {
      const {
        data: { text },
      } = await worker.recognize(img,
        { rectangle }
      );
      console.log(text)
      console.log("Found Price!");
      res.send(text);
      res.end();
      console.log(text);
      await worker.terminate();
    })();
    
    
});

app.get("/getVisualPrice", async function(req, res){
    console.log("Getting Visual Price");
    let url = req.query["url"];
    let name = req.query["name"]; 
    let top = req.query["top"]; //374
    let left = req.query["left"]; //596
    let width = req.query["width"];//75
    let height = req.query["height"];//30

    puppeteer.launch({
        headless: "new",
        defaultViewport: {
          width: 1280,
          height: 2000,
        },
      })
      .then(async (browser) => {
        const page = await browser.newPage();
        await page.goto(url);
        await page.screenshot({ path: `${baseFilePath}/${name}.png` });
        await browser.close();

        console.log("Reading Image");
        const img = fs.readFileSync(`${baseFilePath}/${name}.png`);

        const worker = await createWorker("eng");
        const rectangle = {
          left: left,
          top: top,
          width: width,
          height: height,
        };
        let myText = "Default";
        (async () => {
          console.log("Finding text");
          const {
            data: { text },
          } = await worker.recognize(img, { rectangle });
          res.send(text);
          res.end();
          console.log(text);
          await worker.terminate();
        })();

      });

});

app.get("/getPrice", async function(req, res){
    try{
        console.log(`Getting Price!`);
        //console.log(`Request Body: \n ${JSON.stringify(req.body["instanceCount"])}`);
        let url = req.query["url"];
        let prefix = req.query["prefix"];
        let instanceCount = parseInt(req.query["instanceCount"]);
        let idName = req.query["idName"];


        console.log(`Get Price Info:\n
            Prefix: ${prefix}\n
            InstanceCount: ${instanceCount}\n
            idName: ${idName}\n
            URL: ${url}\n
        `);

        request(url, (err, resp, html) => {
            const $ = cheerio.load(html);
            console.log(html);
            let finalAnswer = "";
            if(instanceCount == 0){
                console.log("Getting single instance");
                let item = {};
                console.log($(idName));
                
                $(idName).each((i, el) => {
                    //console.log("item");
                    //console.log(el);
                   item = $(el).text(); 
                });
                console.log(JSON.stringify(item));
                finalAnswer = item;
            }else{
                $(idName).each((i, el) => {
                    if(i == instanceCount){
                        const item = $(el).text();
                        finalAnswer = item.split(prefix).reverse()[0];
                    }
                });
            }
            console.log(`Answer: ${finalAnswer}`);
            res.send(`Answer: ${finalAnswer}`);
            res.end();
        });
    }catch(e){
        console.error(e);
        res.end();
    }
});


const httpServer = http.createServer(app);
//const httpsServer = https.createServer(credentials, app);

var server = httpServer.listen(portServer, function () {
       var host = server.address().address
       var port = server.address().port

       console.log("Server is listening at http://%s:%s", host, port)
});

