'use strict';

angular.module('app').controller('purchaseSlipCtrl', function ($scope, dataService, exportService) {
    var getLayoutDefTimer = null,
        getSummaryFieldsTimer = null;
    $scope.ctx = {
        multiRow: null,
        footer: null,
        data: dataService.getPurchaseSlip()
    }

    $scope.$watch('ctx.multiRow', function () {
        var multiRow = $scope.ctx.multiRow;
        if (multiRow) {
            var cv = multiRow.collectionView;
            cv.collectionChanged.addHandler(function (sender, e) {
                var quantity, unitCost;
                if (e.action === wijmo.collections.NotifyCollectionChangedAction.Change && !!e.item) {
                    quantity = +e.item.quantity;
                    unitCost = +e.item.unitCost;
                    if (!isNaN(quantity) && !isNaN(unitCost)) {
                        e.item.cost = quantity * unitCost;
                        e.item.price = e.item.cost * 1.35;
                        updateSummary(cv);
                    }
                }
            });

            multiRow.scrollPositionChanged.addHandler(function () {
                var footer = $scope.ctx.footer;
                if (footer && footer.scrollPosition.x !== multiRow.scrollPosition.x) {
                    footer.scrollPosition = new wijmo.Point(multiRow.scrollPosition.x, 0);
                }
            });

            multiRow._root.style.overflowX = 'hidden';
            getLayoutDef();
        }
    });

    $scope.$watch('fields', function () {
        var multiRow = $scope.ctx.multiRow,
            footer = $scope.ctx.footer;
        if (multiRow) {
            getLayoutDef();
        }
        if (footer && footer.rows.length > 0 && footer.columns.length > 0 && multiRow) {
            footer.setCellData(0, 3, $scope.$parent.fields.summary);
            footer.setCellData(0, 6, $scope.$parent.fields.amountSummary);
            updateSummary(multiRow.collectionView);
        }
    });

    $scope.$watch('ctx.footer', function () {
        var footer = $scope.ctx.footer,
            multiRow = $scope.ctx.multiRow;
        if (footer && multiRow) {
            getSummaryFields();
            footer.scrollPositionChanged.addHandler(function () {
                var multiRow = $scope.ctx.multiRow;
                if (multiRow && multiRow.scrollPosition.x !== footer.scrollPosition.x) {
                    multiRow.scrollPosition = new wijmo.Point(footer.scrollPosition.x, multiRow.scrollPosition.y);
                }
            })
        }
    });

    // Export the records of current page to xlsx file.
    $scope.exportToExcel = function () {
        var mainWorkbook = wijmo.grid.xlsx.FlexGridXlsxConverter.save($scope.ctx.multiRow),
            footerWOrkbook = wijmo.grid.xlsx.FlexGridXlsxConverter.save($scope.ctx.footer);

        mainWorkbook.sheets[0].rows.push(footerWOrkbook.sheets[0].rows[0]);
        mainWorkbook.save('PurchaseSlip.xlsx');
    }

    // Save the records of current page to PDF file.
    $scope.exportToPDF = function () {
        var isJapanese = $scope.$parent.culture === 'ja';

        try {
            mergeFooter();
            exportService.exportPdf($scope.ctx.multiRow, 'PurchaseSlip.pdf', isJapanese, null);
        } finally {
            $scope.ctx.multiRow.rows.pop();
        }
    }

    function getLayoutDef() {
        if ($scope.$parent.fields) {
            $scope.ctx.multiRow.layoutDefinition = generateLayoutDef();
        } else {
            clearTimeout(getLayoutDefTimer);
            getLayoutDefTimer = null;
            getLayoutDefTimer = setTimeout(function () {
                getLayoutDef();
            }, 100);
        }
    }

    function getSummaryFields() {
        var footer = $scope.ctx.footer,
            multiRow = $scope.ctx.multiRow,
            cv = multiRow.collectionView;
        if ($scope.$parent.fields) {
            footer.columnLayout = multiRow.columnLayout;
            for (var i = 0; i < footer.columns.length; i++) {
                footer.columns[i].isReadOnly = true;
                footer.columns[i].cssClass = 'summary-footer';
                if (i === 3 || i === 6) {
                    footer.columns[i].cssClass += ' summary-footer-title';
                }
            }
            footer.columns[4].align = 'right';
            footer.rows.push(new wijmo.grid.Row());
            footer.columnHeaders.rows.clear();
            footer.setCellData(0, 3, $scope.$parent.fields.summary);
            footer.setCellData(0, 6, $scope.$parent.fields.amountSummary);
            updateSummary(cv);
        } else {
            clearTimeout(getSummaryFieldsTimer);
            getSummaryFieldsTimer = null;
            getSummaryFieldsTimer = setTimeout(function () {
                getSummaryFields();
            }, 100);
        }
    }

    // Generate the layout definition for the MultiRow control.
    function generateLayoutDef() {
        var caseDataMap = $scope.$parent.buildDataMap($scope.$parent.fields.cases.split(','));

        return [
            {
                cells: [
                    { binding: 'productName', header: $scope.$parent.fields.purchaseSlipFields.productName, align: 'center', width: 200 }
                ]
            },
            {
                cells: [
                    { binding: 'productId', header: $scope.$parent.fields.purchaseSlipFields.productId, width: 90 }
                ]
            },
            {
                cells: [
                    { binding: 'color', header: $scope.$parent.fields.purchaseSlipFields.color, align: 'center' },
                    { binding: 'packageUnit', header: $scope.$parent.fields.purchaseSlipFields.packageUnit, align: 'center' }
                ]
            },
            {
                cells: [
                    { binding: 'size', header: $scope.$parent.fields.purchaseSlipFields.size, width: 80 },
                    { binding: 'case', header: $scope.$parent.fields.purchaseSlipFields.case, dataMap: caseDataMap, width: 80 }
                ]
            },
            {
                cells: [
                    { binding: 'quantity', header: $scope.$parent.fields.purchaseSlipFields.quantity, width: 65 }
                ]
            },
            {
                cells: [
                    { binding: 'deal', header: $scope.$parent.fields.purchaseSlipFields.deal, width: 60 },
                    { binding: 'id', header: $scope.$parent.fields.purchaseSlipFields.id, width: 60 }
                ]
            },
            {
                cells: [
                    { binding: 'unitCost', header: $scope.$parent.fields.purchaseSlipFields.unitCost, width: 130, format: 'c2' }
                ]
            },
            {
                cells: [
                    { binding: 'cost', header: $scope.$parent.fields.purchaseSlipFields.cost, width: 90, format: 'c2', isReadOnly: true }
                ]
            },
            {
                cells: [
                    { binding: 'price', header: $scope.$parent.fields.purchaseSlipFields.price, width: 90, format: 'c2', isReadOnly: true }
                ]
            },
            {
                cells: [
                    { binding: 'remarks', header: $scope.$parent.fields.purchaseSlipFields.remarks, align: 'center' }
                ]
            }
        ];
    }

    // Update the summary info for the MultiRow control.
    function updateSummary(cv) {
        var footer = $scope.ctx.footer,
            qtySum = wijmo.getAggregate(wijmo.Aggregate.Sum, cv.items, 'quantity'),
            costSum = wijmo.getAggregate(wijmo.Aggregate.Sum, cv.items, 'cost'),
            priceSum = wijmo.getAggregate(wijmo.Aggregate.Sum, cv.items, 'price');

        footer.setCellData(0, 4, qtySum);
        footer.setCellData(0, 7, wijmo.Globalize.format(costSum, 'c2'));
        footer.setCellData(0, 8, wijmo.Globalize.format(priceSum, 'c2'));

    }

    // Merge the footer to the multiRow control for exporting pdf.
    function mergeFooter() {
        var multiRow = $scope.ctx.multiRow,
            footer = $scope.ctx.footer,
            rowCnt = multiRow.rows.length,
            newRow = new wijmo.grid.GroupRow(); // use GroupRow since this is unbound (one row per record)

        newRow.recordIndex = 0;
        multiRow.rows.push(newRow);
        multiRow.setCellData(rowCnt, 3, footer.getCellData(0, 3));
        multiRow.setCellData(rowCnt, 4, footer.getCellData(0, 4));
        multiRow.setCellData(rowCnt, 6, footer.getCellData(0, 6), false);
        multiRow.setCellData(rowCnt, 7, footer.getCellData(0, 7));
        multiRow.setCellData(rowCnt, 8, footer.getCellData(0, 8));
    }
});