(function (paw) {
    function CalculateVacationsTableNormalization($root) {
        this.$root = $root;

        this.$ntable;
        this.$tableHeader;
        this.headerValues = [];
        this.$tableBody;
    }

    CalculateVacationsTableNormalization.prototype.normalizeTable = function () {
        this.createNormalizedTable();
        this.populateNormalizedTable();
        return this.$ntable;
    };

    CalculateVacationsTableNormalization.prototype.createNormalizedTable = function () {
        this.$ntable = $('<table style="width: 100%;"></table>');
        this.createHeader();
        this.$tableBody = $('<tbody></tbody>');
        this.$ntable.append(this.$tableBody);
    };

    CalculateVacationsTableNormalization.prototype.createHeader = function () {
        var self = this;
        this.$tableHeader = $('<thead></thead>');
        var $headerRow = $('<tr></tr>');
        this.headerValues = ['']; // add first empty value
        $headerRow.append('<th></th>');

        var $employeeHeaderCells = $($(this.$root.find('table.vacation-table').get(0)).find('tbody tr').get(0)).find('td');
        $employeeHeaderCells.each(function (celli) {
            // skip first cell (name) and last two cells (total calcs)
            if (celli === 0 || celli >= $employeeHeaderCells.length - 2) {
                return;
            }
            var $cell = $(this);
            var value = paw.utils.value.trimString($cell.find('i').attr('title'));
            self.headerValues.push(value);
            $headerRow.append('<th>' + value + '</th>');
        });


        var $headerCells = $($(this.$root.find('table.worker-vacations-grid').get(0)).find('thead tr').get(0)).find('th');

        $headerCells.each(function (celli) {
            var $cell = $(this);
            var value = paw.utils.value.trimString($cell.text());
            self.headerValues.push(value);
            $headerRow.append('<th>' + value + '</th>');
        });

        this.$tableHeader.append($headerRow);
        this.$ntable.append(this.$tableHeader);
    };

    CalculateVacationsTableNormalization.prototype.populateNormalizedTable = function () {
        var self = this;

        this.$root.children('.panel').each(function () {
            var $panel = $(this);
            var employeeValues = self.parseEmployeeValues($panel);
            
            $panel.find('table.worker-vacations-grid tbody tr').each(function (rowi) {
                var $row = $(this);
                var $nrow = $('<tr></tr>');

                for (var i = 0; i < employeeValues.length; i++) {
                    var employeeValue = employeeValues[i];
                    $nrow.append('<td>' + employeeValue + '</td>');
                }

                $row.children('td').each(function (celli) {
                    var $cell = $(this);
                    var value = self.parseCellValue($cell, celli);
                    $nrow.append('<td>' + value + '</td>');
                });

                self.$tableBody.append($nrow);
            });
        });
    };

    CalculateVacationsTableNormalization.prototype.parseEmployeeValues = function ($panel) {
        var employeeValues = [];
        var $employeeCells = $panel.find('table.vacation-table tbody tr td');
        $employeeCells.each(function (celli) {
            if (celli >= $employeeCells.length - 2) {
                return;
            }
            var $cell = $(this);
            var value;
            if (celli === 0) {
                value = paw.utils.value.trimString($cell.find('.vacation-worker-name a').text());
            }
            else {
                value = paw.utils.value.trimString($cell.find('a').text());
            }
            employeeValues.push(value);
        });
        return employeeValues;
    };

    CalculateVacationsTableNormalization.prototype.parseCellValue = function ($cell, celli) {
        switch (celli) {
            case 0: {
                return paw.utils.value.trimString($cell.children('a').text());
            }
            default:
                return paw.utils.value.trimString($cell.text());
        }
    };

    function CalculateVacationsExporter($root, features) {
        this.$root = $root;
        this.features = features;
        this.ntable = new CalculateVacationsTableNormalization(this.$root);
    }

    CalculateVacationsExporter.create = function ($root, features) {
        var exporter = new CalculateVacationsExporter($root, features);
        exporter.init();
    };

    CalculateVacationsExporter.prototype.init = function (features) {
        var self = this;
        if (paw.utils.value.isSet(features)) {
            this.features = features;
        }
        if (paw.utils.value.notSet(this.features) || this.features.length === 0) {
            return;
        }

        var $exportFeatures = $('<div></div>');
        this.$root.prepend($exportFeatures);

        if (this.features.indexOf('excel') > -1) {
            var $exportBtn = $('<button style="padding: .5em 1em; margin-right: .167em; margin-bottom: .333em; margin-top: .333em">Excel</button>');
            $exportFeatures.append($exportBtn);
            $exportBtn.on('click', function () {
                self.exportExcel();
            });
        }
    };

    CalculateVacationsExporter.prototype.compileNormalizedTable = function () {
        return this.ntable.normalizeTable();
    };

    CalculateVacationsExporter.prototype.exportExcel = function () {
        if (paw.utils.value.notSet(this.ntable.$ntable)) {
            this.compileNormalizedTable();
        }
        paw.mod.excel.Excel.exportFromHTMLTable(this.ntable.$ntable);
    };

    paw.view.workhours.CalculateVacationsExporter = CalculateVacationsExporter;
})(paw);