<?php

/*
 * This file is part of the Fancy Bricks package.
 *
 * (c) Joshua Gugun Siagian <suabahasa@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare (strict_types=1);
namespace Fancy\Animations\Api\Admin;

use Fancy\Animations\Api\AbstractApi;
use Fancy\Animations\Api\ApiInterface;
use Fancy\Animations\Plugin;
use _FancyAnimations\FANCY_ANIMATIONS;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
/**
 * @since 1.0.0
 */
class License extends AbstractApi implements ApiInterface
{
    public function __construct()
    {
    }
    public function get_prefix() : string
    {
        return 'admin/license';
    }
    public function register_custom_endpoints() : void
    {
        \register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/index', ['methods' => WP_REST_Server::READABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->index($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]);
        \register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/store', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->store($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]);
    }
    public function index(WP_REST_Request $wprestRequest) : WP_REST_Response
    {
        return new WP_REST_Response(['license' => $this->get_license()]);
    }
    public function store(WP_REST_Request $wprestRequest) : WP_REST_Response
    {
        $payload = $wprestRequest->get_json_params();
        $new_license_key = \sanitize_text_field($payload['license']);
        $old_license = \get_option(FANCY_ANIMATIONS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]);
        $old_license_message = new \stdClass();
        try {
            $old_license_message = \json_decode(\get_option(FANCY_ANIMATIONS::WP_OPTION . '_license_message', '{}'), null, 512, \JSON_THROW_ON_ERROR);
        } catch (\JsonException $e) {
        }
        $notice = [];
        if ($new_license_key !== $old_license['key']) {
            if ($old_license['key'] !== '') {
                if (isset($old_license_message->data->instance->id)) {
                    try {
                        Plugin::get_instance()->maybe_update_plugin()->deactivate_license($old_license['key'], $old_license_message->data->instance->id);
                    } catch (\Throwable $throwable) {
                    }
                }
                \delete_option(FANCY_ANIMATIONS::WP_OPTION . '_license_message');
            }
            if ($new_license_key !== '') {
                try {
                    Plugin::get_instance()->maybe_update_plugin()->activate_license($new_license_key);
                    $notice['success'] = 'Plugin license key stored successfully';
                } catch (\Throwable $throwable) {
                }
            } else {
                $notice['success'] = 'Plugin license key de-activated successfully';
            }
        }
        \update_option(FANCY_ANIMATIONS::WP_OPTION . '_license', ['key' => $new_license_key, 'opt_in_pre_release' => \false]);
        return new WP_REST_Response(['license' => $this->get_license(), 'notice' => $notice]);
    }
    private function permission_callback(WP_REST_Request $wprestRequest) : bool
    {
        return \wp_verify_nonce($wprestRequest->get_header('X-WP-Nonce'), 'wp_rest') && \current_user_can('manage_options');
    }
    private function get_license() : array
    {
        $license = \get_option(FANCY_ANIMATIONS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]);
        $license['is_activated'] = \false;
        try {
            $license_message = \json_decode(\get_option(FANCY_ANIMATIONS::WP_OPTION . '_license_message', '{}'), null, 512, \JSON_THROW_ON_ERROR);
            if (isset($license_message->data->activated)) {
                if ($license_message->data->activated) {
                    $limit = empty($license_message->data->license_key->activation_limit) ? '∞' : $license_message->data->license_key->activation_limit;
                    $license['message'] = \sprintf('You have %s/%s instances activated.', $license_message->data->license_key->activation_usage, $limit);
                    $license['is_activated'] = \true;
                } else {
                    $license['message'] = $license_message->error ?: 'License for this site is not active.';
                }
            }
        } catch (\Throwable $throwable) {
            //throw $th;
        }
        return $license;
    }
}
