import baseTool from '../../util/baseTool';
import {TPL_CONTAINER, TPL_MSG} from './messageTpl';
const Message = (($) => {
    const messageType = {     
            SUCCESS: 'success',
            WARN: 'warn',
            ERROR: 'error',
            INFO: 'info'
        },
        className = {
            success: 'cd-green-style',
            warn: 'cd-yellow-style',
            error: 'cd-red-style',
            info: 'cd-blue-style'
        },
        defaultConfig = {
            skin: className.success,
            text: '',
            needClearMsg: false,
            timeDuration: {
                'success': 2000,
                'warn': 4000,
                'error': 6000,
                'info': 4000
            }
        };
    class Message {
        constructor(config) {
            let _this = this;
            _this._config = _this._getConfig(config);
        }
        
        _getConfig(config) {
            return $.extend({}, defaultConfig, config);
        }
        
        static getMsgContainer(){
            if($('.J_msgContainer').length === 0){
                $('body').append(TPL_CONTAINER);
            }
            return $('.J_msgContainer');
        }
        
        static MessageType() {
            return messageType;
        }
        
        static error(msg, time, needClearMsg) {
            return new Message({
                timeDuration: typeof time === 'number'? time: defaultConfig.timeDuration['error'],
                needClearMsg: typeof needClearMsg === 'boolean'? needClearMsg: (typeof time === 'boolean'? time: false),
                skin: messageType.ERROR,
                text: msg,
            }).show();
        }
        
        static success(msg, time, needClearMsg) {
            return new Message({
                timeDuration: typeof time === 'number'? time: defaultConfig.timeDuration['success'],
                needClearMsg: typeof needClearMsg === 'boolean'? needClearMsg: (typeof time === 'boolean'? time: false),
                skin: messageType.SUCCESS,
                text: msg
            }).show()
        }
        
        static warn(msg, time, needClearMsg) {
            return new Message({
                timeDuration: typeof time === 'number'? time: defaultConfig.timeDuration['warn'],
                needClearMsg: typeof needClearMsg === 'boolean'? needClearMsg: (typeof time === 'boolean'? time: false),
                skin: messageType.WARN,
                text: msg
            }).show();
        }
        
        static info(msg, time, needClearMsg) {
            return new Message({
                timeDuration: typeof time === 'number'? time: defaultConfig.timeDuration['info'],
                needClearMsg: typeof needClearMsg === 'boolean'? needClearMsg: (typeof time === 'boolean'? time: false),
                skin: messageType.INFO,
                text: msg
            }).show();
        }
        
        show() {
            let _this = this;
            let $msg = $(template.render(TPL_MSG, {
                skin: _this._config.skin,
                text: _this._config.text
            }));
            let $contaienr = Message.getMsgContainer();
            let sameFlag = false,
                $msgNodes = $contaienr.find('.J_messageTextNode');
            if ($msgNodes.length) {
                $msgNodes.each(function (index, item) {
                    if ($(item).html().trim() === $.trim(_this._config.text)) {
                        sameFlag = true;
                        return false;
                    }
                });
            }
            if (sameFlag) {
                return;
            }
            if(_this._config.needClearMsg === true){
                $contaienr.find('.J_msgBox').remove();
            }
            $contaienr.removeClass('cd-add-msg');
            this._updateZindex();
            _this.$element = $msg;
            $contaienr.find('.J_msgWrap').prepend($msg);
            setTimeout(() => {
                Message.getMsgContainer().addClass('cd-add-msg');
                this.$element.addClass('cd-show');
            }, 0);
            setTimeout(() => {
                this.hide();
            }, this._config.timeDuration);
        }
        
        hide() {
            this.$element.removeClass('cd-show');
            setTimeout(() => {
                this.$element.remove();
                this.checkAndReset();
            }, 600);
        }
        
        checkAndReset(){
            let $container = Message.getMsgContainer();
            if($container.find('.J_msgBox').length === 0){
                $container.removeClass('cd-add-msg');
            }
        }
        
        _updateZindex() {
            Message.getMsgContainer().css('zIndex', baseTool.getToolTipIndex());
        }
        
        dispose() {
            this.hide();
        }
    }
    return Message;
})($);
export default Message;