define('jForumXhr', ['require','jquery','jForumUtils','jForumTemplate','jForumXhrDefer', 'store'], function (require, $, util, template, xhrDefer, store) {
    'use strict';

    var jForumXhr = (function () {
        var jqXhr = {};
        //抽像的加载JSONP的Loader
        jqXhr.jsonpLoader = function (reqUri, queryDataJson, jqEle) {
            var defer = $.Deferred();
            if(jqEle.attr('data-loader-stamp')){ //防止重复加载
                defer.reject();
            }else{
                jqEle.attr('data-loader-stamp', util.getUnixStamp());
                return $.ajax(reqUri, {
                    method : 'GET',
                    jsonp : 'callback',
                    dataType : 'jsonp',
                    data: $.param(queryDataJson)
                });
            }
            return defer.promise();
        };
        //抽像的加载JSON的Loader
        jqXhr.jsonLoader = function (reqUri, queryDataJson, allowEmpty, jqEle) {
            var defer = $.Deferred();
            if(jqEle.attr('data-loader-stamp')){ //防止重复加载
                defer.reject();
            }else{
                jqEle.attr('data-loader-stamp', util.getUnixStamp());
                
                $.ajax(reqUri, {
                    method : 'GET',
                    dataType : 'json',
                    data : $.param(queryDataJson)
                }).done(function(data, textStatus, jqXHR){
                    if ($.isEmptyObject(data)) {
                        if (!(allowEmpty)) {
                            jqEle.parents('.section').remove();
                        }
                        return;
                    }
                    jqEle.find('.no-record-tip').remove();
                    defer.resolve(data);
                }).fail(function(jqXHR, textStatus, errorThrown){
                    defer.reject(jqXHR, textStatus);
                });
            }
            return defer.promise();
        };
        //加载话题内的回复表单|登录表单
        jqXhr.bodyLoader = function (reqUri, callFun, queryDataJson, jqEle) {
            var requestURI = reqUri;
            if (!$.isEmptyObject(queryDataJson)) {
                requestURI += '?' + $.param(queryDataJson);
            }
            jqEle.addClass('ld-over running');
            jqEle.load(requestURI + ' body', function (response, status, xhr) {
                if (status === "error") {
                    var err = $.parseJSON(response);
                    if (err.level && err.level === 'err') {
                        return template[jqEle.attr('data-error-function')](err);
                    }
                }
                jqEle.append(response);
                jqEle.removeClass('ld-over running');
                try {
                    template[callFun](jqEle);
                } catch (e) { }
            });
        };
        //CMD
        jqXhr.action = function (reqUri, callFun, queryDataJson, showTip, jqEle) {
            var showTipMessage = showTip;
            $.ajax(reqUri, {
                type: 'POST',
                data: queryDataJson,
                dataType: 'json',
                timeout: 2000,
                async: true,
                cache: false
            }).done(function (response) {
                //成功:有回调的执行回调,没有回调的提示
                if (response.level === 'acc') {
                    try {
                        xhrDefer[callFun](jqEle);
                    } catch (e) { console.error(e.message); }
                } else { //失败:都提示
                    showTipMessage = true;
                }
                if (showTipMessage) {
                    response.refresh = false;
                    util.tipDialog(response);
                }
            }).fail(function (jqXHR, textStatus, errorThrown) {
                if (textStatus === 'error') {
                    var err = $.parseJSON(jqXHR.responseText);
                    if (err.level && err.level === 'err') {
                        return util.errorMessage(err.message);
                    }
                }
                util.errorMessage('当前操作因错误而异外中止');
            }).always(function (data, textStatus, jqXHR) {
                var d = {};
                d.fill = jqEle.hasClass('action-cache') ? true : false;
                d.action = jqEle.attr('data-action') || null;
                d.connect = jqEle.attr('data-action-key') || null;
                if (data.level === 'acc') {
                    d.success = true;
                }
                if (data.level === 'err' && data.input === d.connect) {
                    d.success = true;
                }
                asynActionCache(d);
            });
        };
        jqXhr.jsonGet = function (reqUri, option) {
            return jqXHR(reqUri, 'GET', option);
        };
        jqXhr.jsonPost = function (reqUri, option) {
            return jqXHR(reqUri, 'POST', option);
        };
        function jqXHR(reqUri, httpMethod, option){
            var postConfig = option || {};
            var requestData = postConfig.data || {};
            var jsonCallback = postConfig.jsonCallback || null;
            var failCallback = postConfig.failCallback || null;
            var defer = $.Deferred();
            //
            $.ajax(reqUri, {
                type: httpMethod,
                data: requestData,
                dataType: 'json'
            }).done(function (json) {
                if (null !== jsonCallback && $.isFunction(jsonCallback)) {
                    jsonCallback(json);
                }
                defer.resolve(json);
            }).fail(function (jqXHR, textStatus, errorThrown) {
                if (null !== failCallback && $.isFunction(failCallback)) {
                    failCallback(jqXHR, textStatus, errorThrown);
                }
                defer.reject(jqXHR, textStatus);
            });
            return defer.promise();
        }
        function asynActionCache(obj) {
            if (util.isBlank(obj.action) || util.isBlank(obj.connect)) { //重要参数检查
                return;
            }
            //是添加是删除
            if (!obj.success) {
                return;
            }
            if (obj.fill) {
                store.set(obj.action + '#' + obj.connect, true); //删除成功的都不对了
            } else {
                store.remove(obj.action + '#' + obj.connect);
            }
        }
        return jqXhr;
    })();
    return jForumXhr;
});