(function(paw, ExcelJS){
    function PawExcel(settings){
        this.settings = paw.lang.append({
            author: null,
            lastAuthor: null,
            filename: 'New spreadsheet',
            addTitleRow: true,
            ignoreBackgrounds: true,
            ignoreBorders: true,
            ignoreFont: false,
            ignoreHiddenColumns: false,
            ignoreColumns: []
        }, settings);

        this.workbook = null;

        this.sheets = {};
        this.worksheet = null;
    }

    
    PawExcel.prepare = function(settings){
        if(paw.utils.value.notSet(settings)){
            settings = {
                
            };
        }
        if (paw.utils.value.notSet(settings.filename)) {
            settings.filename = $(document).prop('title');
            // only include begining of the page title
            if (settings.filename.indexOf('|') > -1) {
                var parts = settings.filename.split('|');
                settings.filename = paw.utils.value.trimString(parts[0]);
            }
        }
        var excel = new paw.mod.excel.Excel(settings);
        excel.prepareWorkbook();
        return excel;
    };

    PawExcel.exportFromHTMLTable = function($table, settings, cellProcessor, rowProcessor){
        var excel = PawExcel.prepare(settings);
        paw.mod.excel.excelCompiler.fromHTMLTable( $table, excel, cellProcessor, rowProcessor);
        return paw.mod.excel.excelCompiler.generateXlsxFile(excel);
    };

    PawExcel.prepareExporter = function ($table, features, settings, onExport, cellProcessor, rowProcessor) {
        var $parent = $table.parent();
        if (paw.utils.value.isSet(features)) {
            this.features = features;
        }
        if (paw.utils.value.notSet(features) || features.length === 0) {
            return;
        }

        var $exportFeatures = $('<div></div>');
        $parent.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 () {
                if (paw.utils.value.isFunction(onExport)) {
                    onExport($table, settings, cellProcessor, rowProcessor);
                    return;
                }
                PawExcel.exportFromHTMLTable($table, settings, cellProcessor, rowProcessor);
            });
        }
    };

    PawExcel.prototype.prepareWorkbook = function(){
        this.workbook = new ExcelJS.Workbook();
        this.workbook.creator = this.settings.author;
        this.workbook.lastModifiedBy = (paw.utils.value.isSet(this.settings.lastAuthor)? this.settings.lastAuthor :  this.settings.author);
        this.workbook.created = paw.Datetime.now().toDate();
        this.workbook.modified = null;
        this.workbook.lastPrinted = null;
        this.workbook.properties.date1904 = true;
        this.workbook.calcProperties.fullCalcOnLoad = true;
        this.workbook.views = [
            {
                x: 0, y: 0, width: 10000, height: 20000,
                firstSheet: 0, activeTab: 1, visibility: 'visible'
            }
        ];
        
        this.worksheet = this.addWorksheet("Sheet 1", {
            firstHeader: "Sheet 1",
            firstFooter: "Sheet 1"
        });
        return this;
    };

    PawExcel.prototype.addWorksheet = function(name, settings){
        if(paw.utils.value.isSet(this.sheets[name])){
            throw "PawExcel: worksheet with name " + name + " already exists!";
        }
        var sheet = this.workbook.addWorksheet(name, {
            headerFooter: paw.lang.append(
                {
                    firstHeader: "Sheet", 
                    firstFooter: "Sheet"
                }, settings)
        });
        this.sheets[name] = sheet;
        return sheet;
    };

    paw.mod.excel.Excel = PawExcel;
})(paw, ExcelJS);