window.Dashboard=function () {
    let Opts = {
        active: false,
        containerCssSelector:".dashboard",
        testFileMod:false,
        isAdmin:false,
        URL:{
            create:'',
            store:'',
            getDataTableContent:'',
            destroy:'',
        }
    };
    let root = this;
    /*
     * Constructor
     */
    this.construct = function (options) {
        debug('Dashboard constructor called');
        $.extend(Opts, options);
    };
    this.getOpts=function (){
        return Opts;
    }
    this.init = function (options) {
        console.log('Dashboard init called');
        $.extend(Opts, options);
/*
        APP.setOpts({
            afterLoadMainContent:root.initEditor,
        })
*/
        /*
        Init list view
        */
        root.initEditorButtons();
    }

    this.initEditorButtons=function (){
        $('.DashboardEditable button.edit').off('click');
        $('.DashboardEditable button.edit').on('click',function (){
            root.initEditor();
            $('.btnGroupEdit').toggleClass('d-none');
            $('.btnGroupModify').toggleClass('d-none');
        });

        $('.DashboardEditable button.btnSave').off('click');
        $('.DashboardEditable button.btnSave').on('click',function (){
            /*$('.btnGroupEdit').toggleClass('d-none');
            $('.btnGroupModify').toggleClass('d-none');
            */
            root.saveCustomContent();
            //tinymce.activeEditor.isDirty() //true when modified
        });



        $('.DashboardEditable button.btnCancel').off('click');
        $('.DashboardEditable button.btnCancel').on('click',function (){

            $('.btnGroupEdit').toggleClass('d-none');
            $('.btnGroupModify').toggleClass('d-none');
            root.removeEditor();
        });

    }

    this.saveCustomContent=function (){
        let ajaxData= new FormData($('.dashboardForm')[0]);
        ajaxData.append('stored_data',tinymce.activeEditor.getContent());
        let ajaxUrl=Opts.URL.update.replace('%id%',$('div.dashboardCustomContent').data('id'));
        ajaxData.append('_method','PUT');
        $.ajax({
            url:ajaxUrl,
            data:ajaxData,
            type:'POST',
            contentType:false,
            processData: false,
            beforeSend: function() {

            },
            success: function(data) {
                Toast.create("Siekeres mentés!",'Új tartalom elmentve.' , TOAST_STATUS.INFO,5000);
            },
            error: function(data) {
                console.log(data.responseJSON);
                ajaxErrorFieldHandler(data.responseJSON.error);
            }
        })

    }

    this.loadTinymce= function (){
        $.getScript('https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js',function (){
           root.initEditor();
        });
    }
    this.removeEditor = function (options) {
        if(typeof tinymce!=="undefined"){
            tinymce.remove();
        }
    }
    this.initEditor = function (options) {

        if(typeof tinymce==="undefined"){
            root.loadTinymce();
            return;
        }

        tinymce.init({
            selector: 'div.dashboardCustomContent', // Replace this CSS selector to match the placeholder element for TinyMCE
            language: 'hu_HU',
            plugins: 'code table lists',
            toolbar: ' undo redo | formatselect| bold italic | alignleft aligncenter alignright | indent outdent | bullist numlist | code | table',

/*
            toolbar: 'customInsertButton | undo redo | formatselect| bold italic | alignleft aligncenter alignright | indent outdent | bullist numlist | code | table',
            setup: function (editor) {
                editor.ui.registry.addButton('customInsertButton', {
                    text: 'My Button',
                    onAction: function (_) {
                        editor.insertContent('&nbsp;<strong>It\'s my button!</strong>&nbsp;');
                    }});
            }
*/

        });
    }
};
