/**
 *  1.game_id, urlType, dialog, refer, refer_param, game_server_id is configurable
 *  2.not login: click to login->record user name to database->show login
 *  3.logined: query database judge user is exixt ? show login : do nothing
 *  4.toufang page: show login dialog when open page; get refer param from url
 **/
define([
    './mediaSet',
    'utils',
    'config'
], function ( m, utils,config) {
    //declare login object
    var l = new SQ.TgLogin({
        game_id: m.gameId,
        urlType: 0,
        dialog: true,
        dialogOpts: {
            hideId: true,
            zIndex: 1000
        },
        refer: m.refer,
        refer_param: m.refer_param,
        game_server_id: m.game_server_id,
        lr: function (userInfo, res) {
            //record user
            if(m.category !== "toufang"){
                utils._jsonP({
                    url: apiUrl.recordUser,
                    data: {
                        category: m.category
                    }
                });
            }

            l.lrCallback&&l.lrCallback();
            $('.lr_box').children().eq(0).addClass("hide").next().removeClass("hide");
            var username = userInfo.SHOW_NAME;
            if (userInfo.AUTH_TYPE && !userInfo.ALIAS_ACCOUNT) {
                username = "<img src='http://img1.37wanimg.com/www/css/images/game_site/ico-" + userInfo.AUTH_TYPE + ".png' alt=''/> " + username;
            }
            SQ.byId("top-login-name").innerHTML = username;
        },
        unlog: function (res, type) {
            type === "info" && res.msg && SQ.alert(res.msg);
        },
        unreg: function (res, type) {
            type === "reg" && res.msg && SQ.alert(res.msg);
        },
        out: function () {
            window.location.reload();
        }
    });

    //judge user is or isn't exist
    var apiUrl = config.apiUrl;
    if (!apiUrl.exist) {
        throw new Error("please set the api: exist");
    }
    if (!apiUrl.recordUser) {
        throw new Error("please set the api: recordUser");
    }
    //auto login
    if (m.category != 'toufang') {//toufang not retain login state
        //judge user is or isn't aready reged
        utils._jsonP({
            url: apiUrl.exist,
            data: {
                category: m.category
            }
        }, function (res) {
            if (res && res.result) {
                // if ( +res.result === 1 && res.list && res.list.exists ) {
                if (+res.result === 1) {
                    l.getUserInfo();
                }
            }
        });
    } else {
        l.dialog.show(1);
    }

    //event
    $(document)
        .on("click", "#top-log-btn", function (e) {
            e.preventDefault();
            l.dialog.show(0);
        })
        .on("click", "#top-reg-btn", function (e) {
            e.preventDefault();
            l.dialog.show(1);
        })
        .on("click", "#top-out-btn", function (e) {
            e.preventDefault();
            l.toOut();
        });

    return l;
});