const fetch = require('node-fetch');
const { LotDb } = require("./lotDb");
const lotDb = new LotDb();
const { SaleDb } = require("./saleDb");
const saleDb = new SaleDb();
const moment = require('moment-timezone');
const { config } = require("../config.js");

const Agent = class
{
    constructor()
    {
        this.ApiAgentURL = config.agent.ApiAgentURL;
        this.token = config.agent.token;
    }

    async request(url, method){
        return new Promise((resolve, reject) => {            
            fetch(url,{
                method: method,
                headers: {
                    'authorization': this.token
                }
            })
                .then(response => response.json())
                .then(data => {
                    resolve(data);
                })
                .catch(error => {
                    reject(error);
                });
        });
    }

    async getSaleInfos(url)
    {
        return new Promise((resolve, reject) => {
            url = encodeURIComponent(url);
            this.request(this.ApiAgentURL+'/sale/getSaleInfos/'+url, 'GET')                
                .then(data => {
                    resolve(data);
                })
                .catch(error => {
                    reject(error);
                });
        });
    }

    async prepareSale(id)
    {
        return new Promise(async (resolve, reject) => {    
            
            let Sale = await saleDb.get(id);

            const DateSale = moment.tz(Sale.date, "Europe/Paris");
            const NowParis = moment.tz(new Date(),"Europe/Paris")
           
            if (NowParis.isBefore(DateSale)){

                let url = Sale.url
                url = encodeURIComponent(url);
                this.request(this.ApiAgentURL+'/sale/getLotList/'+url, 'GET')
                    .then( async data => {

                        for (let lot of data) {
                            lot.sale_id = Sale._id                
                            await lotDb.post(lot);          
                        }
                        resolve(data);
                    })
                    .catch(error => {
                        reject(error);
                    });
            }else{
                console.log("Sale started or finished");
                resolve([]);
            }
        });
    }

    async followSale(id)
    {
        return new Promise(async (resolve, reject) => {
            let Sale = await saleDb.get(id);
        
            let url = Sale.url
            url = encodeURIComponent(url);
            this.request(this.ApiAgentURL+'/sale/followSale/'+url, 'GET')
                .then(async data => {

                    // set the Sale status to following
                    Sale.status = "following";
                    Sale = await saleDb.put(id, Sale);
                    resolve(data);
                })
                .catch(error => {
                    reject(error);
                });
        });
    }

}

module.exports = {Agent};