<?php
/**
 * Add an element to fusion-builder.
 *
 * @package fusion-builder
 * @since 3.15.0
 */

if ( fusion_is_element_enabled( 'fusion_tb_woo_filters_brands' ) ) {

	if ( ! class_exists( 'FusionTB_WooFiltersBrands' ) ) {
		/**
		 * Shortcode class.
		 *
		 * @since 3.15.0
		 */
		class FusionTB_WooFiltersBrands extends AWB_Woo_Filters {

			/**
			 * Constructor.
			 *
			 * @access public
			 * @since 3.15.0
			 */
			public function __construct() {
				$this->shortcode_handle = 'fusion_tb_woo_filters_brands';
				parent::__construct();

				add_filter( 'awb_woo_filters_wrapper_attr', [ $this, 'adjust_wrapper_attributes' ], 10, 2 );

				add_filter( $this->shortcode_handle . '_widget_content', [ $this, 'adjust_woo_brand_filter_html' ], 10, 3 );
			}

			/**
			 * Gets the default values.
			 *
			 * @static
			 * @access public
			 * @since 3.15.0
			 * @return array
			 */
			public static function get_element_defaults() {
				$defaults        = parent::get_element_defaults();
				$fusion_settings = awb_get_fusion_settings();

				$args = wp_parse_args(
					[
						'columns'                        => '3',
						'column_gap'                     => '15',
						'display_names'                  => 'no',
						'display_count'                  => 'no',
						'brand_opacity'                  => '1',
						'brand_hover_opacity'            => '1',
						'brand_border_size'              => '',
						'brand_border_color'             => $fusion_settings->get( 'link_color' ),
						'brand_border_hover_color'       => $fusion_settings->get( 'link_hover_color' ),
						'brand_border_radius'            => $fusion_settings->get( 'form_border_radius' ),
						'fusion_font_family_brand_font'  => '',
						'fusion_font_variant_brand_font' => '',
						'brand_font_size'                => 'size',
						'brand_line_height'              => 'line_height',
						'brand_letter_spacing'           => 'letter_spacing',
						'brand_text_transform'           => 'text_transform',
						'brand_text_color'               => $fusion_settings->get( 'link_color' ),
						'brand_text_hover_color'         => $fusion_settings->get( 'link_hover_color' ),						
					],
					$defaults
				);

				return $args;
			}

			/**
			 * Get element subparams.
			 *
			 * @access public
			 * @since 3.15.0
			 * @return array
			 */
			public function get_element_subparams() {
				$options = parent::get_element_subparams();

				$params = [
					'fusion_font_family_brand_font'  => 'brand_font',
					'fusion_font_variant_brand_font' => 'brand_font',
					'brand_font_size'                => 'brand_font',
					'brand_line_height'              => 'brand_font',
					'brand_letter_spacing'           => 'brand_font',
					'brand_text_transform'           => 'brand_font',
				];

				return array_merge( $options, $params );
			}
			
			/**
			 * Builds the attributes array.
			 *
			 * @access public
			 * @since 3.15.0
			 * @return array
			 */
			public function attr() {
				$attr = parent::attr();

				$attr['class'] .= ' awb-thumbnails awb-columns-' . $this->args['columns' ];

				return $attr;
			}			

			/**
			 * Fetch general options.
			 *
			 * @access public
			 * @since 3.15.0
			 * @return array
			 */
			public function fetch_general_options() {
				$options = parent::fetch_general_options();
				$params  = [];

				foreach ( $options as $opt ) {
					if ( 'title' === $opt['param_name'] ) {
						$opt['value'] = esc_html__( 'Brands', 'fusion-builder' );
					}
					if ( in_array( $opt['param_name'], [ 'title', 'title_size' ], true ) ) {
						$opt['callback']['action'] = "get_{$this->shortcode_handle}";
					}

					$params[] = $opt;
				}

				return $params;
			}

			/**
			 * Fetch design options.
			 *
			 * @access public
			 * @since 3.15.0
			 * @return array
			 */
			public function fetch_design_options() {
				$options         = parent::fetch_design_options();
				$fusion_settings = awb_get_fusion_settings();

				$params = [
					[
						'type'        => 'range',
						'heading'     => esc_html__( 'Number of Columns', 'fusion-builder' ),
						'description' => esc_html__( 'Set the number of columns.', 'fusion-builder' ),
						'param_name'  => 'columns',
						'value'       => '3',
						'min'         => '1',
						'max'         => '6',
						'step'        => '1',
						'group'       => esc_html__( 'Design', 'fusion-builder' ),
					],
					[
						'type'        => 'range',
						'heading'     => esc_html__( 'Column Gap', 'fusion-builder' ),
						'description' => esc_html__( 'Set the gap size between columns. In Pixels.', 'fusion-builder' ),
						'param_name'  => 'column_gap',
						'value'       => '15',
						'min'         => '0',
						'max'         => '75',
						'step'        => '1',
						'group'       => esc_html__( 'Design', 'fusion-builder' ),
					],
					[
						'type'        => 'range',
						'heading'     => esc_html__( 'Brand Thumb Opacity', 'fusion-builder' ),
						'description' => esc_html__( 'Controls the opacity of the brand thumbs. In percentage, 1 = 100%.', 'fusion-builder' ),
						'param_name'  => 'brand_opacity',
						'group'       => esc_html__( 'Design', 'fusion-builder' ),
						'value'       => '1',
						'min'         => '0',
						'max'         => '1',
						'step'        => '0.05',
						'states'        => [
							'hover' => [
								'label'      => __( 'Hover', 'fusion-builder' ),
								'param_name' => 'brand_hover_opacity',
								'value'      => '1',
								'preview'    => [
									'selector' => '.widget_layered_nav li, .widget_layered_nav li a',
									'type'     => 'class',
									'toggle'   => 'hover',
								],
							],
						],
					],
					[
						'type'        => 'range',
						'heading'     => esc_html__( 'Brand Thumb Border Size', 'fusion-builder' ),
						'description' => esc_html__( 'Controls the border size of the brand thumbs. In pixels.', 'fusion-builder' ),
						'param_name'  => 'brand_border_size',
						'group'       => esc_html__( 'Design', 'fusion-builder' ),
						'value'       => '0',
						'min'         => '0',
						'max'         => '25',
						'step'        => '1',
					],
					[
						'type'          => 'colorpickeralpha',
						'heading'       => esc_html__( 'Brand Thumb Border Color', 'fusion-builder' ),
						'description'   => esc_html__( 'Controls the border color of the brand thumbs.', 'fusion-builder' ),
						'param_name'    => 'brand_border_color',
						'value'         => '',
						'default'       => $fusion_settings->get( 'link_color' ),
						'group'         => esc_html__( 'Design', 'fusion-builder' ),
						'states'        => [
							'hover' => [
								'label'      => __( 'Hover', 'fusion-builder' ),
								'param_name' => 'brand_border_hover_color',
								'default'    => $fusion_settings->get( 'link_hover_color' ),
								'preview'    => [
									'selector' => '.widget_layered_nav li',
									'type'     => 'class',
									'toggle'   => 'hover',
								],
							],
						],
						'dependency'  => [
							[
								'element'  => 'brand_border_size',
								'value'    => '0',
								'operator' => '!=',
							],
						],
					],
					[
						'type'        => 'range',
						'heading'     => esc_html__( 'Brand Thumb Border Radius', 'fusion-builder' ),
						'param_name'  => 'brand_border_radius',
						'description' => esc_html__( 'Controls the border radius of the brand thumbs. In pixels.', 'fusion-builder' ),
						'min'         => '0',
						'max'         => '50',
						'step'        => '1',
						'value'       => '',
						'default'     => $fusion_settings->get( 'form_border_radius' ),
						'group'       => esc_html__( 'Design', 'fusion-builder' ),
					],
					[
						'type'        => 'radio_button_set',
						'heading'     => esc_html__( 'Display Brand Names', 'fusion-builder' ),
						'description' => esc_html__( 'Select if you want to display the brand names.', 'fusion-builder' ),
						'group'       => esc_html__( 'Design', 'fusion-builder' ),
						'param_name'  => 'display_names',
						'default'     => 'no',
						'value'       => [
							'yes' => esc_html__( 'Yes', 'fusion-builder' ),
							'no'  => esc_html__( 'No', 'fusion-builder' ),
						],
						'callback'    => [
							'function' => 'fusion_ajax',
							'action'   => 'get_fusion_tb_woo_filters_brands',
							'ajax'     => true,
						],
					],
					[
						'type'        => 'radio_button_set',
						'heading'     => esc_html__( 'Display Count Number', 'fusion-builder' ),
						'description' => esc_html__( 'Select if you want to display the product counter.', 'fusion-builder' ),
						'group'       => esc_html__( 'Design', 'fusion-builder' ),
						'param_name'  => 'display_count',
						'default'     => 'no',
						'value'       => [
							'yes' => esc_html__( 'Yes', 'fusion-builder' ),
							'no'  => esc_html__( 'No', 'fusion-builder' ),
						],
						'callback'    => [
							'function' => 'fusion_ajax',
							'action'   => 'get_fusion_tb_woo_filters_brands',
							'ajax'     => true,
						],
					],
					[
						'type'             => 'typography',
						'heading'          => esc_html__( 'Name/Counter Typography', 'fusion-builder' ),
						'description'      => esc_html__( 'Controls the typography of the brand name and counter. Leave empty for the global font family.', 'fusion-builder' ),
						'param_name'       => 'brand_font',
						'choices'          => [
							'font-family'    => 'brand_font',
							'font-size'      => 'brand_font_size',
							'text-transform' => 'brand_text_transform',
							'line-height'    => 'brand_line_height',
							'letter-spacing' => 'brand_letter_spacing',
						],
						'default'          => [
							'font-family'    => '',
							'variant'        => '400',
							'font-size'      => '',
							'text-transform' => '',
							'line-height'    => '',
							'letter-spacing' => '',
						],
						'remove_from_atts' => true,
						'global'           => true,
						'group'            => esc_html__( 'Design', 'fusion-builder' ),
						'or'               => true,
						'dependency'       => [
							[
								'element'  => 'display_names',
								'value'    => 'yes',
								'operator' => '=',
							],
							[
								'element'  => 'display_count',
								'value'    => 'yes',
								'operator' => '=',
							],
						],
					],					
					[
						'type'          => 'colorpickeralpha',
						'heading'       => esc_html__( 'Name/Counter Color', 'fusion-builder' ),
						'description'   => esc_html__( 'Controls the color of the brand name and counter.', 'fusion-builder' ),
						'param_name'    => 'brand_text_color',
						'value'         => '',
						'default'       => $fusion_settings->get( 'link_color' ),
						'group'         => esc_html__( 'Design', 'fusion-builder' ),
						'or'               => true,
						'dependency'       => [
							[
								'element'  => 'display_names',
								'value'    => 'yes',
								'operator' => '=',
							],
							[
								'element'  => 'display_count',
								'value'    => 'yes',
								'operator' => '=',
							],
						],
						'states'        => [
							'hover' => [
								'label'      => __( 'Hover', 'fusion-builder' ),
								'param_name' => 'brand_text_hover_color',
								'default'    => $fusion_settings->get( 'link_hover_color' ),
								'preview'    => [
									'selector' => '.widget_layered_nav li, .widget_layered_nav li a',
									'type'     => 'class',
									'toggle'   => 'hover',
								],
							],
						],
					],									
				];

				foreach ( $params as $param ) {
					$options[] = $param;
				}

				return $options;
			}

			/**
			 * Gets the widget instance options.
			 *
			 * @static
			 * @access public
			 * @since 3.15.0
			 * @return array
			 */
			public function get_widget_instance() {
				return [
					'title'        => $this->args['title'],
					'display_type' => 'list',
				];
			}			

			/**
			 * Get the style variables.
			 *
			 * @access protected
			 * @since 3.15.0
			 * @param array $custom_vars The custom CSS vars array.
			 * @return string
			 */
			protected function get_style_variables( $custom_vars = [] ) {
				$custom_vars = parent::get_style_variables( $custom_vars );

				// Brand Typo.
				$custom_vars = $this->get_typo_variables(
					[
						'brand_font'           => 'font',
						'brand_font_size'      => 'size',
						'brand_line_height'    => 'line_height',
						'brand_letter_spacing' => 'letter_spacing',
						'brand_text_transform' => 'text_transform',
					],
					$custom_vars
				);

				// Colors style.
				$css_vars_options = [
					'columns',
					'column_gap'               => [ 'callback' => [ 'Fusion_Sanitize', 'get_value_with_unit' ] ],
					'brand_opacity',
					'brand_hover_opacity',
					'brand_text_color'         => [ 'callback' => [ 'Fusion_Sanitize', 'color' ] ],
					'brand_text_hover_color'   => [ 'callback' => [ 'Fusion_Sanitize', 'color' ] ],
					'brand_border_size'        => [ 'callback' => [ 'Fusion_Sanitize', 'get_value_with_unit' ] ],
					'brand_border_color'       => [ 'callback' => [ 'Fusion_Sanitize', 'color' ] ],
					'brand_border_hover_color' => [ 'callback' => [ 'Fusion_Sanitize', 'color' ] ],
					'brand_border_radius'      => [ 'callback' => [ 'Fusion_Sanitize', 'get_value_with_unit' ] ],
				];

				$styles = $this->get_css_vars_for_options( $css_vars_options ) . $this->get_custom_css_vars( $custom_vars );

				return $styles;
			}

			/**
			 * Emulate filter element for LE.
			 *
			 * @access public
			 * @since 3.15.0
			 * @return void
			 */
			public function emulate_filter_element() {
				if ( is_null( WC()->query->get_main_query() ) ) {
					WC()->query->product_query( $GLOBALS['wp_query'] );
				}
			}

			/**
			 * Restore filter element for LE.
			 *
			 * @access public
			 * @since 3.15.0
			 * @return void
			 */
			public function restore_filter_element() {
				if ( WC()->query->get_main_query() ) {
					WC()->query->product_query( new WP_Query() );
				}
			}
		}
	}

	/**
	 * Instantiates the class.
	 *
	 * @return object
	 */
	function awb_woo_filter_brands() { // phpcs:ignore WordPress.NamingConventions
		return FusionTB_WooFiltersBrands::get_instance();
	}

	// Instantiate.
	awb_woo_filter_brands();
}

/**
 * Map shortcode to Avada Builder.
 */
function fusion_element_woo_filters_brands() {
	if ( class_exists( 'WooCommerce' ) ) {
		$params    = [];
		$subparams = [];

		// We only need options if element is active.
		if ( function_exists( 'awb_woo_filter_brands' ) ) {
			$params    = awb_woo_filter_brands()->get_element_params();
			$subparams = awb_woo_filter_brands()->get_element_subparams();
		}

		fusion_builder_map(
			fusion_builder_frontend_data(
				'FusionTB_WooFiltersBrands',
				[
					'name'         => esc_attr__( 'Woo Filter By Brand', 'fusion-builder' ),
					'shortcode'    => 'fusion_tb_woo_filters_brands',
					'help_url'     => 'https://avada.com/documentation/woo-filter-by-brand-element/',
					'icon'         => 'fusiona-woo-filters-brands',
					'component'    => true,
					'templates'    => [ 'content', 'awb_off_canvas' ],
					'subparam_map' => $subparams,
					'params'       => $params,
					'callback'     => [
						'function' => 'fusion_ajax',
						'action'   => 'get_fusion_tb_woo_filters_brands',
						'ajax'     => true,
					],
				]
			)
		);
	}
}
add_action( 'fusion_builder_before_init', 'fusion_element_woo_filters_brands' );
