<?php

if ( ! function_exists( 'ginevra_core_add_blog_list_shortcode' ) ) {
	/**
	 * Function that isadding shortcode into shortcodes list for registration
	 *
	 * @param array $shortcodes - Array of registered shortcodes
	 *
	 * @return array
	 */
	function ginevra_core_add_blog_list_shortcode( $shortcodes ) {
		$shortcodes[] = 'GinevraCore_Blog_List_Shortcode';

		return $shortcodes;
	}

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

if ( class_exists( 'GinevraCore_List_Shortcode' ) ) {
	class GinevraCore_Blog_List_Shortcode extends GinevraCore_List_Shortcode {

		public function __construct() {
			$this->set_post_type( 'post' );
			$this->set_post_type_taxonomy( 'category' );
			$this->set_post_type_additional_taxonomies( array( 'post_tag' ) );
			$this->set_layouts( apply_filters( 'ginevra_core_filter_blog_list_layouts', array() ) );

			parent::__construct();
		}

		public function map_shortcode() {
			$this->set_shortcode_path( GINEVRA_CORE_INC_URL_PATH . '/blog/shortcodes/blog-list' );
			$this->set_base( 'ginevra_core_blog_list' );
			$this->set_name( esc_html__( 'Blog List', 'ginevra-core' ) );
			$this->set_description( esc_html__( 'Shortcode that displays list of blog posts', 'ginevra-core' ) );
			$this->set_scripts(
				apply_filters( 'ginevra_core_filter_blog_list_register_scripts', array() )
			);
			$this->set_necessary_styles(
				apply_filters( 'ginevra_core_filter_blog_list_register_styles', array() )
			);
			$this->set_option(
				array(
					'field_type' => 'text',
					'name'       => 'custom_class',
					'title'      => esc_html__( 'Custom Class', 'ginevra-core' ),
				)
			);
			$this->map_list_options();
			$this->map_query_options();
			$this->map_layout_options(
				array(
					'layouts' => $this->get_layouts(),
				)
			);
            $this -> set_option( array (
                'field_type' => 'color',
                'name'       => 'title_color',
                'title'      => esc_html__( 'Title Color', 'ginevra-core' ),
                'group'      => esc_html__( 'Layout', 'ginevra-core' ),
                'dependency' => array (
                    'show' => array (
                        'layout' => array (
                            'values'        => 'standard',
                            'default_value' => '',
                        ),
                    ),
                ),
            ) );
			$this->set_option(
				array(
					'field_type' => 'text',
					'name'       => 'excerpt_length',
					'title'      => esc_html__( 'Excerpt Length', 'ginevra-core' ),
					'group'      => esc_html__( 'Layout', 'ginevra-core' ),
				)
			);
            $this -> set_option( array (
                'field_type' => 'select',
                'name'       => 'disable_button',
                'title'      => esc_html__( 'Hide Read More Button', 'ginevra-core' ),
                'options'    => ginevra_core_get_select_type_options_pool( 'no_yes' ),
                'group'      => esc_html__( 'Layout', 'ginevra-core' ),
                'default_value' => '',
                'dependency' => array (
                    'show' => array (
                        'layout' => array (
                            'values'        => 'standard',
                            'default_value' => '',
                        ),
                    ),
                ),
            ) );
            $this -> set_option( array (
                'field_type' => 'select',
                'name'       => 'enable_author',
                'title'      => esc_html__( 'Show Author', 'ginevra-core' ),
                'options'    => ginevra_core_get_select_type_options_pool( 'no_yes' ),
                'default_value' => '',
                'group'      => esc_html__( 'Layout', 'ginevra-core' ),
                'dependency' => array (
                    'show' => array (
                        'layout' => array (
                            'values'        => 'standard',
                            'default_value' => '',
                        ),
                    ),
                ),
            ) );
            $this -> set_option( array (
                'field_type' => 'color',
                'name'       => 'author_color',
                'title'      => esc_html__( 'Author Color', 'ginevra-core' ),
                'group'      => esc_html__( 'Layout', 'ginevra-core' ),
                'dependency' => array (
                    'show' => array (
                        'enable_author' => array (
                            'values'        => 'yes',
                            'default_value' => 'no',
                        ),
                    ),
                ),
            ) );
            $this -> set_option( array (
                'field_type' => 'text',
                'name'       => 'author_margin_bottom',
                'title'      => esc_html__( 'Margin Bottom', 'ginevra-core' ),
                'group'      => esc_html__( 'Layout', 'ginevra-core' ),
                'dependency' => array (
                    'show' => array (
                        'enable_author' => array (
                            'values'        => 'yes',
                            'default_value' => 'no',
                        ),
                    ),
                ),
            ) );
            $this -> set_option( array (
                'field_type' => 'select',
                'name'       => 'remove_author_margin_bottom',
                'title'      => esc_html__( 'Remove Margin Bottom for mobile', 'ginevra-core' ),
                'group'      => esc_html__( 'Layout', 'ginevra-core' ),
                'options'    => ginevra_core_get_select_type_options_pool( 'no_yes' ),
                'default_value' => '',
                'dependency' => array (
                    'show' => array (
                        'enable_author' => array (
                            'values'        => 'yes',
                            'default_value' => 'no',
                        ),
                    ),
                ),
            ) );
			$this->set_option(
				array(
					'field_type' => 'hidden',
					'name'       => 'is_widget_element',
					'title'      => esc_html__( 'Is Shortcode Widget', 'ginevra-core' ),
				)
			);
			$this->map_additional_options();
		}

		public static function call_shortcode( $params ) {
			$html = qode_framework_call_shortcode( 'ginevra_core_blog_list', $params );
			$html = str_replace( "\n", '', $html );

			return $html;
		}

		public function load_assets() {
			parent::load_assets();

			$is_allowed = apply_filters( 'ginevra_core_filter_load_blog_list_assets', false, $this->get_atts() );

			if ( $is_allowed ) {
				wp_enqueue_style( 'wp-mediaelement' );
				wp_enqueue_script( 'wp-mediaelement' );
				wp_enqueue_script( 'mediaelement-vimeo' );
			}
		}

		public function render( $options, $content = null ) {
			parent::render( $options );

			$atts = $this->get_atts();

			$atts['post_type']       = $this->get_post_type();
			$atts['taxonomy_filter'] = $this->get_post_type_filter_taxonomy( $atts );

			// Additional query args
			$atts['additional_query_args'] = $this->get_additional_query_args( $atts );

			$atts['holder_classes'] = $this->get_holder_classes( $atts );
			$atts['query_result']   = new \WP_Query( ginevra_core_get_query_params( $atts ) );
			$atts['slider_attr']    = $this->get_slider_data( $atts );
			$atts['data_attr']      = ginevra_core_get_pagination_data( GINEVRA_CORE_REL_PATH, 'blog/shortcodes', 'blog-list', 'post', $atts );

			$atts['this_shortcode'] = $this;

			return ginevra_core_get_template_part( 'blog/shortcodes/blog-list', 'templates/content', $atts['behavior'], $atts );
		}

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

			$holder_classes[] = 'qodef-blog';
			if ( ! empty( $atts['layout'] ) && 'standard' === $atts['layout'] ) {
				$holder_classes[] = 'qodef--list';
			}
			$holder_classes[] = ! empty( $atts['layout'] ) ? 'qodef-item-layout--' . $atts['layout'] : '';

			$list_classes   = $this->get_list_classes( $atts );
			$holder_classes = array_merge( $holder_classes, $list_classes );

			return implode( ' ', $holder_classes );
		}

		public function get_item_classes( $atts ) {
			$item_classes = $this->init_item_classes();

			$item_classes[] = 'qodef-blog-item';

			if( !empty($atts['remove_author_margin_bottom'])) {
			    $item_classes[] = 'remove-margin-bottom';
            }

			$list_item_classes = $this->get_list_item_classes( $atts );

			$item_classes = array_merge( $item_classes, $list_item_classes );

			return implode( ' ', $item_classes );
		}

		public function get_title_styles( $atts ) {
			$styles = array();

			if ( ! empty( $atts['text_transform'] ) ) {
				$styles[] = 'text-transform: ' . $atts['text_transform'];
			}

			if ( ! empty( $atts['title_color'] ) ) {
			    $styles[] = 'color: ' . $atts['title_color'];
            }

			return $styles;
		}

		public function get_author_styles ( $atts ) {
		    $styles = array();

		    if ( ! empty( $atts['author_color'] ) ) {
		        $styles[] = 'color: ' . $atts['author_color'];
            }

            if ( ! empty( $atts['author_margin_bottom'] ) ) {
		        $styles[] = 'margin-bottom: ' . $atts['author_margin_bottom'];
            }

            return $styles;
        }
	}
}
