<?php

if ( ! function_exists( 'ginevra_core_add_info_list_shortcode' ) ) {
	/**
	 * Function that add shortcode into shortcodes list for registration
	 *
	 * @param array $shortcodes
	 *
	 * @return array
	 */
	function ginevra_core_add_info_list_shortcode( $shortcodes ) {
		$shortcodes[] = 'GinevraCore_Info_List_Shortcode';

		return $shortcodes;
	}

	add_filter( 'ginevra_core_filter_register_shortcodes', 'ginevra_core_add_info_list_shortcode' );
}

if ( class_exists( 'GinevraCore_Shortcode' ) ) {
	class GinevraCore_Info_List_Shortcode extends GinevraCore_Shortcode {

		public function map_shortcode() {
			$this->set_shortcode_path( GINEVRA_CORE_SHORTCODES_URL_PATH . '/info-list' );
			$this->set_base( 'ginevra_core_info_list' );
			$this->set_name( esc_html__( 'Info List', 'ginevra-core' ) );
			$this->set_description( esc_html__( 'Shortcode that adds info list element', 'ginevra-core' ) );
			$this->set_category( esc_html__( 'Ginevra Core', 'ginevra-core' ) );
			$this->set_option(
				array(
					'field_type' => 'text',
					'name'       => 'custom_class',
					'title'      => esc_html__( 'Custom Class', 'ginevra-core' ),
				)
			);
			$this->set_option(
				array(
					'field_type' => 'text',
					'name'       => 'title',
					'title'      => esc_html__( 'Title', 'ginevra-core' ),
				)
			);
			$this->set_option(
				array(
					'field_type' => 'textarea',
					'name'       => 'text',
					'title'      => esc_html__( 'Text', 'ginevra-core' ),
				)
			);

			$this->set_option(
				array(
					'field_type' => 'repeater',
					'name'       => 'children',
					'title'      => esc_html__( 'Info List Items', 'ginevra-core' ),
					'items'      => array(
						array(
							'field_type'    => 'text',
							'name'          => 'info_title',
							'title'         => esc_html__( 'Info Title', 'ginevra-core' ),
							'default_value' => '',
						),
						array(
							'field_type'    => 'text',
							'name'          => 'info_subtitle',
							'title'         => esc_html__( 'Info Subtitle', 'ginevra-core' ),
							'default_value' => '',
						),
                        array(
                            'field_type'    => 'select',
                            'name'          => 'custom_link',
                            'title'         => esc_html__('Enable Custom Link for Info Subtitle', 'ginevra-core'),
                            'default_value' => 'no',
                            'options'       => ginevra_core_get_select_type_options_pool('no_yes', false),
                        ),
                        array(
                            'field_type' => 'text',
                            'name'       => 'link',
                            'title'      => esc_html__( 'Custom Link', 'ginevra-core' ),
                            'dependency' => array(
                                'show' => array(
                                    'custom_link' => array(
                                        'values'        => 'yes',
                                        'default_value' => '',
                                    ),
                                ),
                            ),
                        ),
                        array(
                            'field_type'    => 'select',
                            'name'          => 'target',
                            'title'         => esc_html__( 'Custom Link Target', 'ginevra-core' ),
                            'options'       => ginevra_core_get_select_type_options_pool( 'link_target' ),
                            'default_value' => '_blank',
                            'dependency'    => array(
                                'show' => array(
                                    'custom_link' => array(
                                        'values'        => 'yes',
                                        'default_value' => '',
                                    ),
                                ),
                            ),
                        )
					),
				)
			);
		}

		public function render( $options, $content = null ) {
			parent::render( $options );
			$atts = $this->get_atts();

			$atts['holder_classes'] = $this->get_holder_classes( $atts );
			$atts['items']          = $this->parse_repeater_items( $atts['children'] );

			return ginevra_core_get_template_part( 'shortcodes/info-list', 'templates/info-list', '', $atts );
		}

		private function get_holder_classes( $atts ) {
			$holder_classes = $this->init_holder_classes();

			$holder_classes[] = 'qodef-info-list';

			return implode( ' ', $holder_classes );
		}
	}
}