'use strict';

var sysconfig = require('../../lib/config.js').sys;
var config = require('../../lib/config.js').user;
var EventProxy = require('eventproxy');
var request = require('request');
var ipc = require('ipc');

var ep = new EventProxy();

// check user is login.
ep.tail('is-login', function() {
    var options = {
        url: sysconfig.getApi('islogin'),
        headers: {
            user: config.get('user:name'),
            token: config.get('user:token'),
        }
    }

    // send request
    request.get(options, function(err, response, body) {
        if (err) {
            if (confirm('\t' + err + '\n\tCan not connet,click [OK] try again.Or [Cancel] quit app.\n')) {
                ep.emit('is-login');
            } else {
                ipc.send('app-quit');
            };
        } else {
            var res = JSON.parse(body);
            if (res.data) {
                ipc.send('login-success');
            } else {
                ep.emit('login');
            };
        };
    });
});

ep.tail('login', function() {
    var options = {
        url: sysconfig.getApi('login'),
        form: {
            user: config.get('user:name'),
            password: config.get('user:password'),
        }
    }

    // change tips
    $('#login-process-tips').text('Waiting for login.');

    // send request
    request.post(options, function(err, response, body) {
        if (err) {
            if (confirm('\t' + err + '\n\tCan not connet,click [OK] try again.Or [Cancel] quit app.\n')) {
                ep.emit('is-login');
            } else {
                ipc.send('app-quit');
            };
        } else {
            // parse login api response
            var res = JSON.parse(body);
            if (res.data) {
                ep.emit('update-userinfo', {
                    token: res.data.token
                });
                $('#login-process-tips').html('Login success.')
                ipc.send('login-success');
            } else {
                // alert('\tUsername or Password error.\n\tPlease input again.')
                ep.emit('show-input-view');
            };
        }
    });
});

ep.tail('update-userinfo', function(data) {
    for (var key in data) {
        config.set('user:' + key, data[key]);
    }
});
ep.tail('show-input-view', function() {
    $('#login-check-view').addClass('hidden');
    $('#login-input-view').removeClass('hidden');
    $('#login-input-view').addClass('show');

    $('#submit').click(function() {
        ep.emit('update-userinfo', {
            name: $('#username').val(),
            password: $('#password').val()
        });
        ep.emit('login')
    });
});
ep.emit('is-login');
@@ -941,64 +941,8 @@
    %7D
- else %7B%0A                ep.emit('login');%0A            %7D;
 %0A   
@@ -2019,32 +2019,35 @@
 %0A               
+ //
  ep.emit('show-i
@@ -2613,28 +2613,249 @@
 %7D);%0A%7D);%0A
-ep.emit('is-login')
+%0Aif (config.get('user:token')) %7B%0A    ep.emit('is-login');%0A%7D else if (config.get('user:name') && config.get('user:password')) %7B%0A    ep.emit('login');%0A%7D else %7B%0A    $('#username').val(config.get('user:name'));%0A    ep.emit('show-input-view');%0A%7D
 ;

optimize login process.