/**
 * Created by chen on 2017/8/16.
 */
/**
 * Created by zhang on 2017/8/14.
 */

const prf_cons = require('./pdf_base_files/rpt_pdf_consts');
var pdf = require('pdfkit');
var fs = require('fs');
let jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
let DPI = jpcCmnHelper.getScreenDPI()[0];
//let JV = prf_cons.JV;
let JV = require('../rpt_component/jpc_value_define');

module.exports ={
    export_pdf_file:export_pdf_file
}


function export_pdf_file (pageData, paperSize, fName, callback) {
    let offsetX= 10;
    let offsetY=10;
    var doc = new pdf({autoFirstPage: false});
    let newName = '' + (new Date()).valueOf();
    let stream = doc.pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 21) + '/tmp/'+newName+'.pdf'));
    let pageObj = pageData;
    // doc.rect(5,5,1190,890).lineWidth(1).strokeColor('black').stroke();//边框
    let paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
    let size = JV.PAGES_SIZE[paperSizeIdx];

    if (pageObj && pageObj.items.length > 0 ) {
        for(let i=0;i<pageObj.items.length;i++){
            doc.addPage({size:[size[1]*DPI,size[0]*DPI]});
            var page = pageObj.items[i],
                fonts = pageObj[JV.NODE_FONT_COLLECTION],
                styles = pageObj[JV.NODE_STYLE_COLLECTION],
                controls = pageObj[JV.NODE_CONTROL_COLLECTION],
                mergedBand = pageObj[JV.BAND_PROP_MERGE_BAND];

            for (var j = 0; j < page.cells.length; j++) {
                var cell = page.cells[j];
                private_drawCell(cell, fonts, styles, controls, mergedBand);
            }
        }
    }
    doc.end();
    stream.on('finish',function () {
        console.log(newName + ".pdf was written.");
        callback(newName);
    })


    function private_drawCell(cell, fonts, styles, controls, mergedBand) {
        doc.save();
        //doc.translate(0.5,0.5);
        var style = styles[cell[JV.PROP_STYLE]];
        if (style) {
            private_drawLine(cell, doc, style, JV.PROP_TOP, [JV.PROP_LEFT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_TOP], mergedBand, styles);
            private_drawLine(cell, doc, style, JV.PROP_RIGHT, [JV.PROP_RIGHT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_BOTTOM], mergedBand, styles);
            private_drawLine(cell, doc, style, JV.PROP_BOTTOM, [JV.PROP_RIGHT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_BOTTOM], mergedBand, styles);
            private_drawLine(cell, doc, style, JV.PROP_LEFT, [JV.PROP_LEFT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_TOP], mergedBand, styles);
        }
        private_drawCellText(cell, fonts, controls);
        doc.restore();

    }

    function private_drawLine(cell, doc, style, styleBorderDest, startP, destP, mergedBand, styles) {
        //doc.beginPath();
        var destStyle = style;
        if (mergedBand) {
            if (mergedBand[styleBorderDest] == cell[JV.PROP_AREA][styleBorderDest]) {
                destStyle = styles[mergedBand[JV.PROP_STYLE][JV.PROP_ID]];
            }
        }
        doc.moveTo(cell[JV.PROP_AREA][startP[0]] + offsetX, cell[JV.PROP_AREA][startP[1]] + offsetY);
        if (destStyle[styleBorderDest] && destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT] != 0) {
            doc.lineWidth(1.0 * destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]);
            doc.lineTo(cell[JV.PROP_AREA][destP[0]] + offsetX, cell[JV.PROP_AREA][destP[1]] + offsetY);
            doc.strokeColor(destStyle[styleBorderDest][JV.PROP_COLOR]);
        }
        doc.stroke();
    }
    function private_drawCellText(cell, fonts, controls) {
        if (cell[JV.PROP_VALUE]) {
            var values = ("" + cell[JV.PROP_VALUE]).split('|');
            var font = fonts[cell[JV.PROP_FONT]];
            var control = controls[cell[JV.PROP_CONTROL]];
            var height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
            var area = [cell[JV.PROP_AREA][JV.PROP_LEFT] + offsetX, cell[JV.PROP_AREA][JV.PROP_TOP] + offsetY, cell[JV.PROP_AREA][JV.PROP_RIGHT] + offsetX, cell[JV.PROP_AREA][JV.PROP_BOTTOM] + offsetY];
            for (var i = 0; i < values.length; i++) {
                area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP] + i * (height / values.length) + offsetY;
                area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_TOP] + (i + 1) * (height / values.length) + offsetY;
                private_drawText(values[i], area, font, control);
            }
        }
    }

    function private_drawText(val, area, font, control) {
        var dftFontHeight = 12;
        var output = [];
        if (font) {
            dftFontHeight = 1 * font[JV.FONT_PROPS[1]];
            var dftFontBold = font[JV.FONT_PROPS[3]];
            var dftFontItalic = font[JV.FONT_PROPS[4]];
            if (dftFontBold && dftFontBold == 'T') {
                doc.font(__dirname+'/pdf_base_files/hwxsb.ttf');
            }else if(dftFontItalic && dftFontItalic == 'T'){
                doc.font(__dirname+'/pdf_base_files/Smart-italic.ttf');
            }else {
                doc.font(__dirname+'/pdf_base_files/Smart.ttf');
            }
            doc.fontSize(dftFontHeight);
        }
        var options={};
        if (control) {
            private_setupAreaH(area, control.Horizon, font.FontAngle, dftFontHeight, output,options);
            private_setupAreaV(area, control.Vertical, font.FontAngle, dftFontHeight, output);
        } else {
            private_setupAreaH(area, "left", font.FontAngle, dftFontHeight, output,options);
            private_setupAreaV(area, "bottom", font.FontAngle, dftFontHeight, output);
        }
        var w = area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - area[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
        if (font.FontAngle != "0") {
            w = area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] - area[JV.IDX_TOP] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
        }
        doc.save();
        //doc.translate(output[0], output[1]);
        if (w >= doc.widthOfString(val)) {
            options.width=w;
            options.height=dftFontHeight;
        } else {
            while (true) {
                dftFontHeight--;
                doc.fontSize(dftFontHeight);
                if (w >= doc.widthOfString(val) || dftFontHeight < 6) {
                    options.width=w;
                    options.height=dftFontHeight;
                    break;
                }
            }
        }

        var rotateOptions;
        if (font.FontAngle != "0") {
            if (control){
                rotateOptions=private_setupAreaRotateOption(area,w,control.Vertical,dftFontHeight, output);
            }else {
                rotateOptions=private_setupAreaRotateOption(area,w,"bottom",dftFontHeight, output);
            }
            doc.rotate(font.FontAngle,rotateOptions);
        }
        doc.text(val,output[0], output[1],options);
        doc.restore();
    }

    function private_setupAreaH(area, type, fontAngle, dftFontHeight, outputPoint,options) {
        var lType = type;
        if (type != "left" && type != "right" && type != "center") lType = "left";
        options.align=lType;
        outputPoint[0]=1 * area[JV.IDX_LEFT]+ JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
    }

    function private_setupAreaV(area, type, fontAngle, dftFontHeight, outputPoint) {
        var lType = type;
        if (type != "top" && type != "bottom" && type != "center") lType = "top";
        switch (lType) {
            case "top":
                outputPoint[1] = 1 * area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
                break;
            case "bottom":
                outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]-dftFontHeight;
                break;
            case "center":
                outputPoint[1] = 1 * area[JV.IDX_TOP] + (1 * area[JV.IDX_BOTTOM]-1 * area[JV.IDX_TOP]- dftFontHeight) / 2;
                break;
        }
    }

    function private_setupAreaRotateOption(area,w, type="top",dftFontHeight,outputPoint){
        var x = (area[JV.IDX_RIGHT] - area[JV.IDX_LEFT])/2+area[JV.IDX_LEFT];
        var y =(area[JV.IDX_BOTTOM] - area[JV.IDX_TOP])/2+ area[JV.IDX_TOP];
        var rotateOptions = {origin:[x,y]};
        var h = area[JV.IDX_RIGHT] - area[JV.IDX_LEFT];
        outputPoint[0]=x-w/2+JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
        var lType = type;
        switch (lType) {
            case "top":
                outputPoint[1] = y- h/2+ JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
                break;
            case "bottom":
                outputPoint[1] = y+ h/2-JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]-dftFontHeight;
                break;
            case "center":
                outputPoint[1] = y+dftFontHeight/2;
                break;
        }
        return rotateOptions;
    }

}