// import * as edge from 'edge-js';

// let designerReport = edge.func({
//     assemblyFile: 'lib/GetEasy.Node.Printer.dll',
//     typeName: 'GetEasy.Node.Printer.NodeReportDesigner',
//     methodName: 'DesignerReport'
// });
// designerReport('', true);

import * as ffi from 'ffi';
import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';

const cpu = os.arch() === 'x64' ? '64' : '86';

const dllName = 'GetEasy.Node.Printer.' + cpu + '.dll';

const execDir = path.dirname(process.execPath);
let dllPath = path.join(execDir, dllName);
if (!fs.existsSync(dllPath)) {
    dllPath = path.join(__dirname, 'dll', dllName);
}
let callbackPointer: Buffer;
const dll = ffi.Library(dllPath, {
    designerReport: ['void', ['string', 'string']],
    reportShowPreview: ['void', ['string', 'string']],
    reportPrinter: ['void', ['string', 'string', 'bool']],
    reportToImage: ['string', ['string', 'string']],
    waitSaveOrClose: ['void', ['pointer']]
});
type SaveCallback = (saveTemplate: string, callback: (result: boolean) => void) => void;
export default class ReportApi {

    /**
     *设计模板
     *
     * @param {string} fileName 模板
     * @param {string} tempData 临时演示数据
     * @memberof ReportApi
     */
    static designerReport(template: string, tempData: string, saveCallback: SaveCallback): void {
        callbackPointer = ffi.Callback('void', ['string', 'pointer'], (data: string, func) => {
            if (data) {
                let nativeCallback = ffi.ForeignFunction(func, 'void', ['bool']);
                try {

                    saveCallback(data, (result) => {
                        if (result) {
                            nativeCallback(true);
                        } else {
                            nativeCallback(false);
                        }
                        dll.waitSaveOrClose(callbackPointer);
                    });
                } catch (e) {
                    console.error(e);
                    nativeCallback(false);
                    dll.waitSaveOrClose(callbackPointer);
                }
            }
        });
        dll.designerReport(template, tempData);
        dll.waitSaveOrClose(callbackPointer);
    }
    /**
     *打印预览
     *
     * @param {string} fileName 模板路径
     * @param {string} jsonData 数据
     * @memberof ReportApi
     */
    static reportShowPreview(fileName: string, jsonData: string): void {
        dll.reportShowPreview(fileName, jsonData);
    }
    /**
     *打印
     *
     * @param {string} fileName 模板路径
     * @param {string} jsonData 数据
     * @memberof ReportApi
     */
    static reportPrinter(fileName: string, jsonData: string, isSelectPrinter: boolean): void {
        dll.reportPrinter(fileName, jsonData, isSelectPrinter);
    }

    /**
     * 打印到图片(返回base64图片)
     *
     * @param {string} fileName 模板路径
     * @param {string} jsonData 数据
     * @memberof ReportApi
     */
    static reportToImage(fileName: string, jsonData: string): string {
        return dll.reportToImage(fileName, jsonData);
    }

}
