<?php

namespace FinchCart\Admin\Settings;

use EDITH;

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

/**
 * Product Meta Class
 */
class ProductMeta {

	/**
	 * Class Constructor.
	 */
	public function __construct() {
		add_action( 'after_setup_theme', array( $this, 'adding_product_meta' ) );
		add_filter( 'edith_shortcode_builder_enqueue_assets', array( $this, 'adding_edith_scripts' ) );
	}

	/**
	 * Check for adding Edith scripts
	 *
	 * @param boolean $enqueue Check if enqueue is required.
	 * @return boolean
	 */
	public function adding_edith_scripts( $enqueue ) {
		$screen = get_current_screen();

		if ( isset( $screen ) && 'post' === $screen->base && 'product' === $screen->id ) {
			$enqueue = true;
		}
		return $enqueue;
	}

	/**
	 * Adding Custom Product Meta for choosing custom recommended products
	 *
	 * @return void
	 */
	public function adding_product_meta() {

		$prefix                             = 'finch_cart_product_meta_';
		$settings                           = get_option( 'finchcart_settings_' );
		$individual_product_recommendations = isset( $settings['finchcart_individual_product_recommendations'] ) ? wc_string_to_bool( $settings['finchcart_individual_product_recommendations'] ) : '';

		EDITH::createMetabox(
			$prefix,
			array(
				'title'     => esc_html__( 'Finch Cart', 'finch-cart-product-recommendations' ),
				'post_type' => 'product',
				'data_type' => 'serialize',
				'class'     => 'edith-regular-meta', // required for styling.
				'theme'     => 'light',
			)
		);

		if ( true === $individual_product_recommendations ) {
			EDITH::createSection(
				$prefix,
				array(
					'title'       => esc_html__( 'Recommendations', 'finch-cart-product-recommendations' ),
					'description' => esc_html__( 'These options allows you set product recommendations specifically for this product only.', 'finch-cart-product-recommendations' ),
					'icon'        => 'fas fa-link',
					'fields'      => array(
						array(
							'id'       => 'finchcart_specific_product_recommendations',
							'type'     => 'button_set',
							'title'    => esc_html__( 'Show Recommendations', 'finch-cart-product-recommendations' ),
							'subtitle' => esc_html__( 'If you prefer not to display any recommended items for this product, you can use this option. Default option will follow the plugin settings value.', 'finch-cart-product-recommendations' ),
							'options'  => array(
								'default' => esc_html__( 'Default', 'finch-cart-product-recommendations' ),
								'yes'     => esc_html__( 'Yes', 'finch-cart-product-recommendations' ),
								'no'      => esc_html__( 'No', 'finch-cart-product-recommendations' ),
							),
							'default'  => 'default',
						),
						array(
							'id'         => 'finchcart_override_product_recommendations',
							'type'       => 'switcher',
							'title'      => esc_html__( 'Override Recommendations', 'finch-cart-product-recommendations' ),
							'subtitle'   => esc_html__( 'This option allows you to turn on or off the default recommendations override for this product.', 'finch-cart-product-recommendations' ),
							'dependency' => array( 'finchcart_specific_product_recommendations', '!=', 'no' ),
						),
						array(
							'id'         => 'finchcart_specific_product_recommendations_type',
							'type'       => 'button_set',
							'title'      => esc_html__( 'Recommendation Type', 'finch-cart-product-recommendations' ),
							'subtitle'   => esc_html__( 'You may either select particular goods or use categories and other filters to narrow down recommended products for this product.', 'finch-cart-product-recommendations' ),
							'options'    => array(
								'manual'  => esc_html__( 'Manual Selection', 'finch-cart-product-recommendations' ),
								'dynamic' => esc_html__( 'Dynamic Selection', 'finch-cart-product-recommendations' ),
							),
							'default'    => 'manual',
							'dependency' => array(
								array( 'finchcart_override_product_recommendations', '==', 'true' ),
								array( 'finchcart_specific_product_recommendations', '!=', 'no' ),
							),
						),
						array(
							'id'         => 'finchcart_specific_product_manual_recommendations',
							'type'       => 'product_select',
							'title'      => esc_html__( 'Manual Product Selection', 'finch-cart-product-recommendations' ),
							'subtitle'   => esc_html__( 'Select the products that you want to display as recommended products for this product.', 'finch-cart-product-recommendations' ),
							'dependency' => array(
								array( 'finchcart_override_product_recommendations', '==', 'true' ),
								array( 'finchcart_specific_product_recommendations_type', '==', 'manual' ),
								array( 'finchcart_specific_product_recommendations', '!=', 'no' ),
							),
						),
						array(
							'id'          => 'finchcart_specific_categories',
							'type'        => 'select',
							'title'       => esc_html__( 'Select Product Categories', 'finch-cart-product-recommendations' ),
							'placeholder' => esc_html__( 'Select product categories', 'finch-cart-product-recommendations' ),
							'chosen'      => true,
							'multiple'    => true,
							'sortable'    => true,
							'ajax'        => true,
							'options'     => 'categories',
							'query_args'  => array(
								'taxonomy' => 'product_cat',
							),
							'dependency'  => array(
								array( 'finchcart_override_product_recommendations', '==', 'true' ),
								array( 'finchcart_specific_product_recommendations_type', '==', 'dynamic' ),
								array( 'finchcart_specific_product_recommendations', '!=', 'no' ),
							),
						),
						array(
							'id'          => 'finchcart_specific_tags',
							'type'        => 'select',
							'title'       => esc_html__( 'Select Product Tags', 'finch-cart-product-recommendations' ),
							'placeholder' => esc_html__( 'Select product tags', 'finch-cart-product-recommendations' ),
							'chosen'      => true,
							'multiple'    => true,
							'sortable'    => true,
							'ajax'        => true,
							'options'     => 'categories',
							'query_args'  => array(
								'taxonomy' => 'product_tag',
							),
							'dependency'  => array(
								array( 'finchcart_override_product_recommendations', '==', 'true' ),
								array( 'finchcart_specific_product_recommendations_type', '==', 'dynamic' ),
								array( 'finchcart_specific_product_recommendations', '!=', 'no' ),
							),
						),
						array(
							'id'         => 'finchcart_specificl_additional_options',
							'type'       => 'fieldset',
							'class'      => 'edith-fieldset-inline',
							'title'      => esc_html__( 'Additional Settings', 'finch-cart-product-recommendations' ),
							'subtitle'   => esc_html__( 'These extra options let you narrow down and reorder the recommended products.', 'finch-cart-product-recommendations' ),
							'dependency' => array(
								array( 'finchcart_override_product_recommendations', '==', 'true' ),
								array( 'finchcart_specific_product_recommendations', '!=', 'no' ),
							),
							'fields'     => array(
								array(
									'id'          => 'finchcart_specific_products_orderby',
									'type'        => 'select',
									'title'       => esc_html__( 'Products Order By', 'finch-cart-product-recommendations' ),
									'placeholder' => esc_html__( 'Select an option', 'finch-cart-product-recommendations' ),
									'options'     => array(
										'rand'       => esc_html__( 'Random Order', 'finch-cart-product-recommendations' ),
										'date'       => esc_html__( 'Publish Date', 'finch-cart-product-recommendations' ),
										'price'      => esc_html__( 'Price', 'finch-cart-product-recommendations' ),
										'popularity' => esc_html__( 'Popularity (sales)', 'finch-cart-product-recommendations' ),
										'rating'     => esc_html__( 'Product Rating', 'finch-cart-product-recommendations' ),
										'menu'       => esc_html__( 'Menu Order', 'finch-cart-product-recommendations' ),
										'title'      => esc_html__( 'Title', 'finch-cart-product-recommendations' ),
									),
									'default'     => 'rand',
								),
								array(
									'id'          => 'finchcart_specific_products_order',
									'type'        => 'select',
									'title'       => esc_html__( 'Products Order', 'finch-cart-product-recommendations' ),
									'placeholder' => esc_html__( 'Select an option', 'finch-cart-product-recommendations' ),
									'options'     => array(
										'ASC'  => esc_html__( 'Ascending Order', 'finch-cart-product-recommendations' ),
										'DESC' => esc_html__( 'Descending Order', 'finch-cart-product-recommendations' ),
									),
									'default'     => 'DESC',
								),
								array(
									'id'          => 'finchcart_specific_products_type',
									'type'        => 'select',
									'title'       => esc_html__( 'Products Type', 'finch-cart-product-recommendations' ),
									'placeholder' => esc_html__( 'Select an option', 'finch-cart-product-recommendations' ),
									'options'     => array(
										'simple'       => esc_html__( 'Simple product', 'finch-cart-product-recommendations' ),
										'downloadable' => esc_html__( ' → Downloadable', 'finch-cart-product-recommendations' ),
										'virtual'      => esc_html__( ' → Virtual', 'finch-cart-product-recommendations' ),
										'grouped'      => esc_html__( 'Grouped product', 'finch-cart-product-recommendations' ),
										'external'     => esc_html__( 'External/Affiliate product', 'finch-cart-product-recommendations' ),
										'variable'     => esc_html__( 'Variable product', 'finch-cart-product-recommendations' ),
									),
								),
								array(
									'id'          => 'finchcart_specific_products_stock_status',
									'type'        => 'select',
									'title'       => esc_html__( 'Products Stock Status', 'finch-cart-product-recommendations' ),
									'placeholder' => esc_html__( 'Select an option', 'finch-cart-product-recommendations' ),
									'options'     => array(
										'instock'     => esc_html__( 'In stock', 'finch-cart-product-recommendations' ),
										'outofstock'  => esc_html__( 'Out of stock', 'finch-cart-product-recommendations' ),
										'onbackorder' => esc_html__( 'On backorder', 'finch-cart-product-recommendations' ),
									),
								),
								array(
									'id'    => 'finchcart_specific_on_sale_products_only',
									'type'  => 'switcher',
									'title' => esc_html__( 'On-Sale Products Only', 'finch-cart-product-recommendations' ),
								),
								array(
									'id'    => 'finchcart_specific_featured_products_only',
									'type'  => 'switcher',
									'title' => esc_html__( 'Featured Products Only', 'finch-cart-product-recommendations' ),
								),
								array(
									'id'      => 'finchcart_specific_numbers_of_products',
									'type'    => 'slider',
									'title'   => esc_html__( 'Numbers of Products', 'finch-cart-product-recommendations' ),
									'default' => 12,
									'max'     => 100,
									'step'    => 1,
								),
							),
						),
					),
				)
			);
		}

		EDITH::createSection(
			$prefix,
			array(
				'title'       => esc_html__( 'Heading', 'finch-cart-product-recommendations' ),
				'description' => esc_html__( 'By default, a global heading will be displayed for all products. You can use these options to add product-specific headings.', 'finch-cart-product-recommendations' ),
				'icon'        => 'fas fa-heading',
				'fields'      => array(
					array(
						'id'       => 'finchcart_show_heading',
						'type'     => 'button_set',
						'title'    => esc_html__( 'Show Heading', 'finch-cart-product-recommendations' ),
						'subtitle' => esc_html__( 'This option allows you to turn on or off the heading in the popup for this product. Default option will follow the plugin settings value.', 'finch-cart-product-recommendations' ),
						'options'  => array(
							'default' => esc_html__( 'Default', 'finch-cart-product-recommendations' ),
							'yes'     => esc_html__( 'Yes', 'finch-cart-product-recommendations' ),
							'no'      => esc_html__( 'No', 'finch-cart-product-recommendations' ),
						),
						'default'  => 'default',
					),
					array(
						'id'       => 'finchcart_override_heading',
						'type'     => 'switcher',
						'title'    => esc_html__( 'Override Heading', 'finch-cart-product-recommendations' ),
						'subtitle' => esc_html__( 'This option allows you to turn on or off the default heading override for this product.', 'finch-cart-product-recommendations' ),
						'default'  => true,
					),
					array(
						'id'            => 'finchcart_default_heading_text',
						'title'         => esc_html__( 'Default Heading', 'finch-cart-product-recommendations' ),
						'subtitle'      => esc_html__( 'Use the default heading if you want to use the same heading format for all recommendation. For the product title, use pattern %title%. You may change the pattern [item, items] to suit your preferences. You can change the pattern [item, items] to suit your needs based on the recommended singular and plural products.', 'finch-cart-product-recommendations' ),
						'type'          => 'wp_editor',
						'height'        => '100px',
						'media_buttons' => false,
						'default'       => esc_html__( 'You may purchase the following [item, items] with the %title%', 'finch-cart-product-recommendations' ),
						'dependency'    => array(
							array( 'finchcart_show_heading', '!=', 'no' ),
							array( 'finchcart_override_heading', '==', 'true' ),
						),
					),
					array(
						'id'            => 'finchcart_default_subheading_text',
						'title'         => esc_html__( 'Default Heading Description', 'finch-cart-product-recommendations' ),
						'subtitle'      => esc_html__( 'You can add your heading description text here.', 'finch-cart-product-recommendations' ),
						'type'          => 'wp_editor',
						'height'        => '100px',
						'media_buttons' => false,
						'dependency'    => array(
							array( 'finchcart_show_heading', '!=', 'no' ),
							array( 'finchcart_override_heading', '==', 'true' ),
						),
					),
				),
			)
		);
	}
}
