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

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

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

	}

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

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

	}

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

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

	public function get_icon() {
		return 'eicon-posts-grid';
	}

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

	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_tabs',
			array(
				'label' => __( 'Category selection', 'ignition-elementor-widgets' ),
			)
		);

		$repeater = new \Elementor\Repeater();

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

		$repeater->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' ),
			)
		);

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

		$this->add_control(
			'categories',
			array(
				'label'  => esc_html__( 'Categories', 'ignition-elementor-widgets' ),
				'type'   => \Elementor\Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
			)
		);

		$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(
			'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_title',
			array(
				'label'        => esc_html__( 'Show category title', '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_count',
			array(
				'label'        => esc_html__( 'Show category count', '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->end_controls_section();
	}

	protected function render() {
		$settings   = $this->get_settings();
		$categories = $settings['categories'];
		$columns    = $settings['columns']['size'];

		$container_classes = array(
			'ignition-elementor-widgets-product-category-list__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-title' => $settings['show_title'],
			'show-count' => $settings['show_count'],
			'classes'    => $classes,
		);

		$args = array(
			'include' => wp_list_pluck( $categories, 'product_category' ),
			'orderby' => 'include',
		);

		$product_terms = get_terms( 'product_cat', $args );

		$custom_images = wp_list_pluck( $categories, 'category_image', 'product_category' );

		?>
		<div class="ignition-elementor-widgets-product-category-list ignition-elementor-widgets-product-category-list-<?php echo esc_attr( $settings['layout'] ); ?>">
			<?php if ( $product_terms ) : ?>
				<ul class="<?php echo esc_attr( implode( ' ', $container_classes ) ); ?>" data-columns="<?php echo (int) $columns; ?>">
				<?php foreach ( $product_terms as $term ) {
					$item_template_vars['product-category'] = get_term( $term->term_id );
					$item_template_vars['custom-image-id']  = ! empty( $custom_images ) && isset( $custom_images[ $term->term_id ]['id'] ) ? $custom_images[ $term->term_id ]['id'] : false;

					\CSSIgniter\Ignition\Elementor\Widgets_Helper::get_template_part( 'product-category-list/product-category', '', $item_template_vars );
				} ?>
				</ul>
			<?php endif; ?>
		</div>
		<?php
	}
}
