define('jForumMember', ['require', 'jquery', 'jForumUtils', 'jForumSocket', 'store', 'js.cookie'], function (require, $, util, socket, store, Cookies) {
    'use strict';

    var jForumMember = (function () {
        function init() {
            var xhr = require("jForumXhr");
            var template = require("jForumTemplate");

            //快速登录[NW]
            $('#right_member_info').on('submit', '#quick_login_form', function (e) {
                var form = $(e.target);
                var requestData = {};
                requestData.names = form.find('input[name=names]').val();
                requestData.password = form.find('input[name=password]').val();
                requestData.token = form.find('input[name=token]').val();
                if (util.isBlank(requestData.names) || util.isBlank(requestData.password)) {
                    form.find('input').not('[type="submit"],[type="hidden"]').each(function () {
                        if (util.isBlank(this.value)) {
                            return false;
                        }
                    }).first().focus();
                    return false;
                }
                form.find('input[type=submit]').prop('disabled', true).val('正在登录');
                if (util.isBlank(xhr)) {
                    console.log('[right_member_info]xhr is not exists');
                    return;
                };
                xhr.jsonPost(form.attr('action'), {   //???
                    data: requestData,
                    jsonCallback: function (response) {
                        quickLoginCallFun(response, template);
                    },
                    failCallback: 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('当前操作因错误而异外中止');
                    }
                });
                return false;
            });
            //载入页顶的会员信息[NW]
            $('#header_member_panel').on('loadDataEvent', function (e) {
                var _mp = $(this);
                //至少拉一次
                var mv = getMemberVerify();
                if (mv !== -1) { //不需要拉取
                    return getCacheMemberInfo(mv, _mp, template);  //???
                }
                if (util.isBlank(xhr)) {
                    console.log('[header_member_panel]xhr is not exists');
                    return;
                };
                xhr.jsonGet(_mp.attr('data-handler'), {  //???
                    jsonCallback: function (data) {
                        if (!$.isEmptyObject(data)) {
                            //[HMP]创建缓存拉取标记
                            setMemberVerify(data.verify);
                            //[HMP]生成会员面板
                            template.drawMemberPanel(data, _mp); //怎么显示由drawMemberPanel负责
                        }
                    }
                });
            }).trigger('loadDataEvent');
            //更新会员头像[NW]
            $('img.member_default_avtar').on('click', function (e) {
                var _ma = $(this);
                var v = _ma.attr('data-path');
                xhr.jsonPost(BASE + '/member/home/avatar', {
                    data: { "file": v },
                    jsonCallback: function (response) {
                        if (response.level === 'acc') {
                            _ma.addClass('active');
                            response.refresh = false;
                            $('img.member_default_avtar').not(_ma).removeClass('active');
                        }
                        return util.tipDialog(response);
                    },
                    failCallback: function (jqXHR, textStatus, errorThrown) {
                        if (textStatus === 'error') {
                            var err = $.parseJSON(jqXHR.responseText);
                            if (err.level && err.level === 'err') {
                                return util.errorMessage(err.message);
                            }
                        }
                        return util.errorMessage('当前操作因错误而异外中止');
                    }
                });
                return false;
            });
        }
        function quickLoginCallFun(responseMsg, template) {
            //出错了
            if (responseMsg.hasOwnProperty('level')) {
                return util.tipDialog(responseMsg)
            }
            if (responseMsg.hasOwnProperty('id') && parseInt(responseMsg.id) > 0) {
                // 顶部的
                template.drawMemberPanel(responseMsg, $('#header_member_panel'));
                // 登录区
                template.drawMemberInfo(responseMsg, $('#right_member_info'));
                // 重新设置客户端缓存的标记
                setMemberVerify(responseMsg.verify);
            }
        };
        //客户端缓存的cookie标记未过期时更新会员的信息到哪取
        function getCacheMemberInfo(verify, jqEle, template) {
            var cacheData = store.get('member' + verify) || {};
            template.drawMemberPanel(cacheData, jqEle);
        };
        //设置客户端缓存的cookie标记
        function setMemberVerify(verify) {
            var in30Minutes = 1 / 48; //这个值是有效期,还标志着更新的频率
            Cookies.set('msa', verify, { expires: in30Minutes });
        };
        //当前活跃的会员信息
        function getCurrentActiveMemberInfo() {
            var verify = getMemberVerify();
            if (verify === -1) {
                return {};
            }
            return store.get('member' + verify) || {};
        };
        //是否要去拉取数据,客户端缓存的cookie标记是否过期了
        function getMemberVerify() {
            var verify = Cookies.get('msa') || -1;
            return verify;
        };
        //将store和通知从template.drawMemberPanel中取离出来
        function memberPanelDeferFunction(json) {
            //[HMP]检查客户端缓存是否存在
            var cacheData = store.get('member' + json.verify);
            if ($.isEmptyObject(cacheData)) {
                store.set('member' + json.verify, json);
            }
            //开始消息通知
            noticeHandler();
        };
        function noticeHandler() {
            try {
                var s = new socket($('#topmenu').attr('data-socket-uri'), $('#header_member_panel'), {
                    sendDataFun: function (jqEle) {
                        var data = {};
                        data.id = jqEle.attr('data-member');
                        data.cmd = 1;
                        data.ux = jqEle.attr('data-ux');
                        return data;
                    },
                    callbackFun: updateUnReadMessages
                });
                s.init('letterNotice');
                s.start(30000);
            } catch (e) { console.log('letter socket has err: ' + e.message); }
        };
        function updateUnReadMessages(sockplaindata) {
            var json = $.parseJSON(sockplaindata);
            if ($.isEmptyObject(json)) {
                return;
            }
            if (parseInt(json.result) > 0) {
                $('#notification').removeClass('d-none');
                $('#notification').html(json.result);
            } else {
                $('#notification').addClass('d-none');
            }
            $('#header_member_panel').attr('data-ux', util.getUnixStamp());
        };
        return { init: init, get: getCurrentActiveMemberInfo, panelRenderDefer: memberPanelDeferFunction };
    })();
    return jForumMember;
});