/**
 * Created by chen on 2017/7/27.
 */
let can = require('canvas');
let canvas = new can(1200, 900,'pdf'), fs = require('fs');
let reportO = require('./reportOj');
//,ctx = canvas.getContext('2d')
/*var y = 80, x = 50;
ctx.font = '22px Helvetica';
ctx.fillText('node-canvas pdf', x, y);
ctx.font = '10px Arial';
ctx.fillText('Just a quick example of PDFs with node-canvas lonso', x, y += 20);
ctx.globalAlpha = .5;
ctx.fillRect(x, y += 20, 10, 10);
ctx.fillRect(x += 20, y, 10, 10);
ctx.fillRect(x += 20, y, 10, 10);*/
//fs.writeFile('out.pdf', canvas.toBuffer());



let JpcCanvasOutput = {
    offsetX: 10,
    offsetY: 10,
    cleanCanvas: function (canvas) {
        var ctx = canvas.getContext("2d");
        ctx.save();
        ctx.fillStyle="white";
        ctx.clearRect(0,0, canvas.width, canvas.height);
        ctx.restore();
    },
    drawToCanvas : function(pageObj, canvas, pageIdx) {
        var me = this;
        var ctx = canvas.getContext("2d");

        function private_setupAreaH(area, type, fontAngle, dftFontHeight, outputPoint) {
            var lType = type;
            if (type != "left" && type != "right" && type != "center") lType = "left";
            switch (lType) {
                case "left":
                    if (fontAngle == JV.VERTICAL_ANGLE) {
                        outputPoint[1] = 1 * area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
                    } else if (fontAngle == JV.ANTI_VERTICAL_ANGLE) {
                        outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
                    } else outputPoint[0] = 1 * area[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
                    ctx.textAlign="start";
                    break;
                case "right":
                    if (fontAngle == JV.VERTICAL_ANGLE) {
                        outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
                    } else if (fontAngle == JV.ANTI_VERTICAL_ANGLE) {
                        outputPoint[1] = 1 * area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
                    } else outputPoint[0] = 1 * area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
                    ctx.textAlign="end";
                    break;
                case "center":
                    if (fontAngle == JV.VERTICAL_ANGLE || fontAngle == JV.ANTI_VERTICAL_ANGLE) {
                        outputPoint[1] = (1 * area[JV.IDX_TOP] + 1 * area[JV.IDX_BOTTOM]) / 2;
                    } else outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT]) / 2;
                    ctx.textAlign="center";
                    break;
            }
        }
        function private_setupAreaV(area, type, fontAngle, dftFontHeight, outputPoint) {
            var lType = type;
            if (type != "top" && type != "bottom" && type != "center") lType = "top";
            switch (lType) {
                case "top":
                    if (fontAngle == JV.VERTICAL_ANGLE) {
                        outputPoint[0] = 1 * area[JV.IDX_RIGHT] - dftFontHeight - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
                    } else if (fontAngle == JV.ANTI_VERTICAL_ANGLE) {
                        outputPoint[0] = 1 * area[JV.IDX_LEFT] + dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
                    } else outputPoint[1] = 1 * area[JV.IDX_TOP] + dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
                    break;
                case "bottom":
                    if (fontAngle == JV.VERTICAL_ANGLE) {
                        outputPoint[0] = 1 * area[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
                    } else if (fontAngle == JV.ANTI_VERTICAL_ANGLE) {
                        outputPoint[0] = 1 * area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
                    } else outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
                    break;
                case "center":
                    if (fontAngle == JV.VERTICAL_ANGLE) {
                        outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT] - dftFontHeight) / 2;
                    } else if (fontAngle == JV.ANTI_VERTICAL_ANGLE) {
                        outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT] + dftFontHeight) / 2;
                    } else outputPoint[1] = (1 * area[JV.IDX_TOP] + 1 * area[JV.IDX_BOTTOM] + dftFontHeight) / 2;
                    break;
            }
        }
        function private_drawText(val, area, font, control) {
            var dftFontHeight = 12;
            var output = [];
            if (font) {
                dftFontHeight = 1 * font[JV.FONT_PROPS[1]];
                var dftOthers = "";
                var dftFontBold = font[JV.FONT_PROPS[3]];
                if (dftFontBold && dftFontBold == 'T') {
                    dftOthers = "bold " + dftOthers ;
                }
                var dftFontItalic = font[JV.FONT_PROPS[4]];
                if (dftFontItalic && dftFontItalic == 'T') {
                    dftOthers = dftOthers + "italic ";
                }
                ctx.font = dftOthers + dftFontHeight + "px " + font[JV.PROP_NAME];
            }
            if (control) {
                private_setupAreaH(area, control.Horizon, font.FontAngle, dftFontHeight, output);
                private_setupAreaV(area, control.Vertical, font.FontAngle, dftFontHeight, output);
            } else {
                private_setupAreaH(area, "left", font.FontAngle, dftFontHeight, output);
                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];
            }
            ctx.save();
            ctx.translate(output[0], output[1]);
            if (font.FontAngle == JV.VERTICAL_ANGLE) {
                ctx.rotate(Math.PI/2);
            } else if (font.FontAngle == JV.ANTI_VERTICAL_ANGLE) {
                ctx.rotate(-Math.PI/2);
            }
            if (w >= ctx.measureText(val).width) {
                ctx.fillText(val, 0, 0);
            } else {
                while (true) {
                    dftFontHeight--;
                    ctx.font = "" + dftFontHeight + "px " + font[JV.PROP_NAME];
                    if (w >=  ctx.measureText(val).width || dftFontHeight < 6) {
                        ctx.fillText(val, 0, 0);
                        break;
                    }
                }
            }
            ctx.restore();
        }
        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] + me.offsetX, cell[JV.PROP_AREA][JV.PROP_TOP] + me.offsetY, cell[JV.PROP_AREA][JV.PROP_RIGHT] + me.offsetX, cell[JV.PROP_AREA][JV.PROP_BOTTOM] + me.offsetY];
                for (var i = 0; i < values.length; i++) {
                    area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP] + i * (height / values.length) + me.offsetY;
                    area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_TOP] + (i + 1) * (height / values.length) + me.offsetY;
                    private_drawText(values[i], area, font, control);
                }
            }
        }
        function private_drawLine(cell, ctx, style, styleBorderDest, startP, destP, mergedBand, styles) {
            ctx.beginPath();
            var destStyle = style;
            if (mergedBand) {
                if (mergedBand[styleBorderDest] == cell[JV.PROP_AREA][styleBorderDest]) {
                    destStyle = styles[mergedBand[JV.PROP_STYLE][JV.PROP_ID]];
                }
            }
            ctx.moveTo(cell[JV.PROP_AREA][startP[0]] + me.offsetX, cell[JV.PROP_AREA][startP[1]] + me.offsetY);
            if (destStyle[styleBorderDest] && destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT] != 0) {
                ctx.lineWidth = 1.0 * destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT];
                ctx.strokeStyle = destStyle[styleBorderDest][JV.PROP_COLOR];
                ctx.lineTo(cell[JV.PROP_AREA][destP[0]] + me.offsetX, cell[JV.PROP_AREA][destP[1]] + me.offsetY);
            }
            ctx.stroke();
        }
        function private_drawCell(cell, fonts, styles, controls, mergedBand) {
            ctx.save();
            ctx.translate(0.5,0.5);
            var style = styles[cell[JV.PROP_STYLE]];
            if (style) {
                private_drawLine(cell, ctx, style, JV.PROP_TOP, [JV.PROP_LEFT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_TOP], mergedBand, styles);
                private_drawLine(cell, ctx, style, JV.PROP_RIGHT, [JV.PROP_RIGHT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_BOTTOM], mergedBand, styles);
                private_drawLine(cell, ctx, style, JV.PROP_BOTTOM, [JV.PROP_RIGHT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_BOTTOM], mergedBand, styles);
                private_drawLine(cell, ctx, style, JV.PROP_LEFT, [JV.PROP_LEFT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_TOP], mergedBand, styles);
            }
            private_drawCellText(cell, fonts, controls);
            ctx.restore();
        }

        for(var p=1;p<50;p++){
            pageIdx=p;
            if (pageObj && pageObj.items.length > 0 && canvas && pageObj.items.length >= pageIdx) {
                var page = pageObj.items[pageIdx - 1],
                    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);
                }
            }
            ctx.addPage();
        }

        fs.writeFile('report.pdf', canvas.toBuffer());
    },
    drawPageBorder: function(rptTpl, canvas, resolution) {
        var me = this;
        var size = rptTpl[JV.NODE_PAGE_INFO].slice(0);
        size[0] = Math.round(resolution[0] * size[0]);
        size[1] = Math.round(resolution[0] * size[1]);

        var ctx = canvas.getContext("2d");
        ctx.save();
        ctx.beginPath();
        ctx.translate(0.5,0.5);
        ctx.lineWidth = 1;
        ctx.moveTo(me.offsetX, me.offsetY);
        ctx.lineTo(size[0] + me.offsetX, me.offsetY);
        ctx.lineTo(size[0] + me.offsetX, size[1] + me.offsetY);
        ctx.lineTo(me.offsetX, size[1] + me.offsetY);
        ctx.lineTo(me.offsetX, me.offsetY);
        ctx.stroke();
        ctx.restore();

        ctx.fillStyle="black";
        ctx.fillRect(size[0] + me.offsetX,10 + me.offsetY,10,size[1]);
        ctx.fillRect(10 + me.offsetX,size[1] + me.offsetY,size[0],10);
    }
}


let rpt_oj = reportO.reportObj;



JpcCanvasOutput.drawToCanvas(rpt_oj,canvas,1);