<?php
/**
 * The FANCIFY Image component contains a set of functions to edit images on the fly.
 *
 * Edited images are duplicates of the originals. All modified images are stored in a shared folder,
 * which makes it easy to delete them without impacting the originals.
 *
 * @package SplendorStudio\Framework\API\Image
 */
class Fancify_Lite_Image extends Fancify_Lite_Controller {
	/**
	 * Construct class
	 *
	 * @since    1.0.0
	 */
	protected function __construct() {}

	/**
	 * Register callbacks.
	 *
	 * @since    1.0.0
	 */
	protected function register_hook_callbacks() {}

	/**
	 * Edit post attachment.
	 *
	 * This function is shortcut of {@see edit_image_with_url()}. It should be used to edit a post attachment.
	 *
	 * @since 1.0.0
	 *
	 * @param string $post_id     The post id.
	 * @param array  $args        An array of editor arguments
	 *
	 * @return object Edited post attachment data.
	 */
	public static function edit_attachment_media( $attache_id, $attache_url, $args = array() ) {
		if ( ! defined( 'FANCIFYCORE_PATH' ) ) {
			return $attache_url;
		}

		require_once FANCIFYCORE_PATH . '/models/image/editor.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase

		$image = new FancifyCore\Models\Image\FancifyCore_Models_Image_Editor;

		try {
			// resize
			$out = $image->resize( $attache_id, $attache_url, $args[0], $args[1] );

			return $out['url'];
		} catch( Exception $err ) {
			// handle error
			return $err->getMessage(); 
		}
	}

	/**
	 * Edit post attachment.
	 *
	 * This function is shortcut of {@see edit_image_with_url()}. It should be used to edit a post attachment.
	 *
	 * @since 1.0.0
	 *
	 * @param string $post_id     The post id.
	 * @param array  $args        An array of editor arguments
	 *
	 * @return object Edited post attachment data.
	 */
	public static function edit_attachment_by_postid( $post_id, $args = array() ) {
		if ( ! has_post_thumbnail( $post_id ) ) {
			return false;
		}

		// Get full size image.
		$attachment = self::get_post_attachment( $post_id, 'full' );
		$edited     = self::edit_image_with_url( $attachment->src, $args, 'ARRAY_A' );

		if ( ! $edited ) {
			return $attachment;
		}

		return (object) array_merge( (array) $attachment, $edited );
	}

	/**
	 * Get attachment data.
	 *
	 * This function regroups all necessary data about a post attachment into an object.
	 *
	 * @since 1.0.0
	 *
	 * @param string $post_id The post id.
	 * @param string $size    Optional.
	 *
	 * @return object Post attachment data.
	 */
	public static function get_post_attachment( $post_id, $size = 'full' ) {
		$id   = get_post_thumbnail_id( $post_id );
		$post = get_post( $id );
		$src  = wp_get_attachment_image_src( $id, $size );

		$obj              = new stdClass();
		$obj->id          = $id;
		$obj->src         = isset( $src[0] ) ? $src[0] : null;
		$obj->width       = isset( $src[1] ) ? $src[1] : null;
		$obj->height      = isset( $src[2] ) ? $src[2] : null;
		$obj->alt         = trim( strip_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ) ) );
		$obj->title       = isset( $post->post_title ) ? $post->post_title : '';
		$obj->caption     = isset( $post->post_excerpt ) ? $post->post_excerpt : '';
		$obj->description = isset( $post->post_content ) ? $post->post_content : '';

		return $obj;
	}

	/**
	 * Edit image size and/or quality.
	 *
	 * @since 1.0.0
	 *
	 * @param string $src         The image source.
	 * @param array  $args        An array of editor arguments
	 * @param string $output      Optional. Returned format. Accepts STRING, OBJECT, ARRAY_A, or ARRAY_N.
	 *                            Default is STRING.
	 *
	 * @return string|array Image source if output set the STRING, image data otherwise.
	 */
	public static function edit_image_with_url( $src, array $args, $output = 'STRING' ) {
		if ( ! defined( 'FANCIFYCORE_PATH' ) ) {
			return false;
		}

		require_once FANCIFYCORE_PATH . '/models/image/editor.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase

		$editor = new FancifyCore\Models\Image\FancifyCore_Models_Image_Editor;

		$editor->config( $src, $args, $output );

		return $editor->run();
	}

}
