<?php


namespace Elex\MultiVendor\Controller;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

use Elex\MultiVendor\Model\HelpAndSupport;
use Elex\MultiVendor\Model\Browser;


class HelpAndSupportController {


	public static function init() {
		add_action( 'elex_mv_help_and_support_tab_frequent_questions', array( self::class, 'load_faqs_help_and_support' ) );
		add_action( 'elex_mv_help_and_support_tab_tickets', array( self::class, 'load_tickets_help_and_support' ) );
		self::download_info();
	}

	public static function get_current_tab() {
		$tab = HelpAndSupport::get_param( 'tab' );
		return ! empty( $tab ) ? $tab : self::get_default_tab();
	}

	public static function get_default_tab() {
		/** 
		 * Fire a filter hook to get default tab
		 *
		 * @since 1.0.0
		 */
		return apply_filters( 'elex_mv_admin_support', 'frequent_questions' );
	}

	public static function load_faqs_help_and_support() {
		HelpAndSupport::include_file( 'templates/admin-help&support/FAQs.php' );
	}


	public static function load_tickets_help_and_support() {

		HelpAndSupport::include_file( 'templates/admin-help&support/Tickets.php' );
	}


	public static function display_output( $browser, $theme, $host, $WP_REMOTE_POST ) {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'elex-multi-vendor-for-woocommerce' ) );
		}

		global $wpdb;
		ob_start();

		$path = ELEX_MULTI_VENDOR_PLUGIN_PATH . '/templates/admin-help&support/output.php';
		/** 
		 * Fire a filter hook for changing path
		 *
		 * @since 1.0.0
		 * @param $path
		 */
		$path = apply_filters( 'elex_mv_ssi_view_path_output', $path );
		include $path ;

		return ob_get_clean();
	}
	public static function let_to_num( $v ) {
		$l   = substr( $v, -1 );
		$ret = substr( $v, 0, -1 );
  
		switch ( strtoupper( $l ) ) {
			case 'P': // fall-through
			case 'T': // fall-through
			case 'G': // fall-through
			case 'M': // fall-through
			case 'K': // fall-through
				$ret *= 1024;
				break;
			default:
				break;
		}
  
		return $ret;
	}
	public static function display() {
	  

		$browser = new Browser();
		if ( get_bloginfo( 'version' ) < '3.4' ) {
			$theme_data = wp_get_theme( get_stylesheet_directory() . '/style.css' );
			$theme      = $theme_data['Name'] . ' ' . $theme_data['Version'];
		} else {
			$theme_data = wp_get_theme();
			$theme      = $theme_data->Name . ' ' . $theme_data->Version;
		}
  
		// Try to identify the hosting provider
		$host = false;
		if ( defined( 'WPE_APIKEY' ) ) {
			$host = 'WP Engine';
		} elseif ( defined( 'PAGELYBIN' ) ) {
			$host = 'Pagely';
		}
  
		$request['cmd'] = '_notify-validate';
  
		$params = array(
			'sslverify' => false,
			'timeout'   => 60,
			'body'      => $request,
		);
  
		$response = wp_remote_post( 'https://www.paypal.com/cgi-bin/webscr', $params );
  
		if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
			$WP_REMOTE_POST = 'wp_remote_post() works' . "\n";
		} else {
			$WP_REMOTE_POST = 'wp_remote_post() does not work' . "\n";
		}
	  
		return self::display_output( $browser, $theme, $host, $WP_REMOTE_POST );
	}
	public  static function download_info() {

		if ( ! isset( $_POST['system_info_nonce'] ) ) {
			return;
		}
  
		if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['system_info_nonce'] ) ), 'action' ) ) {
			return;
		}
  
		if ( isset( $_POST['action'] ) ) {
			if ( 'download_system_info' !== sanitize_text_field( wp_unslash( $_POST['action'] ) ) ) {
				return;
			}
		}
  
		if ( ! isset( $_POST['send-system-info-textarea'] ) || empty( $_POST['send-system-info-textarea'] ) ) {
			return;
		}
  
  
		header( 'Content-type: text/plain' );
		//Text file name marked with Unix timestamp
		header( 'Content-Disposition: attachment; filename=system_info_' . time() . '.txt' );
		echo ( wp_kses_post( wp_unslash( $_POST['send-system-info-textarea'] ) ) );
		die();
	}
}
