<?php

namespace FinchCart\Admin\Edith\includes\ShortcodeBuilder;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Shortcode Builder Class
 */
class ShortcodeBuilder {
	/**
	 * Builder version
	 *
	 * @var string
	 */
	public static $version = '1.0';

	/**
	 * Enqueue builder scripts
	 *
	 * @var boolean
	 */
	public static $enqueue = false;

	/**
	 * Builder class inited
	 *
	 * @var array
	 */
	public static $inited = array();

	/**
	 * Builder options
	 *
	 * @var array
	 */
	public static $args = array(
		'shortcode_builder_options' => array(),
	);

	/**
	 * Class constructor.
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'setup' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_enqueue_scripts' ) );
	}

	/**
	 * Enqueue admin and fields styles and scripts
	 *
	 * @return void
	 */
	public static function add_admin_enqueue_scripts() {
		global $typenow;

		if ( ! self::$enqueue ) {
			$wpscreen = get_current_screen();
		}

		if ( ! empty( self::$args['shortcode_builder_options'] ) ) {
			foreach ( self::$args['shortcode_builder_options'] as $argument ) {
				// For the admin page.
				if ( substr( $wpscreen->id, -strlen( $argument['menu_slug'] ) ) === $argument['menu_slug'] ) {
					self::$enqueue = true;
				}

				// For the sub pages.
				if ( isset( $argument['sub_menu_items'] ) ) {
					foreach ( $argument['sub_menu_items'] as $sub_menu_item ) {
						if ( substr( $wpscreen->id, -strlen( $sub_menu_item ) ) === $sub_menu_item ) {
							self::$enqueue = true;
						}
					}
				}

				// For the edit post page.
				if ( isset( $argument['post_type'] ) && isset( $typenow ) && $typenow === $argument['post_type'] ) {
					self::$enqueue = true;
				}
			}
		}

		if ( ! apply_filters( 'edith_shortcode_builder_enqueue_assets', self::$enqueue ) ) {
			return;
		}
		wp_enqueue_style( 'edith-shortcode-builder-style', plugin_dir_url( __FILE__ ) . '../../assets/css/edith-shortcode-builder.css', array(), self::$version );
		wp_enqueue_style( 'edith-csf-override-style', plugin_dir_url( __FILE__ ) . '../../assets/css/edith-csf-override.css', array(), self::$version );

		wp_enqueue_script( 'edith-jbox', plugins_url( '../../assets/jbox/jBox.min.js', __FILE__ ), array( 'jquery' ), self::$version, true );
		wp_enqueue_style( 'edith-jbox', plugins_url( '../../assets/jbox/jBox.min.css', __FILE__ ), array(), self::$version );

		wp_enqueue_script( 'edith-shortcode-builder-script', plugin_dir_url( __FILE__ ) . '../../assets/js/edith-shortcode-builder.js', array( 'jquery', 'wp-util', 'wp-i18n' ), '1.0', true );
		wp_set_script_translations( 'edith-shortcode-builder-script', 'finch-cart-product-recommendations' );
	}

	/**
	 * Setup framework
	 *
	 * @return void
	 */
	public static function setup() {
		$params = array();
		if ( ! empty( self::$args['shortcode_builder_options'] ) ) {
			foreach ( self::$args['shortcode_builder_options'] as $key => $value ) {
				if ( ! isset( self::$inited[ $key ] ) ) {
					$params['args']       = $value;
					self::$inited[ $key ] = true;
					ShortCodeBuilderOptions::instance( $key, $params );
					if ( isset( $params['args']['post_type'] ) ) {
						ShortCodeBuilderPostTypeOptions::instance( $key, $params );
					}
				}
			}
		}
	}

	/**
	 * Create shortcode builder
	 *
	 * @param string $id A unique prefix string for shortcode builder.
	 * @param array  $args Shortcode builder options.
	 * @return void
	 */
	public static function createShortcodeBuilder( $id, $args = array() ) {
		self::$args['shortcode_builder_options'][ $id ] = $args;
	}
}
