<?php

/**
 * Handle download requests for wiaas documents
 *
 * Class Wiaas_Document_Download
 */
class Wiaas_Document_Download  {

	public static function init() {
		// register download trigger for downloads from admin panel
		if ( isset($_GET['wiaasdoc']) ) {
			add_action( 'init', array( __CLASS__, 'admin_download' ) );
		}
	}

	/**
	 * Since only REST api uses JWT Authentication this endpoint will work only for users
	 * that are logged in directly to wordpress
	 */
	public static function admin_download() {

		if (!is_user_logged_in()) {
			wp_die(	__( 'No Access.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 403 ) );
		}

		$document_id = (int)$_GET['wiaasdoc'];
		if ($document_id <= 0) {
			wp_die(	__( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
		}

		$version = isset($_GET['v']) ? (int)$_GET['v'] : null;

		self::download_document($document_id, $version);
	}

	/**
	 * Download latest or specific document version
	 * @param $document_id
	 * @param null $version_id
	 */
	public static function download_document($document_id, $version_id = null) {
		$version = Wiaas_Document::get_doc_version($document_id, $version_id);

		$file_path = wiaas_get_document_version_path($version);

		if (!file_exists($file_path)) {
			wp_die(	__( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) );
		}

		WC_Download_Handler::download_file_force(
			$file_path,
			pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION )
		);
	}

	/**
	 * Download document related to order item
	 *
	 * @param $order_id
	 * @param $item_id
	 * @param $type
	 * @param $document_key
	 */
	public static function download_order_item_document($order_id, $item_id, $type, $document_key) {
		$order = wc_get_order($order_id);
		if (!$order) {
			wp_die(	__( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
		}

		$item = $order->get_item($item_id);
		if (!$item) {
			wp_die(	__( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
		}

		$item_documents = wiaas_get_standard_package_order_item_documents($order, $item_id);

		$order_document = null;
		foreach ($item_documents as $item_document) {
			if ($item_document['key'] === $document_key && $item_document['type'] === $type) {
				$order_document = $item_document;
				break;
			}
		}

		if (!isset($order_document)) {
			wp_die(	__( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
		}

		$file_path = wiaas_get_document_version_path($order_document['version']);

		if (!file_exists($file_path)) {
			wp_die(	__( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) );
		}

		WC_Download_Handler::download_file_force(
			$file_path,
			pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION )
		);

	}

	/**
	 * Download cart document
	 *
	 * @param $item_key
	 * @param $type
	 * @param $document_key
	 */
	public static function download_cart_document($item_key, $type, $document_key) {
		$item = WC()->cart->get_cart_item($item_key);
		if (!isset($item)) {
			wp_die(	__( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
		}

		$item_documents = $item['_wiaas_documents'];
		if (!isset($item_documents) ||
		    empty($item_documents) ||
		    !array_key_exists($type, $item_documents)) {
			wp_die(	__( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
		}

		$cart_document = $item_documents[$type];
		if ($cart_document['key'] !== $document_key) {
			wp_die(	__( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
		}

		$file_path = wiaas_get_document_version_path($cart_document['version']);

		if (!file_exists($file_path)) {
			wp_die(	__( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) );
		}

		WC_Download_Handler::download_file_force(
			$file_path,
			pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION )
		);

	}

}

Wiaas_Document_Download::init();
