(function ($) {
    'use strict';

    /**
     * Driver availability updates.
     *
     * Sends the current toggle state to AJAX endpoint.
     */
    $(function () {
        $('.oddw-availability label.switch input[type=checkbox]').on('change', function () {
            var $checkbox = $(this);
            var userId = $checkbox.attr('id');
            var metaKey = 'oddw_driver_availability';
            var metaValue = $checkbox.is(':checked') ? 'checked' : '';
            var nonce = $checkbox.data('oddw-nonce');

            $.post(oddw_ajax.ajaxurl, {
                action: 'oddw_driver_availability_update',
                user_id: userId,
                metakey: metaKey,
                metavalue: metaValue,
                oddw_availability_nonce: nonce
            });
        });

        $('.driver-rating').each(function () {
            var $select = $(this);
            var isReadonly = $select.prop('disabled') || $select.is('[readonly]');

            if ($select.data('oddwEnhanced')) {
                return;
            }

            var initialRating = parseInt($select.data('initial-rating') || $select.val() || '0', 10);
            if (isNaN(initialRating)) {
                initialRating = 0;
            }

            var postIdParts = String($select.data('id') || '').split('_');
            var postId = postIdParts[1];
            var selectedValue = initialRating;

            var $widget = $('<div class="oddw-star-rating" role="radiogroup" aria-label="Delivery rating"></div>');
            for (var i = 1; i <= 5; i++) {
                var $star = $('<button type="button" class="oddw-star" aria-label="Rate ' + i + ' star' + (i > 1 ? 's' : '') + '" data-value="' + i + '">&#9733;</button>');
                if (isReadonly) {
                    $star.prop('disabled', true).attr('aria-disabled', 'true');
                }
                $widget.append($star);
            }

            function paintStars(value) {
                $widget.find('.oddw-star').each(function () {
                    var $star = $(this);
                    var starValue = parseInt($star.data('value'), 10);
                    $star.toggleClass('is-active', starValue <= value);
                });
            }

            $select.after($widget).hide().attr('aria-hidden', 'true');
            $select.data('oddwEnhanced', true);
            paintStars(initialRating);

            if (isReadonly) {
                return;
            }

            $widget.on('click', '.oddw-star', function () {
                var value = parseInt($(this).data('value'), 10);
                if (isNaN(value)) {
                    return;
                }

                selectedValue = value;
                $select.val(String(value)).trigger('change');
                paintStars(value);
            });

            $widget.on('mouseenter', '.oddw-star', function () {
                var value = parseInt($(this).data('value'), 10);
                if (isNaN(value)) {
                    return;
                }
                paintStars(value);
            });

            $widget.on('mouseleave', function () {
                paintStars(selectedValue);
            });
        });
    });
})(jQuery);
