(function () {
    'use strict';
    angular
        .module('app')
        .controller('fcDrawingCellsManuallyCtrl', function ($scope, dataService) {
            $scope.title = 'Drawing cells manually';
            $scope.ctx = {
                grid: null,
                data: dataService.getData(5)
            };

            $scope.export = function () {
                window.setTimeout(function () {
                    wijmo.grid.pdf.FlexGridPdfConverter.export($scope.ctx.grid, 'FlexGrid.pdf', {
                        maxPages: 10,
                        documentOptions: {
                            header: {
                                declarative: {
                                    text: '\t&[Page]\\&[Pages]'
                                }
                            },
                            footer: {
                                declarative: {
                                    text: '\t&[Page]\\&[Pages]'
                                }
                            },
                            info: {
                                author: 'C1',
                                title: 'PdfDocument sample',
                                keywords: 'PDF, C1, sample',
                                subject: 'PdfDocument'
                            }
                        },
                        customCellContent: true,
                        formatItem: function (args) {
                            if (args.panel.cellType === wijmo.grid.CellType.Cell) {
                                if (args.panel.columns[args.col].binding === 'country') {
                                    var r = args.contentRect,
                                        sz = args.canvas.measureText(args.data, args.style.font, {
                                            height: r.height,
                                            width: r.width
                                        }),
                                        image = args.canvas.openImage('resources/' + args.data + '.png'),
                                        imageTop = r.top + (r.height - image.height) / 2,
                                        textTop = r.top + (r.height - sz.size.height) / 2;

                                    // draw flag image
                                    args.canvas.drawImage(image, r.left, imageTop);

                                    // draw text
                                    args.canvas.drawText(args.data, r.left + image.width + 3, textTop, {
                                        brush: args.style.color,
                                        font: args.style.font,
                                        height: r.height,
                                        width: r.width
                                    });

                                    // cancel standard cell content drawing
                                    args.cancel = true;
                                }
                            }
                        }
                    });
                });
            };
        });
})();