jQuery(function() {

    jQuery('.pwlpar-offer-unavailable-button').on('click', function(e) {
        e.preventDefault();
        return false;
    });

    pwlparOfferBindClaimOfferButtons();

    jQuery('.pwlpar-offer-claim-cancel-button').on('click', function(e) {
        var offer = jQuery(this).closest('.pwlpar-offer');
        offer.find('.pwlpar-offer-confirm-buttons, .pwlpar-offer-confirm-info').hide();

        offer.find('.pwlpar-offer-claim-button, .pwlpar-offer-info').show();

        e.preventDefault();
    });

    jQuery('.pwlpar-offer-confirm-button').on('click', function(e) {
        e.preventDefault();

        if (jQuery(this).hasClass('pwlpar-button-unavailable')) {
            return;
        }

        var confirmButton = jQuery(this);
        var confirmButtonOriginalText = confirmButton.text();
        var offerContainer = confirmButton.closest('.pwlpar-offer');
        var offerId = offerContainer.attr('data-id');
        confirmButton.on('click', function () { return false; });
        confirmButton.prop('disabled', true);
        confirmButton.text(pwlparo.i18n.claimingOffer);

        jQuery.post(pwlparo.ajaxurl, {'action': 'pwlpar-claim_offer', 'offer_id': offerId, 'security': pwlparo.nonces.claimOffer }, function(result) {
            if (result.success) {
                if (result.data.url) {
                    window.location.href = result.data.url;
                } else if ( result.data.limitResetSeconds ) {
                    jQuery('#pwlpar-header-text-title').text(result.data.balance);
                    confirmButton.text(confirmButtonOriginalText);
                    offerContainer.find('.pwlpar-offer-claim-cancel-button').trigger('click');

                    var claimOfferButton = offerContainer.find('.pwlpar-offer-claim-button');
                    var claimOfferButtonOriginalText = claimOfferButton.text();

                    claimOfferButton.addClass('pwlpar-offer-unavailable-button');
                    claimOfferButton.addClass('pwlpar-button-unavailable');

                    pwlparOfferStartTimer(result.data.limitResetSeconds, claimOfferButton, claimOfferButtonOriginalText);

                } else {
                    window.location.reload();
                }

            } else {
                confirmButton.text(confirmButtonOriginalText);
                alert(result.data.message);
            }
        }).fail(function(xhr, textStatus, errorThrown) {
            confirmButton.text(confirmButtonOriginalText);
            if (errorThrown) {
                alert(errorThrown);
            } else {
                alert(pwlparo.i18n.error + ' claimOfferButton');
            }

        });
    });

    jQuery('.pwlpar-offer-terms-link').on('click', function(e) {
        var offer = jQuery(this).closest('.pwlpar-offer');
        offer.find('.pwlpar-offer-terms').toggle();
        jQuery(this).trigger('blur');
        e.preventDefault();
        return false;
    });

    jQuery('.pwlpar-offer-category').on('click', function(e) {
        jQuery('.pwlpar-offer-category').removeClass('pwlpar-offer-category-active');
        jQuery(this).addClass('pwlpar-offer-category-active');

        var categoryId = jQuery(this).attr('data-id');
        if (categoryId != 0) {
            jQuery('.pwlpar-offer').hide();
            jQuery('.pwlpar-offer[data-category-id="' + categoryId + '"]').show();
        } else {
            jQuery('.pwlpar-offer').show();
        }

        e.preventDefault();
        return false;
    });
});

function pwlparOfferBindClaimOfferButtons() {
    jQuery('.pwlpar-offer-claim-button').off('click').on('click', function(e) {
        e.preventDefault();

        if (jQuery(this).hasClass('pwlpar-button-unavailable')) {
            return;
        }

        var offer = jQuery(this).closest('.pwlpar-offer');
        offer.find('.pwlpar-offer-claim-button, .pwlpar-offer-info').hide();

        offer.find('.pwlpar-offer-confirm-buttons').css('display', 'flex');
        offer.find('.pwlpar-offer-confirm-info').show();
    });
}

function pwlparOfferStartTimer(duration, claimOfferButton, originalText) {

    var days = parseInt(duration / 86400, 10);
    var hours = parseInt((duration % 86400) / 3600, 10);
    var minutes = parseInt((duration % 3600) / 60, 10);
    var seconds = parseInt(duration % 60, 10);

    var text = '';

    if (days > 0) {
        text = days + ':' + String(hours).padStart(2, '0') + ':' + String(minutes).padStart(2, '0');
    } else if (hours > 0 ) {
        text = hours + ':' + String(minutes).padStart(2, '0');
    } else {
        text = minutes;
    }

    text = text + ':' + String(seconds).padStart(2, '0');

    claimOfferButton.text(text);

    if (--duration < 0) {
        claimOfferButton.text(originalText);
        claimOfferButton.removeClass('pwlpar-offer-unavailable-button');
        claimOfferButton.removeClass('pwlpar-button-unavailable');
        claimOfferButton.addClass('pwlpar-offer-claim-button')
        claimOfferButton.show();

        pwlparOfferBindClaimOfferButtons();
    } else {
        setTimeout(pwlparOfferStartTimer.bind(null, duration, claimOfferButton, originalText), 1000);
    }
}
