(function () {
    'use strict';
    angular
        .module('app')
        .controller('pdDrawingFlexPieSvgCtrl', function ($scope, dataService) {
            $scope.title = 'Drawing FlexPie as a SVG image';

            $scope.export = function () {
                var doc = new wijmo.pdf.PdfDocument({
                    ended: function (sender, args) {
                        wijmo.pdf.saveBlob(args.blob, 'FlexGrid.pdf');
                    }
                }),
                flexPie = new wijmo.chart.FlexPie('#pie'),
                renderingImageFlag = false;

                try {
                    flexPie.initialize({
                        itemsSource: dataService.getExpenseTotals(dataService.getExpenseItems()),
                        binding: 'value',
                        bindingName: 'name',
                        innerRadius: 0.75,
                        dataLabel: {
                            content: '{value:c1}',
                            position: wijmo.chart.PieLabelPosition.Inside
                        },
                        rendered: function (sender, args) {
                            if (!renderingImageFlag) {
                                renderingImageFlag = true;

                                try {
                                    sender.saveImageToDataUrl(wijmo.chart.ImageFormat.Svg, function (url) {
                                        flexPie.dispose();

                                        doc.drawText('Total expenses by category:');
                                        doc.drawSvg(url);

                                        doc.end();
                                    });
                                } catch (ex) {
                                    flexPie.dispose();
                                }
                            }
                        }
                    });
                } catch (ex) {
                    flexPie.dispose();
                }
            }
        });
})();