<?php

namespace GriffinForms\Admin\Js\Local;

class License extends \GriffinForms\Admin\Js\Local\Settings
{
    protected function localJs()
    {
        return
            $this->generalJs() . PHP_EOL .
            $this->printErrorsJs() . PHP_EOL .
            $this->dataStatusJs() . PHP_EOL .
            $this->licenseVarsJs() . PHP_EOL .
            $this->licenseHelpersJs() . PHP_EOL .
            $this->licenseEditToggleJs() . PHP_EOL .
            $this->licenseActivationJs() . PHP_EOL;
    }

    protected function licenseVarsJs(): string
    {
        return 'const licenseSurface = ' . wp_json_encode($this->data['surface'] ?? []) . ';' . PHP_EOL .
            'const freemiusSurface = licenseSurface.freemius || {};' . PHP_EOL;
    }

    protected function licenseHelpersJs(): string
    {
        return 'function gfLicenseSetSuccess(message) {' . PHP_EOL .
            '    $(".griffinforms-settings-notice-success p").text(message);' . PHP_EOL .
            '    $(".griffinforms-settings-notice-success").show();' . PHP_EOL .
            '    window.scrollTo({ top: 0, behavior: "smooth" });' . PHP_EOL .
            '}' . PHP_EOL .
            'function gfLicenseSetError(message) {' . PHP_EOL .
            '    $(".griffinforms-settings-notice-errors").html("<ul><li>" + message + "</li></ul>").show();' . PHP_EOL .
            '    window.scrollTo({ top: 0, behavior: "smooth" });' . PHP_EOL .
            '}' . PHP_EOL .
            'function gfLicenseParseError(response) {' . PHP_EOL .
            '    if (!response) {' . PHP_EOL .
            '        return "' . esc_js($this->lang->getText('license_activation_unexpected')) . '";' . PHP_EOL .
            '    }' . PHP_EOL .
            '    if (typeof response === "string") {' . PHP_EOL .
            '        return response;' . PHP_EOL .
            '    }' . PHP_EOL .
            '    if (response.error && response.error.message) {' . PHP_EOL .
            '        return response.error.message;' . PHP_EOL .
            '    }' . PHP_EOL .
            '    if (response.message) {' . PHP_EOL .
            '        return response.message;' . PHP_EOL .
            '    }' . PHP_EOL .
            '    if (response.msg) {' . PHP_EOL .
            '        return response.msg;' . PHP_EOL .
            '    }' . PHP_EOL .
            '    return "' . esc_js($this->lang->getText('license_activation_unexpected')) . '";' . PHP_EOL .
            '}' . PHP_EOL;
    }

    protected function licenseEditToggleJs(): string
    {
        return '$("#griffinforms-settings-license-edit-btn").on("click", function() {' . PHP_EOL .
            '    var $input = $("#griffinforms-settings-license-licensekey");' . PHP_EOL .
            '    $input.prop("readonly", false).prop("disabled", false).val("").focus();' . PHP_EOL .
            '    $("#griffinforms-settings-license-update-btn").prop("disabled", false).text("' . esc_js($this->lang->getText('update_license')) . '");' . PHP_EOL .
            '    $(this).hide();' . PHP_EOL .
            '});' . PHP_EOL;
    }

    protected function licenseActivationJs(): string
    {
        return '$("#griffinforms-settings-license-update-btn").on("click", function() {' . PHP_EOL .
            '    removeNotices();' . PHP_EOL .
            '    if (!licenseSurface.can_activate) {' . PHP_EOL .
            '        gfLicenseSetError("' . esc_js($this->lang->getText('activation_unavailable')) . '");' . PHP_EOL .
            '        return;' . PHP_EOL .
            '    }' . PHP_EOL .
            '    var $input = $("#griffinforms-settings-license-licensekey");' . PHP_EOL .
            '    var licenseKey = $.trim($input.val());' . PHP_EOL .
            '    if (licenseKey === "") {' . PHP_EOL .
            '        gfLicenseSetError("' . esc_js($this->lang->getText('license_key_required')) . '");' . PHP_EOL .
            '        $input.focus();' . PHP_EOL .
            '        return;' . PHP_EOL .
            '    }' . PHP_EOL .
            '    sendingRequest();' . PHP_EOL .
            '    $.post(ajaxurl, {' . PHP_EOL .
            '        action: freemiusSurface.activation_ajax_action,' . PHP_EOL .
            '        _wpnonce: freemiusSurface.activation_ajax_nonce,' . PHP_EOL .
            '        license_key: licenseKey' . PHP_EOL .
            '    }).done(function(response) {' . PHP_EOL .
            '        if (response && response.success) {' . PHP_EOL .
            '            gfLicenseSetSuccess("' . esc_js($this->lang->getText('license_activation_success')) . '");' . PHP_EOL .
            '            window.setTimeout(function() { window.location.reload(); }, 1200);' . PHP_EOL .
            '            return;' . PHP_EOL .
            '        }' . PHP_EOL .
            '        gfLicenseSetError(gfLicenseParseError(response));' . PHP_EOL .
            '        unlockUi();' . PHP_EOL .
            '    }).fail(function(xhr) {' . PHP_EOL .
            '        var message = "' . esc_js($this->lang->getText('license_activation_unexpected')) . '";' . PHP_EOL .
            '        if (xhr && xhr.responseJSON) {' . PHP_EOL .
            '            message = gfLicenseParseError(xhr.responseJSON);' . PHP_EOL .
            '        } else if (xhr && xhr.responseText) {' . PHP_EOL .
            '            try {' . PHP_EOL .
            '                message = gfLicenseParseError(JSON.parse(xhr.responseText));' . PHP_EOL .
            '            } catch (e) {' . PHP_EOL .
            '            }' . PHP_EOL .
            '        }' . PHP_EOL .
            '        gfLicenseSetError(message);' . PHP_EOL .
            '        unlockUi();' . PHP_EOL .
            '    });' . PHP_EOL .
            '});' . PHP_EOL;
    }
}
