/**
 * MediaTasker — Frontend Core
 *
 * Handles workspace initialization, view toggling, and status filtering.
 *
 * @package MediaTasker
 */

/* global jQuery, mediat_ajax_obj */

(function ($) {
    'use strict';

    $(document).ready(function () {
        var $workspace = $('#media-viewport');

        // Exit early if the workspace isn't on this page.
        if (!$workspace.length) {
            return;
        }

        /* ==================================================================
           View Toggling
           ================================================================== */
        var $viewSwitchers = $('.mediat_switch-btn');
        var $originalUploadPanel = $('.mediat_upload-primary');
        var $editedUploadPanel = $('.mediat_upload-secondary');
        var $summaryOriginal = $('#mediat_summary-original');
        var $summaryEdited = $('#mediat_summary-edited');
        var $globalActionsOriginal = $('#mediat_actions-original');
        var $globalActionsEdited = $('#mediat_actions-edited');

        var $gridOriginal = $('#mediat_grid-original');
        var $gridEdited = $('#mediat_grid-edited');

        $viewSwitchers.on('click', function () {
            var $btn = $(this);
            if ($btn.hasClass('is-active')) return;

            $viewSwitchers.removeClass('is-active');
            $btn.addClass('is-active');

            var view = $btn.data('view');

            if (view === 'original') {
                $summaryOriginal.show();
                $summaryEdited.hide();
                $globalActionsOriginal.show();
                $globalActionsEdited.hide();
                $gridOriginal.show();
                $gridEdited.hide();
                $originalUploadPanel.show();
                $editedUploadPanel.hide();
            } else {
                $summaryOriginal.hide();
                $summaryEdited.show();
                $globalActionsOriginal.hide();
                $globalActionsEdited.show();
                $gridOriginal.hide();
                $gridEdited.show();
                $originalUploadPanel.hide();
                $editedUploadPanel.show();
            }
        });

        /* ==================================================================
           Status Filtering (UI Toggle)
           ================================================================== */
        var $statusBlocks = $('.mediat_status-block');
        var $assetCards = $('.mediat_asset-card');

        $statusBlocks.on('click', function () {
            var $block = $(this);
            var status = $block.data('status');
            var view = $block.closest('.mediat_metric-group').attr('id').includes('original') ? 'original' : 'edited';

            // Toggle selection state
            $block.toggleClass('is-selected');

            // Find all selected statuses for the current view
            var selectedStatuses = [];
            $('#mediat_summary-' + view + ' .mediat_status-block.is-selected').each(function () {
                selectedStatuses.push($(this).data('status'));
            });

            // Filter cards
            if (selectedStatuses.length === 0) {
                // Show all if nothing selected
                $assetCards.show();
                $('.mediat_date-section').show();
            } else {
                // Show only cards matching selected statuses
                $assetCards.hide();
                selectedStatuses.forEach(function (st) {
                    // Extensible: premium code can override via window.MEDIAT_StatusFilter
                    if (typeof window.MEDIAT_StatusFilter === 'function' && window.MEDIAT_StatusFilter(st)) {
                        return; // Handled by custom filter
                    }
                    $('.mediat_asset-card.status-' + st).show();
                });

                // Hide empty date sections
                $('.mediat_date-section').each(function () {
                    var $section = $(this);
                    if ($section.find('.mediat_asset-card:visible').length === 0) {
                        $section.hide();
                    } else {
                        $section.show();
                    }
                });
            }
        });

        /* Selection logic has been moved to mediat_floating-menu.js */
    });
})(jQuery);
