<?php

/**
 * Control for WooCommerce
 *
 * @since 1.0.0
 */

namespace Kubelio\Customizer;

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

class WooCommerce {

	use Helper;

	public function __construct() {
		add_action('after_setup_theme', array($this, 'register_fields'));
		add_action('customize_register', array($this, 'remove_defaults'), 20);
	}

	/**
	 * Remove default WooCommerce customizer controls 
	 */
	public function remove_defaults($wp_customize) {
		$wp_customize->remove_control('woocommerce_catalog_rows');
	}

	/**
	 * Register WooCommerce related customizer fields
	 */
	public function register_fields() {
		$this->add_shop();
		$this->add_single_product();
		$this->add_checkout();
	}


	public function add_single_product() {


		$section = 'woocommerce_product_single';

		$this->add_section(
			$section,
			array(
				'title'    => esc_html__('Single Product', 'kubelio'),
				'panel'    => 'woocommerce',
				'priority' => 30,
			)
		);

		$this->add_field(
			array(
				'type'        => 'select',
				'label'       => esc_html__('Product Layout', 'kubelio'),
				'description' => esc_html__('Classic: left tabs with wide content. Compact: centered tabs with constrained content.', 'kubelio'),
				'settings'    => 'product_layout',
				'section'     => $section,
				'default'     => 'compact',
				'priority'    => 10,
				'choices'     => array(
					'classic' => esc_html__('Classic', 'kubelio'),
					'compact' => esc_html__('Compact', 'kubelio'),
				),
			)
		);
	}

	public function add_shop() {
		$section = 'woocommerce_product_catalog';

		$this->add_field(
			array(
				'type'        => 'number',
				'label'       => esc_html__('Products per row', 'kubelio'),
				'settings'    => 'shop_columns',
				'description' => esc_html__('The number of products to show in each row.', 'kubelio'),
				'section'     => $section,
				'default'     => 4,
				'priority'    => 200,
				'input_attrs' => array(
					'min' => 1,
					'max' => 6,
				),
			)
		);

		$this->add_field(
			array(
				'type'        => 'number',
				'label'       => esc_html__('Product Per Page', 'kubelio'),
				'settings'    => 'shop_per_page',
				'description' => esc_html__('Set the number of products per page (Max 100)', 'kubelio'),
				'section'     => $section,
				'default'     => 12,
				'priority'    => 210,
				'input_attrs' => array(
					'min' => 1,
					'max' => 100,
				),
			)
		);

		$this->add_field(
			array(
				'type'        => 'toggle',
				'label'       => esc_html__('Stack On Mobile', 'kubelio'),
				'settings'    => 'shop_stack_on_mobile',
				'description' => esc_html__('Show one product per row on mobile', 'kubelio'),
				'section'     => $section,
				'default'     => false,
				'priority'    => 220,
			)
		);

		$this->add_field(
			array(
				'type'     => 'select',
				'label'    => esc_html__('Catalog Style', 'kubelio'),
				'settings' => 'shop_style',
				'section'  => $section,
				'default'  => 'button-overlay',
				'priority' => 230,
				'choices'  => array(
					'classic'        => esc_html__('Classic', 'kubelio'),
					'button-overlay' => esc_html__('Button Overlay', 'kubelio'),
				),
			)
		);
	}

	public function add_checkout() {

		$section = 'woocommerce_checkout';

		$this->add_field([
			'type'    => 'divider',
			'section' => $section,
		]);

		$this->add_field(
			array(
				'type'        => 'toggle',
				'label'       => esc_html__('Distraction-Free Checkout', 'kubelio'),
				'description' => sprintf(
					/* translators: %s is the URL to the Logo Options. */
					wp_kses_post(__('Create a clean cart and checkout experience by hiding header elements to minimize distractions. You can change the checkout logo in the <a href="%s">Logo Options</a>.', 'kubelio')),
					"javascript:wp.customize.control( 'checkout_logo' ).focus();"
				),
				'settings'    => 'checkout_focus',
				'section'     => $section,
				'default'     => true,
			)
		);
	}
}
