;(function($) {
    'use strict';

    const NOTIFY = function() {
        const _private = new WeakMap();

        function Notify() {
            _private.set(this, {
                $container: null,
                id: 0
            });

            this._init();
        }
        Notify.prototype = {
            //=============================================
            // Properties & methods (shared for all instances)
            //=============================================
            //=============================================
            // Private Methods
            //=============================================
            _init: function() {
                this._buildDOM();
            },

            _buildDOM: function() {
                const scope = _private.get(this);

                scope.$container = $('<div>').addClass('gutendraw-notify-container');
                $('body').append(scope.$container);
            },

            _animationEvent: function() {
                const el = document.createElement('fakeelement');
                const animations = {
                    'animation'      : 'animationend',
                    'MSAnimationEnd' : 'msAnimationEnd',
                    'OAnimation'     : 'oAnimationEnd',
                    'MozAnimation'   : 'mozAnimationEnd',
                    'WebkitAnimation': 'webkitAnimationEnd'
                }
                for (let i in animations){
                    if (el.style[i] !== undefined){
                        return animations[i];
                    }
                }
                return null;
            },

            _getNotify: function(id) {
                const scope = _private.get(this);
                const $item = scope.$container.find(`.gutendraw-notify[data-id='${id}']`);
                return $item.length ? $item : null;
            },

            _showNotify: function($notify, id, timeout) {
                setTimeout(this.close.bind(this, id), timeout ? timeout : 5000);
            },

            _removeNotify: function($notify) {
                $notify.remove();
            },
            //=============================================
            // Public Methods
            //=============================================
            show: function(html, className, timeout) {
                const scope = _private.get(this);

                const $notify = $('<div>').addClass('gutendraw-notify').addClass(className).attr({'data-id': ++scope.id});
                const $title = $('<div>').addClass('gutendraw-title').html(html);
                $notify.append($title);

                scope.$container.append($notify);

                $notify.removeClass('gutendraw-fx-show gutendraw-fx-hide');
                $notify.addClass('gutendraw-fx-show');
                $notify.one(this._animationEvent(), this._showNotify.bind(this, $notify, scope.id, timeout));

                return scope.id;
            },

            close: function(id) {
                const $notify = this._getNotify(id);
                if ($notify) {
                    $notify.removeClass('gutendraw-fx-show gutendraw-fx-hide');
                    $notify.addClass('gutendraw-fx-hide');
                    $notify.one(this._animationEvent(), this._removeNotify.bind(this, $notify));
                }
            }
        }

        return Notify;
    }();

    // Always export us to window global
    window.GUTENDRAW = window.GUTENDRAW ? window.GUTENDRAW : {};
    window.GUTENDRAW.PLUGINS = window.GUTENDRAW.PLUGINS ? window.GUTENDRAW.PLUGINS : {};
    window.GUTENDRAW.PLUGINS.NOTIFY = function() { return new NOTIFY(); };
}(jQuery));