;
(function () {
    const
        app_cache = require('app-cache'),
        myStorage = require('local-storage'),
        apiService = require('app-service'),
        gui = require('nw.gui'),
        csIdentify = require('cs-identify'),
        Promise  = require("bluebird"),
        EventEmitter = require('myevents'),
        broadcaster = require('win-transfer-data'),
        exec = require('child_process').exec;

    exec(`Online.exe /s2 /c /q`, function(error, stdout, stderr){
        if(error) {
            console.error('error: ' + error);
            return;
        }
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + typeof stderr);
    });

        let win = nw.Window.get();
        let paramOption = {};



    // 给空白窗口添加提示信息
    // win.on('loaded',function () {
    //     var	element	=	document.createElement('div');
    //     element.setAttribute('class','loading');
    //     element.appendChild(document.createTextNode('正在登陆，请稍等……'));
    //     document.body.appendChild(element);
    // })


    // 接收loading传递的事件
    broadcaster.subscribe('autoLogin',function (data) {
        broadcaster.clear('autoLogin');
        autoLogin(data);
    });

    function autoLogin(authLoginParam) {
        let csIdentifyOptions,  userInfo = {};

        getParam(authLoginParam).then(function (data) {
            csIdentifyOptions = data;
            if (!csIdentifyOptions.access_token){
                EventEmitter.emit('authlogin_reject','登录失败，请勿多次点击登录链接');
                return;
            }
            csIdentify.getUserInfo(csIdentifyOptions.access_token).then(function (data) {
                console.log(data);
                if(!data || data.ret ===0){
                    EventEmitter.emit('authlogin_reject','登录失败，单点未能获取用户信息！');
                    return;
                }
                EventEmitter.emit('auth_login',data.user);
            });
        })
    }

    function getParam(tempLink){
        return   Q((resolve,reject) =>{
            nw.Window.open(tempLink,{
                frame:false,
                show:false
            },(new_win) =>{
                let paramArr = tempLink.split('&');
                paramArr.forEach(function (value, index, array1) {
                    let tempArr = value.split('=');
                    paramOption [tempArr[0]]= tempArr[1];
                })
                //截取新窗口链接
                let tempLink2 = new_win.window.location.href.split('http');
                if(!tempLink2 && tempLink2.length < 2){
                        reject(false);
                    }
                    let paramArr2 = tempLink2[1].split('&');
                    paramArr2.forEach(function (value, index, array1) {
                        let tempArr = value.split('=');
                        paramOption [tempArr[0]]= tempArr[1];
                    })
                    resolve(paramOption);
            })
        })
    }

    EventEmitter.on('authlogin_reject', function (msg) {
        // console.log(msg);
        nw.Window.open("./web_content/login.html", {
            frame: false,
            width: 680,
            height: 400,
            show: false
        }, (new_win) => {
            new_win.on('loaded', () => {
                win.close();
                new_win.show(true);
                broadcaster.publish('authlogin_reject',msg);

            });
        });

    });

    EventEmitter.on('auth_login', function (data) {
        data.authorization = csIdentify.getAuthorization(data.loginName,data.password);
        openWin(data);
        // setTimeout(() => {
            // console.log('登录成功');
            // openWin(data);
        // }, 1000);
    });

    function openWin(d) {
        //  app_cache 存储当前全局信息
        app_cache.cur_user = d;

        apiService.use_space().then(e => {
            app_cache.use_space = e.data;
        }).then(e => {
            nw.Window.open("./web_content/main.html", {
                frame: false,
                width: 950,
                height: 600,
                show: false
            }, (new_win) => {
                new_win.on('loaded', () => {
                    win.close();
                    new_win.show(true);
                });
            });
        })
    }

    function Q(caller) {
        let promise = new Promise (function (resolve, reject) {
            caller(resolve, reject);
        });
        return promise;
    }

}())

