/*!
 * Admin script of trainifyframework
 * @package MatrixAddons
 * @subpackage trainifyframework
 */
 (function($){
    'use strict';
    var winWidth, winHeight, TFFAdmin;
    var tff_document = $(document);
    TFFAdmin = {
        // Repeater Library
        Repeater: function(){

            /*sortable*/
            var TFF_REFRESH_VALUE = function (wrapObject) {

                wrapObject.find('[name]').each(function(){
                    $(this).trigger('change');
                });

            };

            var TFF_SORTABLE = function () {
                var repeaters = $('.trainify-framework-main-repeater');
                repeaters.sortable({
                    orientation: "vertical",
                    items: '> .tff-widget-repeater-table',
                    placeholder: 'tff-sortable-placeholder',
                    update: function( event, ui ) {
                        TFF_REFRESH_VALUE(ui.item);
                    }
                });
                repeaters.trigger("sortupdate");
                repeaters.sortable("refresh");
            };

            /*replace*/
            var TFF_REPLACE = function( str, replaceWhat, replaceTo ){
                var re = new RegExp(replaceWhat, 'g');
                return str.replace(re,replaceTo);
            };

            var TFF_REPEATER =  function (){
                tff_document.on('click','.tff-add-repeater',function (e) {
                    e.preventDefault();
                    var add_repeater = $(this),
                        repeater_wrap = add_repeater.closest('.trainify-framework-main-repeater'),
                        code_for_repeater = repeater_wrap.find('.tff-code-for-repeater'),
                        total_repeater = repeater_wrap.find('.tff-total-repeater-counter'),
                        total_repeater_value = parseInt( total_repeater.val() ),
                        repeater_html = code_for_repeater.html();

                    total_repeater.val( total_repeater_value +1 );
                    var final_repeater_html = TFF_REPLACE( repeater_html, add_repeater.attr('id'),total_repeater_value );
                    add_repeater.before($('<div class="tff-widget-repeater-table"></div>').append( final_repeater_html ));
                    var new_html_object = add_repeater.prev('.tff-widget-repeater-table');
                    var repeater_inside = new_html_object.find('.trainify-framework-inside-repeater');
                    repeater_inside.slideDown( 'fast',function () {
                        new_html_object.addClass( 'open' );
                        TFF_REFRESH_VALUE(new_html_object);
                    } );
                    TFFAdmin.Snipits.Color_Picker(repeater_inside.find('.tff-color-picker:not(.wp-color-picker)')); //Color picker initialized on widget repeater added
                });
                tff_document.on('click', '.trainify-framework-repeater-top, .trainify-framework-main-repeater-close', function (e) {
                    e.preventDefault();
                    var accordion_toggle = $(this),
                        repeater_field = accordion_toggle.closest('.tff-widget-repeater-table'),
                        repeater_inside = repeater_field.find('.trainify-framework-inside-repeater');

                    if ( repeater_inside.is( ':hidden' ) ) {
                        repeater_inside.slideDown( 'fast',function () {
                            repeater_field.addClass( 'open' );
                        } );
                    }
                    else {
                        repeater_inside.slideUp( 'fast', function() {
                            repeater_field.removeClass( 'open' );
                        });
                    }
                });
                tff_document.on('click', '.trainify-framework-main-repeater-remove', function (e) {
                    e.preventDefault();
                    var repeater_remove = $(this),
                        repeater_field = repeater_remove.closest('.tff-widget-repeater-table'),
                        repeater_wrap = repeater_remove.closest('.trainify-framework-main-repeater');

                    repeater_field.remove();
                    repeater_wrap.closest('form').trigger('change');
                    TFF_REFRESH_VALUE(repeater_wrap);
                });

                tff_document.on('change', '.tff-select', function (e) {
                    e.preventDefault();
                    var select = $(this),
                        repeater_inside = select.closest('.trainify-framework-inside-repeater'),
                        postid = repeater_inside.find('.tff-postid'),
                        repeater_control_actions = repeater_inside.find('.trainify-framework-main-repeater-control-actions'),
                        optionSelected = select.find("option:selected"),
                        valueSelected = optionSelected.val();

                    if( valueSelected == 0 ){
                        postid.remove();
                    }
                    else{
                        postid.remove();
                        $.ajax({
                            type      : "GET",
                            data      : {
                                action: 'tff_get_edit_post_link',
                                id: valueSelected
                            },
                            url       : ajaxurl,
                            beforeSend: function ( data, settings ) {
                                postid.remove();

                            },
                            success   : function (data) {
                                if( 0 != data ){
                                    repeater_control_actions.append( data );
                                }
                            },
                            error     : function (jqXHR, textStatus, errorThrown) {
                                console.log(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                            }
                        });
                    }
                });
            };

            tff_document.on('widget-added widget-updated panelsopen', function( event, widgetContainer ) {
                TFF_SORTABLE();
            });

            /*
             * Manually trigger widget-added events for media widgets on the admin
             * screen once they are expanded. The widget-added event is not triggered
             * for each pre-existing widget on the widgets admin screen like it is
             * on the customizer. Likewise, the customizer only triggers widget-added
             * when the widget is expanded to just-in-time construct the widget form
             * when it is actually going to be displayed. So the following implements
             * the same for the widgets admin screen, to invoke the widget-added
             * handler when a pre-existing media widget is expanded.
             */
            $( function initializeExistingWidgetContainers() {
                var widgetContainers;
                if ( 'widgets' !== window.pagenow ) {
                    return;
                }
                widgetContainers = $( '.widgets-holder-wrap:not(#available-widgets)' ).find( 'div.widget' );
                widgetContainers.one( 'click.toggle-widget-expanded', function toggleWidgetExpanded() {
                    TFF_SORTABLE();
                });
            });
            TFF_REPEATER();

        },
        //Custom Snipits goes here
        Snipits: {
            Variables: function(){
                winWidth = $(window).width();
                winHeight = $(window).height();
            },

            Color_Picker: function(selector){
                var color_picker_option = {
                    change: function(event, ui){
                        var color_code = ui.color.toString();
                        $(event.target).val(color_code).trigger('change');
                        selector.closest('form').trigger('change');
                    },
                };
                selector.wpColorPicker(color_picker_option);
            },

            ImageUpload: function(evt){
                // Prevents the default action from occuring.
                evt.preventDefault();
                var media_title = $(this).data('title');
                var media_button = $(this).data('button');
                var media_input_val = $(this).prev();
                var media_image_url_value = $(this).prev().prev().children('img');
                var media_image_url = $(this).siblings('.img-preview-wrap');

                var meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
                    title: media_title,
                    button: { text:  media_button },
                    library: { type: 'image' }
                });

                // Opens the media library frame.
                meta_image_frame.open();

                // Runs when an image is selected.
                meta_image_frame.on('select', function(){

                    // Grabs the attachment selection and creates a JSON representation of the model.
                    var media_attachment = meta_image_frame.state().get('selection').first().toJSON();

                    // Sends the attachment URL to our custom image input field.
                    media_input_val.val(media_attachment.url);
                    if( media_image_url_value != null ){
                        media_image_url_value.attr( 'src', media_attachment.url );
                        media_image_url.show();
                    }
                    media_input_val.trigger('change');
                });
            },

            WidgetTab: function(evt){
                if (!$(this).hasClass('nav-tab-active')) {
                    var tab_wraper, tab_id;
                    tab_id = $(this).data('id');
                    tab_wraper = $(this).closest('.tff-widget-field-tab-wraper');
                    $(this).addClass('nav-tab-active').siblings('.nav-tab').removeClass('nav-tab-active');
                    tab_wraper.find('.trainify-framework-tab-contents').removeClass('trainify-framework-tab-active');
                    tab_wraper.find(tab_id).addClass('trainify-framework-tab-active');
                }
            },

            Widget_Accordion: function(){
                var accordion_title = $(this).closest('.trainify-framework-accordion-title');
                var is_checked = $(this).prop('checked');
                if(is_checked){
                    accordion_title.siblings('.trainify-framework-accordion-content').slideDown().removeClass('open close');
                    accordion_title.find('.trainify-framework-accordion-arrow').addClass('fa-angle-up').removeClass('fa-angle-down');
                }else{
                    accordion_title.siblings('.trainify-framework-accordion-content').slideUp().removeClass('open close');
                    accordion_title.find('.trainify-framework-accordion-arrow').addClass('fa-angle-down').removeClass('fa-angle-up');
                }
            },

            Widget_Relation_Action: function(relation_field, relation, relations_key){
                var current_value = relation_field.val();
                // Only we need to change for checkbox by using 
                if(relation_field.prop('tagName')=='CHECKBOX'){
                    current_value = (relation_field.is(':checkbox') && relation_field.is(':checked')) ? current_value : '';
                }
                for(var relation_key in relation){
                    if(relations_key=="empty" || relations_key=="exist"){
                        current_value = relations_key;
                    }else if(relations_key=="values"){
                        if(relation_key!=current_value){
                            continue;
                        }
                    }
                    var relation_details = relation[relation_key];                    
                    for(var action_key in relation_details){
                        var action_detils = relation_details[action_key];
                        var action_detail_class = action_detils.join(", .");
                        var action_class = '.'+action_detail_class;
                        switch(action_key){
                            case 'show_fields':
                                relation_field.closest('.widget-content').find(action_class).removeClass('trainify-framework-hidden-field');
                                break;
                            case 'hide_fields':
                                relation_field.closest('.widget-content').find(action_class).addClass('trainify-framework-hidden-field');
                                break;
                            default:
                                console.warn('Action ' + relation_key + ' case is not defined');
                            break;
                        }
                    }
                }
            },

            Widget_Relation: function(evt) {
                var relation_field = $(this);
                var current_value = relation_field.val();
                // Only we need to change for checkbox by using 
                if(relation_field.prop('tagName')=='CHECKBOX'){
                    current_value = (relation_field.is(':checkbox') && relation_field.is(':checked')) ? current_value : '';
                }
                var relations = $(this).data('relations');
                if(!relations){
                    return;
                }
                var __this = TFFAdmin;
                var snipits = __this.Snipits;
                var relation_action = snipits.Widget_Relation_Action;
                for(var relations_key in relations){
                    var relation = relations[relations_key];
                    if(!relation){
                        continue;
                    }
                    switch(relations_key){
                        case 'exist':
                            if( current_value ){
                                relation_action(relation_field, {'exist':relation}, relations_key);    
                            }
                            break;
                        case 'empty':
                            if( !current_value ){
                                relation_action(relation_field, {'empty':relation}, relations_key);    
                            }
                            break;
                        case 'values':
                            if( relation ){
                                relation_action(relation_field, relation, relations_key);    
                            }
                            break;
                        default: 
                            console.warn('Relation key ' + relations_key + 'is not found.');
                            break;
                    }
                }
            },

            CustomizerIcons: function(){
                var single_icon = $(this),
                    trainify_framework_customize_icons = single_icon.closest( '.trainify-framework-icons-wrapper' ),
                    icon_display_value = single_icon.children('i').attr('class'),
                    icon_split_value = icon_display_value.split(' '),
                    icon_value = icon_split_value[1];

                single_icon.siblings().removeClass('selected');
                single_icon.addClass('selected');
                trainify_framework_customize_icons.find('.tff-icon-value').val( icon_value );
                trainify_framework_customize_icons.find('.trainify-framework-icon-preview').html('<i class="' + icon_display_value + '"></i>');
                trainify_framework_customize_icons.find('.tff-icon-value').trigger('change');
            },

            IconToggle: function(){
                var icon_toggle = $(this),
                    trainify_framework_customize_icons = icon_toggle.closest( '.trainify-framework-icons-wrapper' ),
                    icons_list_wrapper = trainify_framework_customize_icons.find( '.icons-list-wrapper' ),
                    dashicons = trainify_framework_customize_icons.find( '.dashicons' );
                if ( icons_list_wrapper.is(':hidden') ) {
                    icons_list_wrapper.slideDown();
                    dashicons.removeClass('dashicons-arrow-down');
                    dashicons.addClass('dashicons-arrow-up');
                } else {
                    icons_list_wrapper.slideUp();
                    dashicons.addClass('dashicons-arrow-down');
                    dashicons.removeClass('dashicons-arrow-up');
                }
            },

            RemoveIcon: function(){
                var trainify_framework_remove_icon = $(this);
                var trainify_framework_menuicon_wrapper = trainify_framework_remove_icon.closest( '.trainify-framework-icons-wrapper' );
                var icons_list_wrapper = trainify_framework_menuicon_wrapper.find( '.icons-list-wrapper' );
                trainify_framework_menuicon_wrapper.find('.tff-icon-value').val('');
                trainify_framework_menuicon_wrapper.find('.trainify-framework-icon-preview').html('');
                icons_list_wrapper.find('.single-icon').removeClass('selected');
            },

            IconSearch: function(){
                var text = $(this),
                value = this.value,
                trainify_framework_customize_icons = text.closest( '.trainify-framework-icons-wrapper' ),
                icons_list_wrapper = trainify_framework_customize_icons.find( '.icons-list-wrapper' );
                icons_list_wrapper.find('i').each(function () {
                    if ($(this).attr('class').search(value) > -1) {
                        $(this).parent('.single-icon').show();
                    } else {
                        $(this).parent('.single-icon').hide();

                    }
                });
            },
            Widget_Change: function(evt, widget){
                var __this = TFFAdmin;
                var snipits = __this.Snipits;
                var this_widget = $(widget);
                this_widget.find('.trainify_framework_widget_field_relation').trigger('change');
                snipits.Color_Picker(this_widget.find('.tff-color-picker'));
                if(this_widget.hasClass('widget-dirty')){
                    this_widget.removeClass('widget-dirty');
                    this_widget.find('input[type="submit"]').attr('disabled','disabled');    
                }
            },
        },     

        Click: function(){

            var __this = TFFAdmin;
            var snipits = __this.Snipits;

            var image_upload = snipits.ImageUpload;
            tff_document.on('click', '.media-image-upload', image_upload);

            var widget_tab = snipits.WidgetTab;
            tff_document.on('click', '.tff-widget-tab-list .nav-tab', widget_tab);

            var widget_relations = snipits.Widget_Relation;
            $(document).on('change', '.trainify_framework_widget_field_relation', widget_relations);
            
            //for default load
            $('.trainify_framework_widget_field_relation').trigger('change');

            // Runs when the image button is clicked.
            tff_document.on('click','.media-image-remove', function(e){
                $(this).siblings('.img-preview-wrap').hide();
                $(this).prev().prev().val('');
            });

             /**
             * Script for Customizer icons
             */
            var customizer_icons = snipits.CustomizerIcons;
            tff_document.on('click', '.trainify-framework-icons-wrapper .single-icon', customizer_icons);

            var icon_toggle = snipits.IconToggle;
            tff_document.on('click', '.trainify-framework-icons-wrapper .icon-toggle, .trainify-framework-icons-wrapper .trainify-framework-icon-preview', icon_toggle);

            var remove_icon = snipits.RemoveIcon;
            tff_document.on('click', '.trainify-framework-icons-wrapper .remove-icon', remove_icon);

            var icon_search = snipits.IconSearch;
            tff_document.on('keyup', '.trainify-framework-icons-wrapper .icon-search', icon_search);

            var widget_accordion = snipits.Widget_Accordion;
            tff_document.on('change', '.trainify-framework-accordion-title input', widget_accordion);

            var widget_change = snipits.Widget_Change;
            tff_document.on('widget-added widget-updated panelsopen', widget_change);

        },

        Ready: function(){
            var __this = TFFAdmin;
            var snipits = __this.Snipits;

            //Library
            __this.Repeater();

            //This is newsmagazine functions
            snipits.Variables();
            snipits.Color_Picker($('.tff-color-picker'));
            __this.Click();

        },

        Load: function(){

        },

        Resize: function(){

        },

        Scroll: function(){

        },

        Init: function(){
            var __this = TFFAdmin;
            var docready = __this.Ready;
            var winload = __this.Load;
            var winresize = __this.Resize;
            var winscroll = __this.Scroll;
            $(document).ready(docready);
            $(window).load(winload);
            $(window).scroll(winscroll);
            $(window).resize(winresize);
        },

     };
     
     TFFAdmin.Init();

})(jQuery);