define(["utils", "config", "dialog", "serverData"], function (utils, config, dialogFactory, serverData) {
    var apiUrl = config.apiUrl;
    var types, s_type = 19;
    serverData.defer.then(function (result) {
        types = result;
    });

    return function (callback) {
        var select_server = dialogFactory({
            title: "选择服务器",
            content: SQ.T(__inline('./server_select.html')),
            buttons: {
                "确定": function () {
                }
            }
        }), select_menu;

        select_server.bind("show", function () {
            var $dialog_btn = $('.sq-dialog-btn', select_server.el);
            $dialog_btn.attr('target', '_blank');
            $dialog_btn.click(function (e) {
                if(+$('#zodiacServerList_dialog :selected').data('state') === 1){
                    e.preventDefault();
                    SQ.sAlert($('#zodiacServerList_dialog :selected').text()+'还未开启，请您选择其他服');
                };

                if(+$('#zodiacServerList_dialog :selected').data('state') === 3){
                    e.preventDefault();
                    SQ.sAlert($('#zodiacServerList_dialog :selected').text()+'在维护中，请您选择其他服');
                };

                if(+$('#zodiacServerList_dialog :selected').data('state') === 2){
                    utils._jsonP(apiUrl.reportFirstLogin, function (res) {
                        callback && callback($("#zodiacServerList_dialog").val());
                        select_server.hide();
                    });
                }else{
                    SQ.sAlert('敬请期待！');
                    e.preventDefault();
                }
            });

            if (types.length === 0) {
                return;
            }

            var allZodiacServers = types;

            $("#zodiacServerTypeList_dialog").empty();

            var options = $.map(allZodiacServers, function (item, key) {
                return '<option data-index="' + key + '"value="' + item.s_name_id + '">' + item.s_name + '</option>';
            }).join('');

            $("#zodiacServerTypeList_dialog").html(options);

            var servers = allZodiacServers[0].servers;
            var serverOptions = '';
            for (var i = 0; i < servers.length; i++) {
                serverOptions += '<option data-state="' + servers[i].state + '" value="' + servers[i].sid + '">' + servers[i].s_name + '</option>';
            }

            $("#zodiacServerList_dialog").empty().append($(serverOptions));
            $dialog_btn.attr('href', config.getPlayGameUrl($("#zodiacServerList_dialog").val()))

            $("#zodiacServerTypeList_dialog").on('change', function () {
                var $ts =$(this);
                var key  = +$(':selected',$ts).data('index');
                var servers = types[key].servers;
                var serverOptions = '';
                for (var i = 0; i < servers.length; i++) {
                    serverOptions += '<option data-state="' + servers[i].state + '" value="' + servers[i].sid + '">' + servers[i].s_name + '</option>';
                }
                $("#zodiacServerList_dialog").empty().append($(serverOptions));
                $dialog_btn.attr('href', config.getPlayGameUrl($("#zodiacServerList_dialog").val()))
            });

            $("#zodiacServerList_dialog").on('change', function () {
                $dialog_btn.attr('href', config.getPlayGameUrl($(this).val()));
            })
        });

        select_server.show();
        return select_server;
    }

});