// reset-fields.js
jQuery(document).ready(function($) {

    function toggleDataSourceFields() {
        var val = $('#rzcps-post-type-select').val();

        $('#rzcps-category-group').hide();
        $('.rzcps-product-category-group').hide();
        $('#rzcps-xml-group').hide();
        $('#rzcps-stock-xml-group').hide();
        $('#rzcps-rss-group').hide();

        if(val === 'post') {
            $('#rzcps-category-group').show();
        }else if(val === 'product') {
            $('.rzcps-product-category-group').show();
        }else if(val === 'xml') {
            $('#rzcps-xml-group').show();
        } else if (val === 'stock_xml') {
            $('#rzcps-stock-xml-group').show();
        } else if (val === 'rss') {
            $('#rzcps-rss-group').show();
        }
    }
    toggleDataSourceFields();
    $('#rzcps-post-type-select').on('change', toggleDataSourceFields);


    $('#rzcps-icon-remove-btn').on('click', function() {
        $('#rzcps-icon-input').val('');
        $('#rzcps-icon-preview').hide();
        $(this).hide();
    });

    $('.rzcps-tab-btn').on('click', function () {
        var target = $(this).data('tab');
        $('.rzcps-tab-btn').removeClass('active');
        $(this).addClass('active');
        $('.rzcps-tab-content').removeClass('active');
        $('#tab-' + target).addClass('active');

        // Ensure vertical/horizontal dependent fields are toggled when switching tabs
        if (target === 'styling') {
            toggleThumbnailSizeInput();
        }
    });


    // Toggle ticker title input visibility based on scroll type
    function toggleTickerTitleInput() {
        var scrollType = $('select[name="scroller_settings_rzcps[scroll_type]"]').val();
        if (scrollType === 'horizontal') {
            $('.rzcps-horizontal-group').show();
        } else {
            $('.rzcps-horizontal-group').hide();
        }
    }

    // Initial check on page load
    toggleTickerTitleInput();

    // Update on scroll type change
    $('select[name="scroller_settings_rzcps[scroll_type]"]').on('change', function () {
        toggleTickerTitleInput();
    });

    $('#rzcps-reset-styling').on('click', function () {
        var scrollType = $('select[name="scroller_settings_rzcps[scroll_type]"]').val();

        var defaults = (scrollType === 'vertical') ? {
            'bg_color': '#ffffff',
            'title_background_color': '#1E8005',
            'title_color': '#ffffff',
            'item_title_color': '#333333',
            'content_color': '#555555',
            'date_color': '#777777',
            'title_size': '14',
            'item_title_size': '13',
            'content_size': '12',
            'date_size': '10',
            'xml_link_color': '#0066cc',
            'xml_link_size': '12',
            'thumbnail_width': '60',
            'thumbnail_height': '60',
            'old_price_color': '#d9534f',
            'new_price_color': '#1E8005',
            'old_price_size': '12',
            'new_price_size': '12',
            'product_regular_price_color': '#666666',
            'product_sale_price_color': '#c0392b',
            'product_regular_price_size': '12',
            'product_sale_price_size': '12',
            'padding_top': '0',
            'padding_right': '0',
            'padding_bottom': '0',
            'padding_left': '0',
            'title_weight': '500',
            'item_title_weight': '500',
            'content_weight': '400',
            'date_weight': '400',
            'xml_link_weight': '400',
            'old_price_weight': '400',
            'new_price_weight': '400',
            'product_regular_price_weight': '400',
            'product_sale_price_weight': '400',

        } : {
            'bg_color': '#f3f3f3',
            'title_background_color': '#1E8005',
            'title_color': '#ffffff',
            'item_title_color': '#333333',
            'content_color': '#555555',
            'date_color': '#777777',
            'title_size': '14',
            'item_title_size': '13',
            'content_size': '12',
            'date_size': '10',
            'xml_link_color': '#0066cc',
            'xml_link_size': '12',
            'icon_width': '15',
            'icon_height': '15',
            'old_price_color': '#d9534f',
            'new_price_color': '#1E8005',
            'old_price_size': '12',
            'new_price_size': '12',
            'product_regular_price_color': '#666666',
            'product_sale_price_color': '#c0392b',
            'product_regular_price_size': '12',
            'product_sale_price_size': '12',
            'padding_top': '0',
            'padding_right': '0',
            'padding_bottom': '0',
            'padding_left': '0',
            'title_weight': '500',
            'item_title_weight': '500',
            'content_weight': '400',
            'date_weight': '400',
            'xml_link_weight': '400',
            'old_price_weight': '400',
            'new_price_weight': '400',
            'product_regular_price_weight': '400',
            'product_sale_price_weight': '400',
        };

        // Loop through defaults
        for (const key in defaults) {
            var value = defaults[key];
            var selector;

            // Styling keys are inside the 'styling' array except icon_width, thumbnail_width etc.
            if (['icon_width', 'icon_height', 'thumbnail_width', 'thumbnail_height'].includes(key)) {
                selector = '[name="scroller_settings_rzcps[' + key + ']"]';
            } else {
                selector = '[name="scroller_settings_rzcps[styling][' + key + ']"]';
            }

            var $el = $(selector);
            if ($el.length) {
                if ($el.is('select') || $el.prop('tagName') === 'SELECT') {
                    $el.val(value).trigger('change');
                } else {
                    $el.val(value);
                }
            }
        }

        $('input[name="scroller_settings_rzcps[show_item_icon]"]').prop('checked', true);
        $('input[name="scroller_settings_rzcps[show_post_image]"]').prop('checked', false);
        $('input[name="scroller_settings_rzcps[show_ticker_title]"]').prop('checked', true);
        $('input[name="scroller_settings_rzcps[show_discount_percent]"]').prop('checked', true);
        $('input[name="scroller_settings_rzcps[show_date]"]').prop('checked', false);
    });

    $('select[name="scroller_settings_rzcps[scroll_type]"]').on('change', function () {
        toggleTickerTitleInput();
        toggleIconInput();
    });

    function toggleThumbnailSizeInput() {
        var scrollType = $('select[name="scroller_settings_rzcps[scroll_type]"]').val();
        if (scrollType === 'vertical') {
            $('.rzcps-vertical-group').show();
        } else {
            $('.rzcps-vertical-group').hide();
        }
    }
    toggleThumbnailSizeInput();
    $('select[name="scroller_settings_rzcps[scroll_type]"]').on('change', toggleThumbnailSizeInput);

});