CKEDITOR.plugins.add('emailtemplates',
    {
        requires: ['richcombo'], //, 'styles' ],
        init: function (editor) {
            var config = editor.config,
                lang = editor.lang.format;

            // Gets the list of tags from the settings.
            var options = []; //new Array();
            var delimiter = '|^~^|';
            //this.add('value', 'drop_text', 'drop_label');
            //tags[0] = ["[contact_name]", "Name", "Name"];
            //tags[1] = ["[contact_email]", "email", "email"];
            //tags[2] = ["[contact_user_name]", "User name", "User name"];

            // Create style objects for all defined styles.

            //var fnData = editor.config.fnData;

            //if (!fnData || typeof (fnData) != 'function')
            //    throw "You must provide a function to retrieve the list data.";
             
            editor.ui.addRichCombo('emailtemplatesA',
                { 
                    label: "Sila pilih template emel – ezHasil",
                    title: "Sila pilih template emel – ezHasil",
                    voiceLabel: "Choose template",
                    toolbar: 'others', 
                    multiSelect: false,

                    panel:
                    {
                        css: [CKEDITOR.skin.getPath('editor')].concat(config.contentsCss),
                        //css: [config.contentsCss, CKEDITOR.getUrl(editor.skinPath + 'editor.css')],
                        voiceLabel: lang.panelVoiceLabel
                    },
                     
                    init: function () {
                        var self = this;
                        self.startGroup("Email Templates");
                        var retryCount = 0;
                        var delaySetDDLSelectedOption = function () {
                            if (editor.isTemplateLoaded == undefined && retryCount++ < 10) { 
                                setTimeout(delaySetDDLSelectedOption, retryCount * 100);
                            } else {
                                if (editor.IsEmailTemplateASelected)
                                    self.setValue(editor.selTemplateHTML, editor.selEmailSubject); 
                            }
                        }
                        
                        delaySetDDLSelectedOption();
                        //this.add('value', 'drop_text', 'drop_label');
                        //for (var this_tag in tags) {
                        //    this.add(tags[this_tag][0], tags[this_tag][1], tags[this_tag][2]);
                        //}

                        //var content = fnData();

                        //$.each(content, function (index, value) {
                        //    // value, html, text
                        //    self.add(value.name, value.name, value.name)
                        //});
                    },

                    onClick: function (value) {
                        
                        var decodeEntities =function (encodedString) {
                            var textArea = document.createElement('textarea');
                            textArea.innerHTML = encodedString;
                            return textArea.value;
                        }

                        editor.focus();
                        editor.fire('saveSnapshot'); 
                        editor.selTemplateID = value; 
                        editor.selEmailSubject = this._.items[value];

                        for (i = 0; i < editor.listTemplates.length; i++) {
                            if (editor.listTemplates[i].TemplateID == value) {
                                editor.setData(decodeEntities(editor.listTemplates[i].TemplateHTML));
                                break;
                            }
                        } 
                        editor.fire('saveSnapshot');
                        editor.IsEmailTemplateASelected = 1;
                        editor.IsEmailTemplateBSelected = 0;
                        //editor.insertHtml(value); 
                        //editor.fire('saveSnapshot');
                         
                        //this.setValue(this._.items, editor.selEmailSubject);

                        ////Comment out the replace or prompt change Email Subject part
                        //if (!$('#TempEmail_EmailSubject').val()) { 
                        //    var input = $('#TempEmail_EmailSubject');
                        //    input.val(editor.selEmailSubject);
                        //    input.trigger('input'); // Use for Chrome/Firefox/Edge
                        //    input.trigger('change'); // Use for Chrome/Firefox/Edge + IE11
                        //    return;
                        //};
                       
                        //swal({
                        //    title: "Would you like to replace the Subject to the selected template's Subject?",
                        //    text: "", 
                        //    showCancelButton: true,
                        //    confirmButtonText: "Yes",
                        //    cancelButtonText: "No",
                        //    closeOnConfirm: true,
                        //    closeOnCancel: true
                        //}, function (isConfirm) {
                        //    if (!isConfirm) { 
                        //        return;
                        //    }
                        //    var input = $('#TempEmail_EmailSubject');
                        //    input.val(editor.selEmailSubject);
                        //    input.trigger('input'); // Use for Chrome/Firefox/Edge
                        //    input.trigger('change'); // Use for Chrome/Firefox/Edge + IE11
                        //}); 
                    },

                    onRender: function () {
                        
                        editor.on("selectionChange", function (b) {
                           
                            if (editor.IsEmailTemplateASelected) {
                                this.setValue(editor.selTemplateHTML, editor.selEmailSubject);
                            }
                            else {
                                this.setValue(null);
                            }

                            //editor.focus();
                            //editor.fire('saveSnapshot');
                            return
                        }, this)
                    }
                });

            editor.ui.addRichCombo('emailtemplatesB',
               {
                   label: "Sila pilih template emel – Umum",
                   title: "Sila pilih template emel – Umum",
                   voiceLabel: "Choose template",
                   toolbar: 'others',
                   multiSelect: false,

                   panel:
                   {
                       css: [CKEDITOR.skin.getPath('editor')].concat(config.contentsCss),
                       //css: [config.contentsCss, CKEDITOR.getUrl(editor.skinPath + 'editor.css')],
                       voiceLabel: lang.panelVoiceLabel
                   },

                   init: function () {
                       var self = this;
                       self.startGroup("Email Templates");
                       var retryCount = 0;
                       var delaySetDDLSelectedOption = function () {
                           if (editor.isTemplateLoaded == undefined && retryCount++ < 10) {
                               setTimeout(delaySetDDLSelectedOption, retryCount * 100);
                           } else {
                               if (editor.IsEmailTemplateBSelected) 
                                self.setValue(editor.selTemplateHTML, editor.selEmailSubject);
                           }
                       }

                       delaySetDDLSelectedOption();
                       //this.add('value', 'drop_text', 'drop_label');
                       //for (var this_tag in tags) {
                       //    this.add(tags[this_tag][0], tags[this_tag][1], tags[this_tag][2]);
                       //}

                       //var content = fnData();

                       //$.each(content, function (index, value) {
                       //    // value, html, text
                       //    self.add(value.name, value.name, value.name)
                       //});
                   },

                   onClick: function (value) {

                       var decodeEntities = function (encodedString) {
                           var textArea = document.createElement('textarea');
                           textArea.innerHTML = encodedString;
                           return textArea.value;
                       }
                       editor.focus();
                       editor.fire('saveSnapshot'); 
                       editor.selTemplateID = value; 
                       editor.selEmailSubject = this._.items[value];
                       
                       for (i = 0; i < editor.listTemplates.length; i++) {
                           if (editor.listTemplates[i].TemplateID == value) {
                               editor.setData(decodeEntities(editor.listTemplates[i].TemplateHTML));
                               break;
                           }
                       } 
                       editor.fire('saveSnapshot');

                       editor.IsEmailTemplateASelected = 0;
                       editor.IsEmailTemplateBSelected = 1;
                       //editor.insertHtml(value); 
                       //editor.fire('saveSnapshot');

                       //this.setValue(this._.items, editor.selEmailSubject);

                       ////Comment out the replace or prompt change Email Subject part
                       //if (!$('#TempEmail_EmailSubject').val()) { 
                       //    var input = $('#TempEmail_EmailSubject');
                       //    input.val(editor.selEmailSubject);
                       //    input.trigger('input'); // Use for Chrome/Firefox/Edge
                       //    input.trigger('change'); // Use for Chrome/Firefox/Edge + IE11
                       //    return;
                       //};

                       //swal({
                       //    title: "Would you like to replace the Subject to the selected template's Subject?",
                       //    text: "", 
                       //    showCancelButton: true,
                       //    confirmButtonText: "Yes",
                       //    cancelButtonText: "No",
                       //    closeOnConfirm: true,
                       //    closeOnCancel: true
                       //}, function (isConfirm) {
                       //    if (!isConfirm) { 
                       //        return;
                       //    }
                       //    var input = $('#TempEmail_EmailSubject');
                       //    input.val(editor.selEmailSubject);
                       //    input.trigger('input'); // Use for Chrome/Firefox/Edge
                       //    input.trigger('change'); // Use for Chrome/Firefox/Edge + IE11
                       //}); 
                   },

                   onRender: function () {

                       editor.on("selectionChange", function (b) {
                           if (editor.IsEmailTemplateBSelected) {
                               this.setValue(editor.selTemplateHTML, editor.selEmailSubject);
                           }
                           else {
                               this.setValue(null);
                           }
                          

                           //editor.focus();
                           //editor.fire('saveSnapshot');
                           return
                       }, this)
                   }
               });

        }
    });