const asyncHandler = require("express-async-handler");
const fetch = require('node-fetch');
const {ScraperTools} = require('../AuctionServices/Scraper/Scraper.js')

const Drouot = require('../AuctionServices/Scraper/Drouot/Drouot.js')
const Interencheres = require('../AuctionServices/Scraper/Interencheres/Interencheres.js')


let getAuctionPlatform = function(Url){
  
	let AuctionPlatform
	let STools = new ScraperTools();
  
	switch (STools.detectPlatform(Url)) {
  
	  case STools._CONST_INTERENCHERES:
		AuctionPlatform = new Interencheres(Url);
		break;
	  
	  case STools._CONST_DROUOT:
		AuctionPlatform = new Drouot(Url);
		break;
  
	  default:
		break;
	}
  
	return AuctionPlatform
}

let CleanUrl = function(url){
  
	if(String(url).split("http").length > 1){
		url = 'http'+String(url).split("http")[1]
	}else{
		url = ""
	}

	return url
}

// ## PUPPETEER CLUSTER
// get Sale info
exports.getSaleInfos = asyncHandler(async (req, res, next) => {

	let url = req.params.url	
	url = decodeURIComponent(url);
	
	url = CleanUrl(url)
	if(url == ""){
		res.status(400).send("URL not supported")
	}
	
	try{
		let AuctionPlatform = getAuctionPlatform(url);
		if(AuctionPlatform){
			const SaleInfos = await req.puppeteerCluster.execute(AuctionPlatform.getSaleInfos);
			res.json(SaleInfos);
		}else{
			res.status(400).send("URL not supported")
		}
	}catch(e){
		res.status(500).send("Error: "+e)
	}		
});

// get Sale Lot list
exports.getLotList = asyncHandler(async (req, res, next) => {

	let url = req.params.url	
	url = decodeURIComponent(url);
	
	url = CleanUrl(url)
	if(url == ""){
		res.status(400).send("URL not supported")
	}
	
	try{
		let AuctionPlatform = getAuctionPlatform(url);
		if(AuctionPlatform){
			const LotList = await req.puppeteerCluster.execute(AuctionPlatform.getLotList);
			res.json(LotList);
		}else{
			res.status(400).send("URL not supported")
		}
	}catch(e){
		res.status(500).send("Error: "+e)
	}		
});

// ## AGENT PUPPETEER
//Follow a live Sale
exports.followSale = asyncHandler(async (req, res, next) => {

	let url = req.params.url	
	url = decodeURIComponent(url);
	
	url = CleanUrl(url)
	if(url == ""){
		res.status(400).send("URL not supported")
	}
	
	try{
		let AuctionPlatform = getAuctionPlatform(url);
		if(AuctionPlatform){
			console.log('Scrapper followSale : '+encodeURIComponent(url))
			fetch('http://agent/internApi/follow/sale/'+encodeURIComponent(url))
    		   .then(response => {
				console.log("fetch OK")
				//response.json()
				} )
    		   .then(saleInfo => {})
			   .catch(error => {
				     console.error(error);
				     throw new Error('Error: '+error)
				});
			
			//res.status(200).send({status: "Following"})
			res.status(500).send({"Error": "ok"})
		}else{
			res.status(400).send("URL not supported")
		}
	}catch(e){
		res.status(500).send("Error: "+e)
	}		
});
