/**
 * Created by Tony on 2017/3/31.
 */
var test = require('tape');
var JSZip = require("jszip");

var fs = require('fs');

test('test zip excel files: ', function (t) {
    //rptUtil.setReportDefaultCache();
    var file = '[Content_Types].xml';
    var data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/[Content_Types].xml', 'utf8', 'r');
    var zip = new JSZip();
    zip.file(file, data, {compression: 'DEFLATE'});

    var zip_rels = zip.folder('_rels');
    file = '.rels';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/_rels/.rels', 'utf8', 'r');
    zip_rels.file(file, data, {compression: 'DEFLATE'});

    var zip_docProps = zip.folder('docProps');
    file = 'app.xml';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/docProps/app.xml', 'utf8', 'r');
    zip_docProps.file(file, data, {compression: 'DEFLATE'});
    file = 'core.xml';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/docProps/core.xml', 'utf8', 'r');
    zip_docProps.file(file, data, {compression: 'DEFLATE'});

    var zip_xl = zip.folder('xl');
    file = 'sharedStrings.xml';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/xl/sharedStrings.xml', 'utf8', 'r');
    zip_xl.file(file, data, {compression: 'DEFLATE'});
    file = 'styles.xml';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/xl/styles.xml', 'utf8', 'r');
    zip_xl.file(file, data, {compression: 'DEFLATE'});
    file = 'workbook.xml';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/xl/workbook.xml', 'utf8', 'r');
    zip_xl.file(file, data, {compression: 'DEFLATE'});

    var zip_rels2 = zip_xl.folder('_rels');
    file = 'workbook.xml.rels';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/xl/_rels/workbook.xml.rels', 'utf8', 'r');
    zip_rels2.file(file, data, {compression: 'DEFLATE'});
    var zip_theme = zip_xl.folder('theme');
    file = 'theme1.xml';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/xl/theme/theme1.xml', 'utf8', 'r');
    zip_theme.file(file, data, {compression: 'DEFLATE'});

    var zip_worksheets = zip_xl.folder('worksheets');
    file = 'sheet1.xml';
    data = fs.readFileSync('../../../tmp/excel_test_raw_data/08-2/xl/worksheets/sheet1.xml', 'utf8', 'r');
    zip_worksheets.file(file, data, {compression: 'DEFLATE'});
    //sheet1.xml

    zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
        .pipe(fs.createWriteStream('../../../tmp/outExcel.xlsx'))
        .on('finish', function () {
            // JSZip generates a readable stream with a "end" event,
            // but is piped here in a writable stream which emits a "finish" event.
            console.log("outExcel.xlsx was written.");
            t.pass('just pass for excel(xlsx) output!');
            t.end();
        }
    );
});
