<?php

namespace Prodigy\Includes\Support\Addons\Elementor\Builder\Conditions;

use Elementor\Controls_Manager;
use ElementorPro\Modules\ThemeBuilder;
use Prodigy\Includes\Models\Prodigy_Products;

class Prodigy_Product_Names extends ThemeBuilder\Conditions\Condition_Base {

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

	protected function register_controls() {
		$this->add_control(
			'prodigy_product_names',
			array(
				'section'  => 'settings',
				'type'     => Controls_Manager::SELECT2,
				'multiple' => false,
				'options'  => Prodigy_Products::get_products_by_type( Prodigy_Products::PRODUCTS_NAMES ),
				'default'  => array(),
			)
		);
	}

	public function get_name() {
		return 'prodigy_product_names';
	}

	public static function get_priority() {
		return 40;
	}

	public function get_label() {
		return __( 'In Product Names', 'prodigy-commerce' );
	}

	/**
	 * @param array $args
	 * @return bool
	 */
	public function check( $args ) {
		$is_product = false;
		if ( isset( $args['id'] ) && is_single() ) {
			global $post;
			$title   = get_post( $post )->post_title;
			$product = prodigy_get_post_by_title( $args['id'], \Prodigy\Includes\Prodigy::get_prodigy_product_type() );
			if ( ! empty( $product ) ) {
				$is_product = $title == $product->post_title;
			}
		}

		return $is_product;
	}
}
