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 openZip = require('../view/openZip');
let _admZip = require('../zip/zipParse.js').admZip2;
let fs = require('fs');
let files = {};
let zipData = "";
let meta = null;
let command = false;
let zipSender = null;
let pid = "";
const { app, ipcMain, dialog, BrowserWindow } = require('electron');

function init() {

    console.log("传入的值", process.argv.toString())
    for (let i = 0; i < process.argv.length; i++) {
        if (process.argv[i].indexOf('file=') != -1) {
            command = true;
            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'){
				if(files.path.indexOf("\\")>1){
					files.name = files.path.substring(files.path.lastIndexOf('\\') + 1, files.path.lastIndexOf('.'));
				}else{
					files.name = files.path.substring(files.path.lastIndexOf('/') + 1, files.path.lastIndexOf('.'));
				}
				
			}
            _admZip(files, zipParseSucc);
            return;
        }
    }
	pid = process.pid;
    if (!command) {
		console.log("激活窗口");
        openZip.showWindow();
    }
}

function zipParseSucc(data) {
    // ipcMain.s
    // if (zipSender) {
    //     zipSender.send('zipSuccess');
    // }
	console.log('dajshdkajhsd-->'+data);
	openZip.openErrorWindow(data);
	if(data == 0){
		openZip.closeWindow();
		parseJSON();
	}else{
		
	}
    
}

function parseJSON() {
	//console.log("开始播放地址---->"+files.name);
    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:
				console.log("dangqian de dizhi shi --〉");
                model5.showWindow();
                break;
            case 6:
                model6.showWindow();
                break;
        }
        zipData = JSON.stringify({ path: `${global.tempDir}/tmp/zip`, name: files.name, meta: data,pid:pid })
    });
}
ipcMain.on('getZipData', function(event, arg) {
    event.returnValue = zipData;
})
ipcMain.on('openZip_filePath', function(e, arg) {
    files.path = arg;	
    zipSender = e.sender;
    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('.'));
	//console.log("地址是---->"+files.name);
    _admZip(files, zipParseSucc);
})
module.exports.zipData = zipData;
module.exports.init = init;