let libs_root = "../"
const index_path = libs_root + "index.js";
const barcode_ = require(index_path)
const aspose_barcode = barcode_.AsposeBarcode
//in case you obtained the lib from npmjs.com by calling the command "npm i aspose.barcode"
// replace all above lines by two lines
// const barcode_ = require("aspose.barcode");
// const aspose_barcode = barcode_.AsposeBarcode

const path_to_license_file = "./lic/Aspose.BarCode.Nodejs.via.Java.lic";
const fs = require("fs");


function setLicense()
{
    let license = new aspose_barcode.License();
    if (fs.existsSync(path_to_license_file))
    {
        license.setLicense(path_to_license_file);
    }
    else
    {
        console.log("Path \"" + path_to_license_file + "\" doesn't exist\n");
    }
    let is_licensed = license.isLicensed();
    console.log('is licensed: '  +  is_licensed);
}

function isExists(file_path)
{
    if (fs.existsSync(file_path))
    {
        console.log(file_path + " exists")
    }
    else
    {
        console.log(file_path + " doesn't exist")
    }
}

/**
 * @param subFolder path to image
 * @param fileName name of image
 * @returns base64 encoded image
 */
function loadImageByName(subFolder, fileName)
{
    let filePath = subFolder + "/" + fileName;
    try
    {
        return fs.readFileSync(filePath).toString('base64');
    } catch (ex)
    {
        console.error(ex.toString());
    }
}

function saveImageBase64(image64, path)
{
    try
    {
        console.log("Will be saved to : " + path);
        let image_bytes = Buffer.from(image64, 'base64');
        fs.writeFileSync(path, image_bytes);
        return fs.existsSync(path);
    } catch (ex)
    {
        let barcode_exception = new barcodetestexception_.BarcodeTestException(ex);
        throw barcode_exception;
    }
}

module.exports = {
    aspose_barcode, setLicense, isExists, loadImageByName, saveImageBase64
};