<?php
namespace DOZENT_LMS_CERTIFICATE;

use Dompdf\Dompdf;
use Dompdf\Options;

defined( 'ABSPATH' ) || exit;

/**
 * Class PostType
 *
 * @package Dozent
 */

class Certificate_Download {

	private $debug = false;

	public function __construct() {
		add_action( 'course_stat_after', [ $this, 'course_stat_after' ] );
		add_action( 'save_post_dozent_certificate', [ $this, 'test_certificate' ] );

		add_action( 'dozent_certificate_download', [ $this, 'download_course_certificate' ] );
	}

	/**
	 * Course state, like course completed box.
	 *
	 */

	public function course_stat_after(){
		if ( ! is_user_logged_in() ) {
			return;
		}
		if ( ! dozent_is_completed_course() ) {
			return;
		}

		$show_download_btn = apply_filters( 'show_course_home_certificate_download_btn', true );

		if ( $show_download_btn ) {
			$course_id = get_the_ID();

			$url = add_query_arg( [ 'dozent_action' => 'dozent_certificate_download', 'course_id' => $course_id ] );

			echo "<a href='{$url}' class='dozent-mt-3 dozent-d-block'> <i class='dicon-download-alt'></i> " . __( 'Download Certificate', 'dozent-lms-certificate' ) . " </a> ";
		}
	}

	/**
	 * Test Certificate, enable debug mode
	 *
	 */

	public function test_certificate( $post_ID ) {
		if ( isset( $_POST['test_dozent_certificate'] ) ) {
			$this->debug = true;

			$content = $this->load_certificate_content( $post_ID ) ;
			$this->generate_PDF( $content );
		}
	}

	/**
	 * Load HTML certificate, generate HTML content and pass it to PDF generator.
	 *
	 */

	public function load_certificate_content( $certificate_id, $course_id = 0, $student_id = 0 ) {
		$certificate_background_url = get_the_post_thumbnail_url( $certificate_id, 'full' );

		ob_start();
		include DLC_ABSPATH . 'core/views/certificate.php';
		$content = ob_get_clean();

		return $content;
	}

	/**
	 * Generate PDF
	 *
	 */

	public function generate_PDF( $content = null ){
		if ( ! $content ) {
			return;
		}

		// include DOMPDF autoloader
		require_once DLC_ABSPATH . 'vendor/autoload.php';

		$options = new Options( apply_filters( 'certificate_dompdf_options', array(
			'defaultFont'             => 'sans',
			'isRemoteEnabled'         => true,
			'isFontSubsettingEnabled' => true,
			// HTML5 parser requires iconv
			'isHtml5ParserEnabled'    => extension_loaded( 'iconv' ) ? true : false,
		) ) );

		$dompdf = new Dompdf( $options );

		$dompdf->loadHtml( $content );
		$dompdf->setPaper( 'A4', 'landscape' );
		$dompdf->render();

		if ( $this->debug ) {
			$dompdf->stream( "certificate.pdf", array( "Attachment" => false ) );
			exit( 0 );
		}
		$dompdf->stream( "certificate.pdf" );
	}

	/**
	 * Download or stream Live Certificate
	 *
	 */

	public function download_course_certificate(){
		$course_id = (int) dozent_input_text( 'course_id' );
		$course_id = apply_filters( 'download_certificate_course_id', $course_id );

		$certificate_id = (int) get_post_meta( $course_id, '_certificate_id', true );
		if ( ! $certificate_id ) {
			$certificate_id = (int) get_option( 'default_certificate_id' );
		}

		if ( ! $course_id ) {
			wp_die( __( 'Course Not found', 'dozent-lms-certificate' ) );
		}

		$certificate_content = $this->load_certificate_content( $certificate_id, $course_id );
		$this->generate_PDF( $certificate_content );
	}


}