<?php
namespace Jet_Engine_Divi_Views\Components\Jet_Engine;

// Prevent direct access to the file.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Layout manager
 */
class Layout_Manager {

	protected static $base_suffix = '_jet_lg_';
	protected static $base_layout = 'jet_listing_';

	protected $view_id;
	protected $layouts_option_name = 'jet_engine_divi_layouts';
	protected $layouts = null;

	/**
	 * Constructor
	 */
	public function __construct( $view_id ) {

		$this->view_id = $view_id;

		add_filter( 'et_theme_builder_layout_types', [ $this, 'register_listing_layouts' ] );
		add_filter( 'et_builder_order_class_name_suffix_map', [ $this, 'register_listing_suffixes' ] );
		add_action( 'save_post', [ $this, 'synch_layouts' ], 0, 2 );
	}

	/**
	 * Synch layouts оп saving of listing item with Divi views
	 *
	 * @param int $post_id Post ID.
	 * @param object $post Post object.
	 */
	public function synch_layouts( $post_id, $post ) {

		if ( jet_engine()->post_type->slug() !== $post->post_type ) {
			return;
		}

		if ( jet_engine()->listings->data->get_listing_type( $post_id ) !== $this->view_id ) {
			return;
		}

		$layouts = get_option( $this->layouts_option_name, [] );

		if ( ! in_array( $post_id, $layouts ) ) {
			$layouts[] = $post_id;
		}

		update_option( $this->layouts_option_name, $layouts );

		// also cleate layout-related cache
		delete_post_meta( $post_id, self::get_layout_name( $post_id ) );
	}

	/**
	 * Get layout cache
	 *
	 * @param int $post_id Post ID.
	 *
	 * @return mixed
	 */
	public static function get_layout_cache( $post_id ) {
		return get_post_meta( $post_id, self::get_layout_name( $post_id ), true );
	}

	/**
	 * Set layout cache
	 *
	 * @param int $post_id Post ID.
	 * @param string $content Content to set.
	 */
	public static function set_layout_cache( $post_id, $content ) {
		update_post_meta( $post_id, self::get_layout_name( $post_id ), $content );
	}

	/**
	 * Get layouts
	 *
	 * @return array
	 */
	public function get_layouts() {

		if ( null === $this->layouts ) {
			$this->layouts = get_option( $this->layouts_option_name, [] );
		}

		return $this->layouts;
	}

	/**
	 * Register listing suffixes
	 *
	 * @param array $suffixes_map Suffixes map.
	 *
	 * @return array
	 */
	public function register_listing_suffixes( $suffixes_map ) {
		foreach ( $this->get_layouts() as $l_id ) {
			$suffixes_map[ self::get_layout_name( $l_id ) ] = self::get_suffix_name( $l_id );
		}

		return $suffixes_map;
	}

	/**
	 * Register listing layouts
	 *
	 * @param array $layouts Layouts list.
	 *
	 * @return array
	 */
	public function register_listing_layouts( $layouts ) {

		foreach ( $this->get_layouts() as $l_id ) {
			$layouts[] = self::get_layout_name( $l_id );
		}

		return $layouts;
	}

	/**
	 * Get layout name
	 *
	 * @param int $listing_id Listing ID.
	 *
	 * @return string
	 */
	public static function get_layout_name( $listing_id ) {
		return self::$base_layout . $listing_id;
	}

	/**
	 * Get suffix name
	 *
	 * @param int $listing_id Listing ID.
	 *
	 * @return string
	 */
	public static function get_suffix_name( $listing_id ) {
		return self::$base_suffix . $listing_id;
	}

}
