<?php

/**
 * Integration Service class for GsheetConnector-jetforms License operations
 * @since 1.0.0 
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

/**
 * GSHFJF_JetForm_Integration_Service Class
 * @since 1.0.0
 */

class GSHFJF_JetForm_Integration_Service
{
    /**
     * Register AJAX actions for jetForm Google Sheets integration.
     *
     * @access public
     * @since 1.0.0
     */

    public function __construct()
    {
        // Verify integration
        add_action('wp_ajax_gshfjf_verify_integration', array($this, 'gshfjf_verify_integration'));

        // Deactivate integration
        add_action('wp_ajax_gshfjf_deactivate_integration', array($this, 'gshfjf_deactivate_integration'));

        //Save auth method
        add_action('wp_ajax_gshfjf_save_auth_method', array($this, 'gshfjf_save_auth_method'));
    }

    /**
     * LOGGER
     * @since 1.0.0
     */

    public function logger($message)
    {
        if (defined('WP_DEBUG') && WP_DEBUG === true) {
            if (is_object($message) && method_exists($message, 'getMessage')) {
                GSHFJF_JetForm_Error_Logs::gshfjf_log_from_debug($message->getMessage());
            } else {
                GSHFJF_JetForm_Error_Logs::gshfjf_log_from_debug($message);
            }
        }
    }

    /**
     * Saves the selected authentication method for Google Sheets.
     *
     * @access public
     * @since 1.0.0
     */

    public function gshfjf_save_auth_method()
    {
        check_ajax_referer('gshfjf-ajax-nonce', 'security');
        if (current_user_can('manage_options')) {
            $method = isset($_POST['auth_method']) ? sanitize_text_field(wp_unslash($_POST['auth_method'])) : '0';
            update_option('gshfjf_jetform_manual_setting', $method);
            wp_send_json_success(['saved' => true]);
        }
        wp_send_json_error(['error' => 'Permission denied']);
    }

    /**
     * AJAX function - verifies the token
     *
     * @param string $Code The code to be verified.
     *
     * @since 1.0.0
     */

    public function gshfjf_verify_integration($Code = '')
    {
        try {
            // nonce checksave_gs_settings
            check_ajax_referer('gshfjf-ajax-nonce', 'security');

            /* sanitize incoming data */
            $Code = isset($_POST['code']) ? sanitize_text_field(wp_unslash($_POST['code'])) : '';

            if (!empty($Code)) {
                update_option('gshfjf_jetform_access_code', $Code);
            } else {
                return;
            }

            if (get_option('gshfjf_jetform_access_code') != '') {
                include_once GSHFJF_JETFORM_CONNECTOR_ROOT . '/lib/google-sheets.php';
                GSHFJF_JetForm_GoogleSheet::preauth(get_option('gshfjf_jetform_access_code'));
                update_option('gshfjf_validation_manual_setting', 'valid');
                wp_send_json_success();
            } else {
                update_option('gshfjf_jetform_verify', 'invalid');
                wp_send_json_error();
            }
        } catch (Exception $e) {
            throw new LogicException("Auth, Error fetching OAuth2 access token, message: " . esc_html($e->getMessage()));
            exit();
        }
    }

    /**
     * AJAX function - deactivate activation
     *
     * @since 1.0.0
     */

    public function gshfjf_deactivate_integration()
    {
        check_ajax_referer('gshfjf-ajax-nonce', 'security');

        if (get_option('gshfjf_jetform_token') != '') {
            delete_option('gshfjf_jetform_token');
            delete_option('gshfjf_jetform_access_code');
            delete_option('gshfjf_jetform_verify');
            wp_send_json_success();
        } else {
            wp_send_json_error();
        }
    }
}
$GSHFJF_JetForm_Integration_Service = new GSHFJF_JetForm_Integration_Service();
