(function (factory) {
    /* global define */
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['jquery'], factory);
    } else if (typeof module === 'object' && module.exports) {
        // Node/CommonJS
        module.exports = factory(require('jquery'));
    } else {
        // Browser globals: jQuery
        factory(window.jQuery);
    }
}(function ($) {

    $.extend($.summernote.plugins, {

        'ts_video': function (context) {
            var self = this;

            var ui = $.summernote.ui;
            var $editor = context.layoutInfo.editor;
            var options = context.options;
            var lang = options.langInfo;


            context.memo('button.ts_video', function () {

                var button = ui.button({
                    contents: '<i class="fab fa-youtube"></i>',
                    tooltip: 'ts_video',
                    click: function () {
                        self.show()
                    }
                });

                // create jQuery object from button instance.
                var $highlight = button.render();
                return $highlight;
            });




            this.createDialog = function () {

                var $box = $('<div />');
                var $frame = $('<iframe width="100%" height="444" frameborder="0" scrolling="no" src="'+siteUrl+'index.php?app=video&ac=editor&ts=list" />');
                $box.append($frame);
                return $box.html();
            };



            this.showAttachDialog = function () {
                return $.Deferred(function () {
                    ui.onDialogShown(self.$dialog, function () {

                    });

                    ui.onDialogHidden(self.$dialog, function () {

                    });

                    ui.showDialog(self.$dialog);

                });
            };



            this.show = function () {


                this.showAttachDialog().then(function () {
                    self.$dialog.modal('hide');
                    context.invoke('editor.restoreRange');


                });

            };



            this.initialize = function () {
                var $container = options.dialogsInBody ? $(document.body) : $editor;

                var body = [''].join('');

                this.$dialog = ui.dialog({
                    className: 'ts-video',
                    title: '我的视频',
                    body: this.createDialog(),
                    footer: body,
                    //callback: function ($node) {
                    //    $node.find('.modal-body').css({
                    //        'max-height': 300,
                    //        'overflow': 'scroll'
                    //    });
                    //}
                }).render().appendTo($container);
            };




            this.destroy = function () {
                ui.hideDialog(this.$dialog);
                this.$dialog.remove();
            };




        }
    })

}))