let model1 = require('../view/model1');
let model2 = require('../view/model2');
let model3 = require('../view/model3');
let model4 = require('../view/model4');
let model5 = require('../view/model5');
let model6 = require('../view/model6');
let _admZip = require('../zip/zipParse.js').admZip2;
let fs = require('fs');
let files = {};
let zipData = "";
let meta = null;
const { app, ipcMain, dialog, BrowserWindow } = require('electron');

function init() {


    for (let i = 0; i < process.argv.length; i++) {
        if (process.argv[i].indexOf('file=') != -1) {
            files.path = process.argv[i].substring(5, process.argv[i].length);
            if (process.platform == 'darwin')
                files.name = files.path.substring(files.path.lastIndexOf('/') + 1, files.path.lastIndexOf('.'))
            else if (process.platform == 'win32')
                files.name = files.path.substring(files.path.lastIndexOf('\\') + 1, files.path.lastIndexOf('.'))
            _admZip(files, zipParseSucc);
            return;
        }
    }
}

function zipParseSucc() {
    parseJSON();
}

function parseJSON() {
    fs.readFile(`${global.tempDir}/tmp/zip/` + files.name + '/meta.json', { flag: 'r+', encoding: 'utf8' }, function(err, data) {
        if (err) {
            console.error(err);
            return;
        }
        meta = JSON.parse(data);
        switch (meta.datas.template.type) {
            case 1:
                model1.showWindow();
                break;
            case 2:
                model2.showWindow();
                break;
            case 3:
                model3.showWindow();
                break;
            case 4:
                model4.showWindow();
                break;
            case 5:
                model5.showWindow();
                break;
            case 6:
                model6.showWindow();
                break;
        }
        zipData = JSON.stringify({ path: `${global.tempDir}/tmp/zip`, name: files.name, meta: data })
    });
}
ipcMain.on('getZipData', function(event, arg) {
    event.returnValue = zipData;
})
console.log("我也启动啦 但是我是新的");
module.exports.zipData = zipData;
module.exports.init = init;