<?php

/**
 * Utilities class for Formidable Forms Google Sheet Connector
 *
 * @since 1.0
 */
if (!defined('ABSPATH')) {
  exit; // Exit if accessed directly.
}

/**
 * Utilities class - singleton class
 *
 * @since 1.0
 */
class Gscfrmdform_Connector_Free_Utility
{

  public function __construct()
  {
    // Do Nothing
  }



  /**
   * Get the singleton instance of the Gscfrmdform_Connector_Free_Utility class
   *
   * @return singleton instance of Gscfrmdform_Connector_Free_Utility
   */
  public static function instance()
  {
    static $instance = null;
    if (is_null($instance)) {
      $instance = new Gscfrmdform_Connector_Free_Utility();
    }
    return $instance;
  }

  /**
   * Prints message (string or array) in the debug.log file
   *
   * @param mixed $message
   */
  public function logger($message) {
      if (WP_DEBUG === true) {
         if (is_array($message) || is_object($message)) {
            error_log(print_r($message, true));
         } else {
            error_log($message);
         }
      }
   }


  /**
   * Display error or success message in the admin section
   *
   * @param array $data containing type and message.
   * @return string with HTML containing the error message
   *
   * @since 1.0
   */
  public function admin_notice($data = array())
  {
    // extract message and type from the $data array
    $message = isset($data['message']) ? $data['message'] : "";
    $message_type = isset($data['type']) ? $data['type'] : "";
    switch ($message_type) {
      case 'error':
        $admin_notice = '<div id="message" class="error notice is-dismissible">';
        break;
      case 'update':
        $admin_notice = '<div id="message" class="updated notice is-dismissible">';
        break;
      case 'update-nag':
        $admin_notice = '<div id="message" class="update-nag">';
        break;
      case 'upgrade':
        $admin_notice = '<div id="message" class="error notice edds-gs-upgrade is-dismissible">';
        break;
      case 'auth-expired-notice':
        $admin_notice = '<div id="message" class="error notice formidable_form-gs-auth-expired-adds is-dismissible">';
        break;
      default:
        $message = __('There\'s something wrong with your code...', 'gsheetconnector-formidable-forms');
        $admin_notice = "<div id=\"message\" class=\"error\">\n";
        break;
    }
    $admin_notice .= '<p>' . wp_kses_post($message) . '</p>';
    $admin_notice .= "</div>\n";
    return $admin_notice;
  }
  /**
   * Utility function to get the current user's role
   *
   * @since 1.0
   */
  public function get_current_user_role()
  {
    global $wp_roles;
    foreach ($wp_roles->role_names as $role => $name):
      if (current_user_can($role)) {
        return $role;
      }
    endforeach;
  }


  /**
   * Utility function to get the current user's role
   */
  public function get_system_roles()
  {
    $participating_roles = array();
    $editable_roles = get_editable_roles();
    foreach ($editable_roles as $role => $details) {
      $participating_roles[$role] = $details['name'];
    }
    return $participating_roles;
  }


  /**
   * Fetch and save Auto Integration API credentials
   *
   * @since 1.0.9
   */
  public function save_api_credentials()
  {
    // Create a nonce
    $nonce = wp_create_nonce('formidablegsc_api_creds');

    // Prepare parameters for the API call
    $params = array(
      'action' => 'get_data',
      'nonce' => $nonce,
      'plugin' => 'FORMIDABLEGSC',
      'method' => 'get',
    );

    // Add nonce and any other security parameters to the API request
    $api_url = add_query_arg($params, GSCFRMDFORM_VERSION_API_URL);

    // Make the API call using wp_remote_get
    $response = wp_remote_get($api_url);

    // Check for errors
    if (is_wp_error($response)) {
      // Handle error
      self::frmgsc_debug_log_file(__METHOD__ . ' Error: ' . $response->get_error_message());
    } else {
      // API call was successful, process the data
      $response = wp_remote_retrieve_body($response);

      $decoded_response = json_decode($response);

      if (isset($decoded_response->api_creds) && (!empty($decoded_response->api_creds))) {
        $api_creds = wp_parse_args($decoded_response->api_creds);
        if (is_multisite()) {
          // If it's a multisite, update the site option (network-wide)
          update_site_option('formidablegsc_api_creds', $api_creds);
        } else {
          // If it's not a multisite, update the regular option
          update_option('formidablegsc_api_creds', $api_creds);
        }
      }
    }
  }

  /**
   * Connected Forms function
   */
  public function get_forms_connected_to_formidable_forms()
  {
    global $wpdb;

    // Check if cache exists.
    $cached_forms = wp_cache_get('frm_connected_forms');

    if ($cached_forms === false) {
      // Cache not found, fetch from database.
      // phpcs:ignore
      $query = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'postmeta AS pm WHERE pm.meta_key = "frm_entry_meta"');
      $cached_forms = $query;

      // Store in cache for future use.
      wp_cache_set('frm_connected_forms', $cached_forms);
    }

    return $cached_forms;
  }
  /**
   * Utility function to get the current user's role
   *
   * @since 1.0
   */
  public static function frmgsc_debug_log_file($error)
  {


    if (!function_exists('WP_Filesystem')) {
      require_once ABSPATH . 'wp-admin/includes/file.php';
    }
    global $wp_filesystem;
    if (!WP_Filesystem()) {
      return;
    }

    $upload_dir = wp_upload_dir();
    $log_dir = trailingslashit($upload_dir['basedir']) . 'gsc-formidable-logs/';
    $log_file = get_option('frmgsc_debug_log_file');
    $timestamp = gmdate('Y-m-d H:i:s') . "\t PHP " . phpversion() . "\t";

    try {
      if (!$wp_filesystem->is_dir($log_dir)) {
        $wp_filesystem->mkdir($log_dir, FS_CHMOD_DIR);
      }

      // Protect directory with .htaccess
      $wp_filesystem->put_contents($log_dir . '.htaccess', "Deny from all\n", FS_CHMOD_FILE);

      $old_file = $log_dir . 'log.txt';
      if ($wp_filesystem->exists($old_file)) {
        $wp_filesystem->delete($old_file);
      }

      $log_message = is_array($error) || is_object($error)
        ? $timestamp . wp_json_encode($error) . "\r\n"
        : $timestamp . $error . "\r\n";

      if (!empty($log_file) && $wp_filesystem->exists($log_file)) {
        $existing = $wp_filesystem->get_contents($log_file);
        $wp_filesystem->put_contents($log_file, $existing . $log_message, FS_CHMOD_FILE);
      } else {
        $new_log_file = $log_dir . 'log-' . uniqid() . '.txt';
        $log_content = "Log created at " . gmdate('Y-m-d H:i:s') . "\r\n" . $log_message;

        if ($wp_filesystem->put_contents($new_log_file, $log_content, FS_CHMOD_FILE)) {
          update_option('frmgsc_debug_log_file', $new_log_file);
        }
      }

    } catch (Exception $e) {

      Gscfrmdform_Connector_Free_Utility::frmgsc_debug_log_file('Exception in gs_debug_log:  "' . $e->getMessage());

    }


  }
}
