'use strict';
import request from 'request';

export default class CatalogClient {
    constructor(params) {
    }

    /**
     * 
     * @param {any} cb
     * 
     * @memberOf Client
     */
    getCatalog(cb) {
        request('https://bsboyshackday.herokuapp.com/api/products', function (error, response, body) {
            if (!error && response.statusCode == 200) {
                let parsed = JSON.parse(body);

                console.log(body.substring(1, 120)); // Show the HTML for the Google homepage.

                if(parsed === null) {
                    cb(null, "parsing error");
                }

                cb(parsed);
            } else {
                cb(null, error);
            }

        });

    }

    processImage(url_image) {
        request.post(
            'https://bsboyshackday.herokuapp.com/api/generals/processImage',
            { json: { url: url_image } },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    console.log(body)
            }
        });    
    }

    getProductThumb(productId, cb) {
        request('https://bsboyshackday.herokuapp.com/api/products/' + productId, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                let parsed = JSON.parse(body);

                console.log(body.substring(1, 120)); // Show the HTML for the Google homepage.

                if(parsed === null) {
                    cb(null, "parsing error");
                }

                cb(parsed.thumb);
            } else {
                cb(null, error);
            }

        });
        
    }

}