<?php
if ( ! defined( 'ABSPATH' ) ) {
	// Exit if accessed directly.
	exit;
}

if ( ! function_exists( 'qode_badges_for_woocommerce_add_badge_meta_box' ) ) {
	/**
	 * Function that add general options for this module
	 */
	function qode_badges_for_woocommerce_add_badge_meta_box() {
		$qode_badges_for_woocommerce_framework = qode_badges_for_woocommerce_framework_get_framework_root();

		$page = $qode_badges_for_woocommerce_framework->add_options_page(
			array(
				'scope' => array( 'qbfw_badge' ),
				'type'  => 'meta',
				'slug'  => 'qbfw_badge',
				'title' => esc_html__( 'Badge', 'qode-badges-for-woocommerce' ),
			)
		);

		if ( $page ) {

			/* Badge section */

			// Hook to include additional options after module options.
			do_action( 'qode_badges_for_woocommerce_action_before_badge_meta_box_map', $page );

			$badge_types = apply_filters(
				'qode_badges_for_woocommerce_filter_badge_types',
				array()
			);

			$default_type = apply_filters( 'qode_badges_for_woocommerce_filter_default_badge_type', '' );

			$page->add_field_element(
				array(
					'field_type'    => 'select',
					'name'          => 'qode_badges_for_woocommerce_badge_type',
					'title'         => esc_html__( 'Type', 'qode-badges-for-woocommerce' ),
					'options'       => $badge_types,
					'default_value' => $default_type,
				)
			);
			// Hook to include additional options after initial type option.
			do_action( 'qode_badges_for_woocommerce_action_after_badge_type_meta_box_map', $page );

			$position_section = $page->add_section_element(
				array(
					'name'  => 'qode_badges_for_woocommerce_position_section',
					'title' => esc_html__( 'Position Settings', 'qode-badges-for-woocommerce' ),
				)
			);

			$position_section->add_field_element(
				array(
					'field_type'    => 'select',
					'name'          => 'qode_badges_for_woocommerce_badge_position',
					'title'         => esc_html__( 'Position', 'qode-badges-for-woocommerce' ),
					'options'       => array(
						'on_image'    => esc_html__( 'On Image', 'qode-badges-for-woocommerce' ),
						'below_image' => esc_html__( 'Below Image', 'qode-badges-for-woocommerce' ),
					),
					'default_value' => 'on_image',
					'args'          => array(
						'col_width' => 6,
					),
				)
			);

			$position_on_image_row = $position_section->add_row_element(
				array(
					'name'       => 'qode_badges_for_woocommerce_position_on_image_row',
					'dependency' => array(
						'show' => array(
							'qode_badges_for_woocommerce_badge_position' => array(
								'values'        => 'on_image',
								'default_value' => 'on_image',
							),
						),
					),
				)
			);

			$position_on_image_row->add_field_element(
				array(
					'field_type'    => 'radio',
					'name'          => 'qode_badges_for_woocommerce_badge_horizontal_position',
					'title'         => esc_html__( 'Horizontal Position', 'qode-badges-for-woocommerce' ),
					'options'       => array(
						'left'   => array(
							'image' => QODE_BADGES_FOR_WOOCOMMERCE_CPT_URL_PATH . '/badge/assets/img/top-left.svg',
							'label' => esc_html__( 'Left', 'qode-badges-for-woocommerce' ),
						),
						'center' => array(
							'image' => QODE_BADGES_FOR_WOOCOMMERCE_CPT_URL_PATH . '/badge/assets/img/center.svg',
							'label' => esc_html__( 'Center', 'qode-badges-for-woocommerce' ),
						),
						'right'  => array(
							'image' => QODE_BADGES_FOR_WOOCOMMERCE_CPT_URL_PATH . '/badge/assets/img/right.svg',
							'label' => esc_html__( 'Right', 'qode-badges-for-woocommerce' ),
						),
					),
					'default_value' => 'left',
					'args'          => array(
						'col_width'    => 6,
						'images'       => true,
						'custom_class' => 'qodef-with-border',
					),
					'data_attrs'    => array(
						'data-property' => 'horizontal',
					),
				)
			);

			$position_on_image_row->add_field_element(
				array(
					'field_type'    => 'radio',
					'name'          => 'qode_badges_for_woocommerce_badge_vertical_position',
					'title'         => esc_html__( 'Vertical Position', 'qode-badges-for-woocommerce' ),
					'options'       => array(
						'top'    => array(
							'image' => QODE_BADGES_FOR_WOOCOMMERCE_CPT_URL_PATH . '/badge/assets/img/top-left.svg',
							'label' => esc_html__( 'Top', 'qode-badges-for-woocommerce' ),
						),
						'middle' => array(
							'image' => QODE_BADGES_FOR_WOOCOMMERCE_CPT_URL_PATH . '/badge/assets/img/middle.svg',
							'label' => esc_html__( 'Middle', 'qode-badges-for-woocommerce' ),
						),
						'bottom' => array(
							'image' => QODE_BADGES_FOR_WOOCOMMERCE_CPT_URL_PATH . '/badge/assets/img/bottom.svg',
							'label' => esc_html__( 'Bottom', 'qode-badges-for-woocommerce' ),
						),
					),
					'default_value' => 'top',
					'args'          => array(
						'col_width'    => 6,
						'images'       => true,
						'custom_class' => 'qodef-with-border',
					),
					'data_attrs'    => array(
						'data-property' => 'vertical',
					),
				)
			);

			$position_on_image_row->add_field_element(
				array(
					'field_type' => 'text',
					'name'       => 'qode_badges_for_woocommerce_badge_horizontal_offset',
					'title'      => esc_html__( 'Horizontal Offset', 'qode-badges-for-woocommerce' ),
					'dependency' => array(
						'hide' => array(
							'qode_badges_for_woocommerce_badge_horizontal_position' => array(
								'values'        => 'center',
								'default_value' => 'left',
							),
						),
					),
					'args'       => array(
						'col_width' => 6,
						'suffix'    => 'px,%',
					),
					'data_attrs' => array(
						'data-property' => 'translateX',
					),
				)
			);

			$position_on_image_row->add_field_element(
				array(
					'field_type' => 'text',
					'name'       => 'qode_badges_for_woocommerce_badge_vertical_offset',
					'title'      => esc_html__( 'Vertical Offset', 'qode-badges-for-woocommerce' ),
					'dependency' => array(
						'hide' => array(
							'qode_badges_for_woocommerce_badge_vertical_position' => array(
								'values'        => 'middle',
								'default_value' => 'top',
							),
						),
					),
					'args'       => array(
						'col_width' => 6,
						'suffix'    => 'px,%',
					),
					'data_attrs' => array(
						'data-property' => 'translateY',
					),
				)
			);

			$position_below_image_row = $position_section->add_row_element(
				array(
					'name'       => 'qode_badges_for_woocommerce_position_below_image_row',
					'dependency' => array(
						'show' => array(
							'qode_badges_for_woocommerce_badge_position' => array(
								'values'        => 'below_image',
								'default_value' => 'on_image',
							),
						),
					),
				)
			);

			$position_below_image_row->add_field_element(
				array(
					'field_type' => 'text',
					'name'       => 'qode_badges_for_woocommerce_badge_margin',
					'title'      => esc_html__( 'Margin', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'col_width' => 6,
					),
					'data_attrs' => array(
						'data-property' => '--qbfw-margin',
					),
				)
			);

			$position_all_row = $position_section->add_row_element(
				array(
					'name' => 'qode_badges_for_woocommerce_position_all_row',
				)
			);

			do_action( 'qode_badges_for_woocommerce_action_position_before_z_index', $position_all_row );

			$position_all_row->add_field_element(
				array(
					'field_type' => 'text',
					'name'       => 'qode_badges_for_woocommerce_badge_z_index',
					'title'      => esc_html__( 'Z-index', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'col_width' => 6,
					),
					'data_attrs' => array(
						'data-property' => 'z-index',
					),
				)
			);

			$size_section = $page->add_section_element(
				array(
					'name'  => 'qode_badges_for_woocommerce_size_section',
					'title' => esc_html__( 'Size Settings', 'qode-badges-for-woocommerce' ),
				)
			);

			$size_row = $size_section->add_row_element(
				array(
					'name' => 'qode_badges_for_woocommerce_size_row',
				)
			);

			$width_hide_types = apply_filters( 'qode_badges_for_woocommerce_filter_hide_width_types', array() );

			$size_row->add_field_element(
				array(
					'field_type'    => 'text',
					'name'          => 'qode_badges_for_woocommerce_badge_width',
					'title'         => esc_html__( 'Width', 'qode-badges-for-woocommerce' ),
					'default_value' => '100',
					'args'          => array(
						'col_width' => 6,
						'suffix'    => 'px,%,vw',
					),
					'data_attrs'    => array(
						'data-property' => '--qbfw-width',
					),
					'dependency'    => array(
						'hide' => array(
							'qode_badges_for_woocommerce_badge_type' => array(
								'values'        => $width_hide_types,
								'default_value' => $default_type,
							),
						),
					),
				)
			);

			$height_hide_types = apply_filters( 'qode_badges_for_woocommerce_filter_hide_height_types', array() );

			$size_row->add_field_element(
				array(
					'field_type' => 'text',
					'name'       => 'qode_badges_for_woocommerce_badge_height',
					'title'      => esc_html__( 'Height', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'col_width' => 6,
						'suffix'    => 'px,%,vw',
					),
					'data_attrs' => array(
						'data-property' => '--qbfw-height',
					),
					'dependency' => array(
						'hide' => array(
							'qode_badges_for_woocommerce_badge_type' => array(
								'values'        => $height_hide_types,
								'default_value' => $default_type,
							),
						),
					),
				)
			);

			$size_row->add_field_element(
				array(
					'field_type'    => 'text',
					'name'          => 'qode_badges_for_woocommerce_badge_padding',
					'title'         => esc_html__( 'Padding', 'qode-badges-for-woocommerce' ),
					'default_value' => '10px',
					'args'          => array(
						'col_width' => 6,
					),
					'data_attrs'    => array(
						'data-property' => '--qbfw-padding',
					),
				)
			);

			$size_row->add_field_element(
				array(
					'field_type'    => 'text',
					'name'          => 'qode_badges_for_woocommerce_badge_width_880',
					'title'         => esc_html__( 'Width under 880px', 'qode-badges-for-woocommerce' ),
					'default_value' => '100',
					'args'          => array(
						'col_width' => 6,
						'suffix'    => 'px,%,vw',
					),
					'data_attrs'    => array(
						'data-property' => '--qbfw-width-880',
					),
					'dependency'    => array(
						'hide' => array(
							'qode_badges_for_woocommerce_badge_type' => array(
								'values'        => $width_hide_types,
								'default_value' => $default_type,
							),
						),
					),
				)
			);

			$size_row->add_field_element(
				array(
					'field_type' => 'text',
					'name'       => 'qode_badges_for_woocommerce_badge_height_880',
					'title'      => esc_html__( 'Height under 880px', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'col_width' => 6,
						'suffix'    => 'px,%,vw',
					),
					'data_attrs' => array(
						'data-property' => '--qbfw-height-880',
					),
					'dependency' => array(
						'hide' => array(
							'qode_badges_for_woocommerce_badge_type' => array(
								'values'        => $height_hide_types,
								'default_value' => $default_type,
							),
						),
					),
				)
			);

			$size_row->add_field_element(
				array(
					'field_type'    => 'text',
					'name'          => 'qode_badges_for_woocommerce_badge_padding_880',
					'title'         => esc_html__( 'Padding under 880px', 'qode-badges-for-woocommerce' ),
					'default_value' => '10px',
					'args'          => array(
						'col_width' => 6,
					),
					'data_attrs'    => array(
						'data-property' => '--qbfw-padding-880',
					),
				)
			);

			$colors_show_types = apply_filters( 'qode_badges_for_woocommerce_filter_colors_badge_types', array() );

			$colors_section = $page->add_section_element(
				array(
					'name'       => 'qode_badges_for_woocommerce_colors_section',
					'title'      => esc_html__( 'Color Settings', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'custom_class' => 'qodef-no-indent',
					),
					'dependency' => array(
						'show' => array(
							'qode_badges_for_woocommerce_badge_type' => array(
								'values'        => $colors_show_types,
								'default_value' => $default_type,
							),
						),
					),
				)
			);

			$colors_row = $colors_section->add_row_element(
				array(
					'name' => 'qode_badges_for_woocommerce_colors_row',
				)
			);

			$colors_row->add_field_element(
				array(
					'field_type'    => 'color',
					'name'          => 'qode_badges_for_woocommerce_badge_background_color',
					'title'         => esc_html__( 'Background Color', 'qode-badges-for-woocommerce' ),
					'default_value' => '#fff',
					'args'          => array(
						'col_width' => 6,
					),
					'data_attrs'    => array(
						'data-property' => '--qbfw-background-color',
					),
				)
			);

			do_action( 'qode_badges_for_woocommerce_action_badges_color_meta_options', $colors_row, $colors_section );

			$border_show_types = apply_filters( 'qode_badges_for_woocommerce_filter_border_badge_types', array() );

			$border_section = $page->add_section_element(
				array(
					'name'       => 'qode_badges_for_woocommerce_border_section',
					'title'      => esc_html__( 'Border Settings', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'custom_class' => 'qodef-no-indent',
					),
					'dependency' => array(
						'show' => array(
							'qode_badges_for_woocommerce_badge_type' => array(
								'values'        => $border_show_types,
								'default_value' => $default_type,
							),
						),
					),
				)
			);

			$border_row = $border_section->add_row_element(
				array(
					'name' => 'qode_badges_for_woocommerce_border_row',
				)
			);

			$border_row->add_field_element(
				array(
					'field_type' => 'color',
					'name'       => 'qode_badges_for_woocommerce_badge_border_color',
					'title'      => esc_html__( 'Border Color', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'col_width' => 6,
					),
					'data_attrs' => array(
						'data-property' => '--qbfw-border-color',
					),
				)
			);

			$border_row->add_field_element(
				array(
					'field_type' => 'text',
					'name'       => 'qode_badges_for_woocommerce_badge_border_width',
					'title'      => esc_html__( 'Border Width', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'col_width' => 6,
					),
					'data_attrs' => array(
						'data-property' => '--qbfw-border-width',
					),
				)
			);

			$border_row->add_field_element(
				array(
					'field_type'    => 'select',
					'name'          => 'qode_badges_for_woocommerce_badge_border_style',
					'title'         => esc_html__( 'Border Style', 'qode-badges-for-woocommerce' ),
					'options'       => array(
						'none'   => esc_html__( 'None', 'qode-badges-for-woocommerce' ),
						'solid'  => esc_html__( 'Solid', 'qode-badges-for-woocommerce' ),
						'dashed' => esc_html__( 'Dashed', 'qode-badges-for-woocommerce' ),
						'dotted' => esc_html__( 'Dotted', 'qode-badges-for-woocommerce' ),
					),
					'default_value' => 'none',
					'args'          => array(
						'col_width' => 6,
					),
					'data_attrs'    => array(
						'data-property' => '--qbfw-border-style',
					),
				)
			);

			$border_row->add_field_element(
				array(
					'field_type' => 'text',
					'name'       => 'qode_badges_for_woocommerce_badge_border_radius',
					'title'      => esc_html__( 'Border Radius', 'qode-badges-for-woocommerce' ),
					'args'       => array(
						'col_width' => 6,
					),
					'data_attrs' => array(
						'data-property' => '--qbfw-border-radius',
					),
				)
			);
			// Hook to include additional options after module options.
			do_action( 'qode_badges_for_woocommerce_action_after_badge_meta_box_map', $page );
		}
	}

	add_action( 'qode_badges_for_woocommerce_action_default_meta_boxes_init', 'qode_badges_for_woocommerce_add_badge_meta_box' );
}


if ( ! function_exists( 'qode_badges_for_woocommerce_badge_text_mce_buttons' ) ) {
	function qode_badges_for_woocommerce_badge_text_mce_buttons( $buttons, $id ) {
		if ( 'qode_badges_for_woocommerce_text' === $id ) {
			$buttons_to_remove = array(
				'bullist',
				'numlist',
				'blockquote',
				'alignleft',
				'aligncenter',
				'alignright',
				'link',
				'wp_more',
				'fullscreen',
			);
			foreach ( $buttons_to_remove as $to_remove ) {
				$key = array_search( $to_remove, $buttons, true );
				if ( $key ) {
					unset( $buttons[ $key ] );
				}
			}

			$buttons_to_add = array(
				'forecolor',
				'fontselect',
				'fontsizeselect',
			);

			$buttons = array_merge( $buttons_to_add, $buttons );
		}

		return $buttons;
	}

	add_filter( 'mce_buttons', 'qode_badges_for_woocommerce_badge_text_mce_buttons', 10, 2 );
}
if ( ! function_exists( 'qode_badges_for_woocommerce_badge_text_mce_buttons_2' ) ) {
	function qode_badges_for_woocommerce_badge_text_mce_buttons_2( $buttons, $id ) {
		if ( 'qode_badges_for_woocommerce_text' === $id ) {
			$buttons_to_remove = array(
				'wp_help',
				'forecolor',
				'hr',
			);
			foreach ( $buttons_to_remove as $to_remove ) {
				$key = array_search( $to_remove, $buttons, true );
				if ( $key ) {
					unset( $buttons[ $key ] );
				}
			}

			$buttons_to_add = array();

			$buttons = array_merge( $buttons_to_add, $buttons );
		}

		return $buttons;
	}

	add_filter( 'mce_buttons_2', 'qode_badges_for_woocommerce_badge_text_mce_buttons_2', 10, 2 );
}
if ( ! function_exists( 'qode_badges_for_woocommerce_badge_text_quicktags_settings' ) ) {
	function qode_badges_for_woocommerce_badge_text_quicktags_settings( $qt_init, $id ) {
		if ( 'qode_badges_for_woocommerce_text' === $id ) {
			$qt_init['buttons'] = 'strong,em,del,close';
		}

		return $qt_init;
	}

	add_filter( 'quicktags_settings', 'qode_badges_for_woocommerce_badge_text_quicktags_settings', 10, 2 );
}
