<?php

use IGD\App;

defined( 'ABSPATH' ) || exit;

function igd_pro_share_file( $file, $user_data ) {

	if ( empty( $file['accountId'] ) || empty( $file['id'] ) || empty( $user_data['email'] ) ) {
		return false;
	}

	$account_id = $file['accountId'];
	$service    = App::instance( $account_id )->getService();

	$permission = new \IGDGoogle_Service_Drive_Permission();
	$permission->setEmailAddress( sanitize_email( $user_data['email'] ) );
	$permission->setType( 'user' );
	$permission->setRole( 'reader' );

	// Prepare email message
	$email_message = sprintf(
		esc_html__(
			'Hello %s, you have been granted access to the file "%s". You can now view and download the contents of this file. If you have any questions or need further assistance, please do not hesitate to contact us.',
			'integrate-google-drive-premium'
		),
		esc_html( $user_data['name'] ),
		esc_html( $file['name'] )
	);

	$params = [
		'supportsAllDrives'     => true,
		'sendNotificationEmail' => true,
		'emailMessage'          => $email_message,
	];

	try {
		$service->permissions->create( $file['id'], $permission, $params );

		return true;
	} catch ( \Exception $e ) {
		error_log( 'IGD Share file error: ' . $e->getMessage() );

		return false;
	}

}

function igd_pro_get_all_parent_folders( $file ) {
	$list = [];

	$app = App::instance( $file['accountId'] );

	// Check if file has parents
	if ( ! empty( $file['parents'] ) ) {
		foreach ( $file['parents'] as $parent_id ) {
			$parent_folder = $app->get_file_by_id( $parent_id );

			// Check if retrieved parent folder is indeed a directory
			if ( igd_is_dir( $parent_folder ) ) {
				$list[] = $parent_folder;

				// Recursively get parents of the parent folder
				$parent_folders = igd_pro_get_all_parent_folders( $parent_folder );
				$list           = array_merge( $list, $parent_folders );
			}
		}
	}

	return $list;
}

function igd_pro_get_scheduled_interval( $hook ) {
	$schedule  = wp_get_schedule( $hook );
	$schedules = wp_get_schedules();

	return ! empty( $schedules[ $schedule ] ) ? $schedules[ $schedule ]['interval'] : false;
}

if ( ! function_exists( 'tutor_get_template' ) ) {
	function tutor_get_template( $template = null, $tutor_pro = false ) {
		if ( ! $template ) {
			return false;
		}

		$template = str_replace( '.', DIRECTORY_SEPARATOR, $template );

		/**
		 * Get template first from child-theme if exists
		 * If child theme not exists, then get template from parent theme
		 */
		$template_location = trailingslashit( get_stylesheet_directory() ) . "tutor/{$template}.php";
		if ( ! file_exists( $template_location ) ) {
			$template_location = trailingslashit( get_template_directory() ) . "tutor/{$template}.php";
		}
		$file_in_theme = $template_location;
		if ( ! file_exists( $template_location ) ) {
			$template_location = trailingslashit( tutor()->path ) . "templates/{$template}.php";

			if ( $tutor_pro && function_exists( 'tutor_pro' ) ) {
				$pro_template_location = trailingslashit( tutor_pro()->path ) . "templates/{$template}.php";
				if ( file_exists( $pro_template_location ) ) {
					$template_location = trailingslashit( tutor_pro()->path ) . "templates/{$template}.php";
				}
			}

			//Integrate Google Drive Templates
			if ( ! file_exists( $template_location ) ) {
				$template_location = trailingslashit( IGD_PRO_INCLUDES ) . "/integrations/templates/tutor/{$template}.php";
			}

			if ( ! file_exists( $template_location ) ) {
				$warning_msg = __( 'The file you are trying to load does not exist in your theme or Tutor LMS plugin location. If you are extending the Tutor LMS plugin, please create a php file here: ', 'integrate-google-drive-premium' );
				$warning_msg = $warning_msg . "<code>$file_in_theme</code>";
				$warning_msg = apply_filters( 'tutor_not_found_template_warning_msg', $warning_msg );
				echo wp_kses( $warning_msg, array( 'code' => true ) );
				?>
				<?php
			}
		}

		return apply_filters( 'tutor_get_template_path', $template_location, $template );
	}
}
