<?php
namespace Prodigy\Includes\Support\Addons\Elementor\Builder\Documents;

use Elementor\Controls_Manager;
use ElementorPro\Modules\ThemeBuilder\Documents\Single_Base;
use ElementorPro\Plugin;
use Prodigy\Includes\Prodigy;

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

class Product extends Single_Base {

	public static function get_properties() {
		$properties = parent::get_properties();

		$properties['location']       = 'single';
		$properties['condition_type'] = Prodigy::PRODIGY_POST_TYPE_DEFAULT;

		return $properties;
	}

	public function get_name() {
		return 'product-prodigy';
	}

	public static function get_type() {
		return 'product-prodigy';
	}

	protected static function get_site_editor_type() {
		return 'product-prodigy';
	}

	public static function get_title() {
		return __( 'Prodigy Product', 'prodigy-commerce' );
	}

	public static function get_plural_title() {
		return __( 'Prodigy Products', 'prodigy-commerce' );
	}

	protected static function get_site_editor_icon() {
		return 'eicon-single-product';
	}

	protected static function get_site_editor_tooltip_data() {
		return array(
			'title'     => __( 'What is a Single Product Template?', 'prodigy-commerce' ),
			'content'   => __( 'A single product template allows you to easily design the layout and style of Prodigy single product pages, and apply that template to various conditions that you assign.', 'prodigy-commerce' ),
			'tip'       => __( 'You can create multiple single product templates, and assign each to different types of products, enabling a custom design for each group of similar products.', 'prodigy-commerce' ),
			'docs'      => 'https://go.elementor.com/app-theme-builder-product',
			'video_url' => 'https://www.youtube.com/embed/PjhoB1RWkBM',
		);
	}

	public function get_depended_widget() {
		return Plugin::elementor()->widgets_manager->get_widget_types( 'woocommerce-product-data-tabs' );
	}

	public function get_container_attributes() {
		$attributes = parent::get_container_attributes();

		$attributes['class'] .= ' product';

		return $attributes;
	}

	public function __construct( array $data = array() ) {
		parent::__construct( $data );
	}

	protected function register_controls() {
		parent::register_controls();
		if ( $this->get_controls( 'preview_settings' ) ) {
			return;
		}
		$this->add_control(
			'preview_type',
			array(
				'label'       => esc_html__( 'Preview Dynamic Content as', 'prodigy-commerce' ),
				'label_block' => true,
				'type'        => Controls_Manager::SELECT,
				'default'     => $this::get_preview_as_default(),
				'groups'      => $this::get_preview_as_options(),
				'export'      => false,
			)
		);

		$latest_posts = get_posts(
			array(
				'posts_per_page' => 1,
				'post_type'      => 'product',
			)
		);

		if ( ! empty( $latest_posts ) ) {
			$this->update_control(
				'preview_id',
				array(
					'default' => $latest_posts[0]->ID,
				)
			);
		}
		$this->end_controls_section();

		$this->inject_html_tag_control();
	}

	private function inject_html_tag_control() {
		$wrapper_tags = $this->get_wrapper_tags();

		if ( ! $wrapper_tags ) {
			return;
		}

		// Check if control is already registered to prevent redeclaration
		if ( $this->get_controls( 'content_wrapper_html_tag' ) ) {
			return;
		}

		array_unshift( $wrapper_tags, 'div' );

		$this->start_injection(
			array(
				'of'       => 'post_status',
				'fallback' => array(
					'of' => 'post_title',
				),
			)
		);

		$this->add_control(
			'content_wrapper_html_tag',
			array(
				'label'   => esc_html__( 'HTML Tag', 'prodigy-commerce' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'div',
				'options' => array_combine( $wrapper_tags, $wrapper_tags ),
			)
		);

		$this->end_injection();
	}

	protected function get_remote_library_config() {
		$config = parent::get_remote_library_config();

		$config['category'] = 'prodigy product';

		return $config;
	}

	/**
	 * Fix for thumbnail name that is different from editor type.
	 *
	 * @return string
	 */
	protected static function get_site_editor_thumbnail_url() {
		return ELEMENTOR_ASSETS_URL . 'images/app/site-editor/product.svg';
	}

	protected static function get_editor_panel_categories() {
		$categories = array(
			'prodigy-elements-single' => array(
				'title' => __( 'Prodigy Product', 'prodigy-commerce' ),

			),
			// Move to top as active.
			'prodigy-elements'        => array(
				'title'  => __( 'Prodigy', 'prodigy-commerce' ),
				'active' => true,
			),
		);

		$categories += parent::get_editor_panel_categories();

		unset( $categories['theme-elements-single'] );

		return $categories;
	}
}
