<?php
namespace CSSIgniter\Ignition\Elementor\Widget;

class Featured_Product_Category extends \Elementor\Widget_Base {
	public function get_style_depends() {
		wp_register_style( 'ignition-elementor-featured-product-category', plugin_dir_url( __DIR__ ) . 'assets/css/featured-product-category.min.css', array( 'slick' ), IGNITION_ELEMENTOR_WIDGETS_VERSION );

		return array(
			'ignition-elementor-featured-product-category',
		);

	}

	public function get_script_depends() {
		wp_register_script( 'ignition-elementor-featured-product-category', plugin_dir_url( __DIR__ ) . 'assets/js/featured-product-category.js', array( 'slick' ), IGNITION_ELEMENTOR_WIDGETS_VERSION, true );

		return array(
			'ignition-elementor-featured-product-category',
		);

	}

	public function get_name() {
		return 'ignition-featured-product-category';
	}

	public function get_title() {
		return __( 'Featured Product Category', 'ignition-elementor-widgets' );
	}

	public function get_icon() {
		return 'eicon-product-categories';
	}

	public function get_categories() {
		return array( 'ignition-elementor-widgets' );

	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => __( 'Content options', 'ignition-elementor-widgets' ),
			)
		);

		$product_categories = get_terms(
			array(
				'taxonomy' => 'product_cat',
			)
		);

		$this->add_control(
			'product_category',
			array(
				'label'   => esc_html__( 'Product category', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => '',
				'options' => wp_list_pluck( $product_categories, 'name', 'term_id' ),
			)
		);

		$this->add_control(
			'handpicked_products',
			array(
				'label'       => esc_html__( 'Select products', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::SELECT2,
				'label_block' => true,
				'multiple'    => true,
				'options'     => \CSSIgniter\Ignition\Elementor\Widgets_Helper::get_all_post_type_items( 'product' ),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_layout',
			array(
				'label' => __( 'Layout', 'ignition-elementor-widgets' ),
			)
		);

		$this->add_control(
			'columns',
			array(
				'label'   => esc_html__( 'Number of columns', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 2,
						'max'  => 4,
						'step' => 1,
					),
				),
				'default' => array(
					'unit' => 'px',
					'size' => 3,
				),
			)
		);

		$this->add_control(
			'number_products',
			array(
				'label'   => esc_html__( 'Number of products', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 1,
						'max'  => 12,
						'step' => 1,
					),
				),
				'default' => array(
					'unit' => 'px',
					'size' => 3,
				),
			)
		);

		$this->add_control(
			'layout',
			array(
				'label'   => esc_html__( 'Layout', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'grid',
				'options' => array(
					'grid'   => esc_html__( 'Grid', 'ignition-elementor-widgets' ),
					'slider' => esc_html__( 'Slider', 'ignition-elementor-widgets' ),
				),
			)
		);

		$this->add_control(
			'show_category',
			array(
				'label'        => esc_html__( 'Show category', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'show_price',
			array(
				'label'        => esc_html__( 'Show price', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_rating',
			array(
				'label'        => esc_html__( 'Show rating', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_stock',
			array(
				'label'        => esc_html__( 'Show stock', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'show_add_to_cart',
			array(
				'label'        => esc_html__( 'Show add to cart button', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_category_card',
			array(
				'label'        => esc_html__( 'Show category card', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_card',
			array(
				'label' => __( 'Category card options', 'ignition-elementor-widgets' ),
			)
		);

		$this->add_control(
			'category_image',
			array(
				'label'   => esc_html__( 'Featured image', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::MEDIA,
				'default' => array(
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				),
			)
		);

		$this->add_control(
			'category_title',
			array(
				'label'       => esc_html__( 'Title', 'ignition-elementor-widgets' ),
				'placeholder' => esc_html__( 'Custom title...', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'label_block' => true,
			)
		);

		$this->add_control(
			'category_description',
			array(
				'label'       => esc_html__( 'Description', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXTAREA,
				'rows'        => 10,
				'placeholder' => esc_html__( 'Type a custom description here', 'ignition-elementor-widgets' ),
			)
		);

		$this->add_control(
			'button_text',
			array(
				'label'       => esc_html__( 'Button text', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'label_block' => true,
			)
		);

		$this->add_control(
			'content_position',
			array(
				'label'   => esc_html__( 'Content position', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'bottom-center',
				'options' => array(
					'top-left'      => esc_html__( 'Top left', 'ignition-elementor-widgets' ),
					'top-center'    => esc_html__( 'Top center', 'ignition-elementor-widgets' ),
					'top-right'     => esc_html__( 'Top right', 'ignition-elementor-widgets' ),
					'middle-left'   => esc_html__( 'Middle left', 'ignition-elementor-widgets' ),
					'middle-center' => esc_html__( 'Middle center', 'ignition-elementor-widgets' ),
					'middle-right'  => esc_html__( 'Middle right', 'ignition-elementor-widgets' ),
					'bottom-left'   => esc_html__( 'Bottom left', 'ignition-elementor-widgets' ),
					'bottom-center' => esc_html__( 'Bottom center', 'ignition-elementor-widgets' ),
					'bottom-right'  => esc_html__( 'Bottom right', 'ignition-elementor-widgets' ),
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'card_styles',
			array(
				'label' => esc_html__( 'Card colors', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'text_color',
			array(
				'label'     => esc_html__( 'Text color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ignition-elementor-widgets-featured-product-category__card p, {{WRAPPER}} .ignition-elementor-widgets-featured-product-category__card h2' => 'color: {{VALUE}}',
				),
			)
		);

		$this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			array(
				'name'     => 'card_overlay',
				'types'    => array( 'gradient' ),
				'selector' => '{{WRAPPER}} .ignition-elementor-widgets-featured-product-category__card::after',
			)
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings     = $this->get_settings();
		$columns      = $settings['columns']['size'];
		$num_products = $settings['number_products']['size'];
		$category_id  = $settings['product_category'];
		$handpicked   = $settings['handpicked_products'];

		$selected_term = $category_id ? get_term( $category_id, 'product_cat' ) : false;

		$container_classes = array(
			'ignition-elementor-widgets-featured-product-category__content',
			'row',
			'row-items',
		);

		$container_classes[] = "row-columns-{$columns}";

		$classes[] = ignition_get_columns_classes( $columns );

		if ( function_exists( 'ignition_customizer_defaults' ) ) {
			$mobile_classes = get_theme_mod( 'woocommerce_shop_mobile_columns', ignition_customizer_defaults( 'woocommerce_shop_mobile_columns' ) );

			if ( 2 === (int) $mobile_classes && 1 !== (int) $columns ) {
				$classes[0] = str_replace( 'col-12', 'col-6', $classes[0] );
			}
		}

		$item_template_vars = array(
			'columns'       => $columns,
			'show-cat'      => $settings['show_category'],
			'show-rating'   => $settings['show_rating'],
			'show-price'    => $settings['show_price'],
			'show-stock'    => $settings['show_stock'],
			'show-button'   => $settings['show_add_to_cart'],
			'selected-term' => $selected_term,
		);
		?>

		<div class="ignition-elementor-widgets-featured-product-category ignition-elementor-widgets-featured-product-category-<?php echo esc_attr( $settings['layout'] ); ?>">
		<?php
			$product_data = array();

			if ( ! empty( $handpicked ) ) {
				$product_data = $handpicked;
			} elseif ( ! empty( $category_id ) ) {
				$product_data = $category_id;
			}

			$args = array(
				'post_type'      => 'product',
				'posts_per_page' => $num_products,
			);

			if ( ! empty( $handpicked ) ) {
				$handpicked_query = array(
					'post__in' => $product_data,
					'orderby'  => 'post__in',
				);

				$args = array_merge( $args, $handpicked_query );
			} elseif ( ! empty( $category_id ) ) {
				$tax_query = array(
					'tax_query' => array(
						array(
							'taxonomy' => 'product_cat',
							'terms'    => $product_data,
							'field'    => 'id',
						),
					),
				);

				$args = array_merge( $args, $tax_query );
			}

			$loop = new \WP_Query( $args );
			if ( $loop->have_posts() ) :
				?>
				<div class="<?php echo esc_attr( implode( ' ', $container_classes ) ); ?>" data-columns="<?php echo (int) $columns; ?>" data-products="<?php echo (int) $num_products; ?>">
				<?php
				while ( $loop->have_posts() ) :
					$loop->the_post();

					global $product;

					if ( ( empty( $product ) || ! $product->is_visible() ) ) {
						continue;
					} else {
						?>
						<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
							<?php
							\CSSIgniter\Ignition\Elementor\Widgets_Helper::get_template_part( 'featured-product-category/item', get_post_type(), $item_template_vars );
							?>
						</div>
						<?php
					}
				endwhile;
				wp_reset_postdata();

				if ( $settings['show_category_card'] && ! empty( $category_id ) && empty( $handpicked ) ) :
					$category_meta     = get_term_meta( $category_id );
					$category_thumb_id = isset( $category_meta['thumbnail_id'] ) ? $category_meta['thumbnail_id'][0] : false;
					$category_image    = $settings['category_image'];

					$card_template_vars = array(
						'selected-term'      => $selected_term,
						'selected-term-meta' => $category_meta,
						'image-id'           => ! empty( $category_image['id'] ) ? $category_image['id'] : $category_thumb_id,
						'title'              => $settings['category_title'],
						'description'        => $settings['category_description'],
						'button-text'        => $settings['button_text'],
						'content-position'   => $settings['content_position'],
					);

					$classes[] = 'ignition-elementor-widgets-featured-product-category__card-wrap'
					?>
					<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
						<?php \CSSIgniter\Ignition\Elementor\Widgets_Helper::get_template_part( 'featured-product-category/category-card', '', $card_template_vars ); ?>
					</div>
					<?php
				endif;
			endif; ?>
		</div>

		<?php
	}
}
