<?php

if ( ! function_exists( 'ginevra_core_add_horizontal_showcase_shortcode' ) ) {
	/**
	 * Function that add shortcode into shortcodes list for registration
	 *
	 * @param array $shortcodes
	 *
	 * @return array
	 */
	function ginevra_core_add_horizontal_showcase_shortcode( $shortcodes ) {
		$shortcodes[] = 'GinevraCore_Horizontal_Showcase_Shortcode';
		
		return $shortcodes;
	}
	
	add_filter( 'ginevra_core_filter_register_shortcodes', 'ginevra_core_add_horizontal_showcase_shortcode' );
}

if ( class_exists( 'GinevraCore_Shortcode' ) ) {
	class GinevraCore_Horizontal_Showcase_Shortcode extends GinevraCore_List_Shortcode {
        public $num_of_items = 4;

        public function __construct() {
            $this->set_post_type( 'portfolio-item' );
            $this->set_post_type_taxonomy( 'portfolio-category' );

            parent::__construct();
        }
		
		public function map_shortcode() {
			$this->set_shortcode_path( GINEVRA_CORE_CPT_URL_PATH . '/portfolio/shortcodes/horizontal-showcase' );
			$this->set_base( 'ginevra_core_horizontal_showcase' );
			$this->set_name( esc_html__( 'Horizontal Showcase', 'ginevra-core' ) );
			$this->set_description( esc_html__( 'Shortcode that adds horizontal layout holder', 'ginevra-core' ) );
			$this->set_category( esc_html__( 'Ginevra Core', 'ginevra-core' ) );
			$this->set_scripts(
				array_merge(
					array(
						'SmoothScrollbar'        => array(
							'registered' => false,
							'url'        => GINEVRA_CORE_CPT_URL_PATH . '/portfolio/shortcodes/horizontal-showcase/assets/js/plugins/smooth-scrollbar.js',
							'dependency' => array( 'jquery' )
						),
						'HorizontalScrollPlugin' => array(
							'registered' => false,
							'url'        => GINEVRA_CORE_CPT_URL_PATH . '/portfolio/shortcodes/horizontal-showcase/assets/js/plugins/HorizontalScrollPlugin.js',
							'dependency' => array( 'jquery' )
						),
						'overscroll'             => array(
							'registered' => false,
							'url'        => GINEVRA_CORE_CPT_URL_PATH . '/portfolio/shortcodes/horizontal-showcase/assets/js/plugins/overscroll.js',
							'dependency' => array( 'jquery' )
						)
					), apply_filters( 'ginevra_core_filter_horizontal_showcase_register_assets', array() )
				)
			);
			$this->set_option(
			    array(
                    'field_type' => 'text',
                    'name'       => 'custom_class',
                    'title'      => esc_html__( 'Custom Class', 'ginevra-core' )
                )
            );
			$this->set_option(
			    array(
                    'field_type' => 'image',
                    'name'       => 'featured_image',
                    'title'      => esc_html__('Featured Image', 'ginevra-core')
                )
            );
			$this->set_option(
                array(
                    'field_type'    => 'text',
                    'name'          => 'featured_title',
                    'title'         => esc_html__( 'Vertical Title', 'ginevra-core' )
                )
            );
            $this->set_option(
                array(
                    'field_type'    => 'color',
                    'name'          => 'featured_title_color',
                    'title'         => esc_html__( 'Vertical Title Color', 'ginevra-core' ),
                    'group'         => esc_html__( 'Vertical Title Style', 'ginevra-core' )
                )
            );
			$this->set_option(
			    array(
                    'field_type' => 'image',
                    'name'       => 'images',
                    'multiple'   => 'yes',
                    'title'      => esc_html__( 'Carousel Images', 'ginevra-core' )
                )
            );
			$this->set_option(
			    array(
			        'field_type' => 'text',
                    'name'       => 'info_title',
                    'title'      => esc_html__( 'Info Title', 'ginevra-core' ),
                    'group'      => esc_html__( 'Info', 'ginevra-core' )
                )
            );
            $this->set_option(
                array(
                    'field_type' => 'color',
                    'name'       => 'info_title_color',
                    'title'      => esc_html__( 'Info Title Color', 'ginevra-core' ),
                    'group'      => esc_html__( 'Info Style', 'ginevra-core' )
                )
            );
            $this->set_option(
                array(
                    'field_type'  => 'text',
                    'name'        => 'item_line_break_positions',
                    'title'       => esc_html__( 'Positions of Line Break', 'ginevra-core' ),
                    'description' => esc_html__( 'Enter the positions of the words after which you would like to create a line break. Separate the positions with commas (e.g. if you would like the first, third, and fourth word to have a line break, you would enter "1,3,4")', 'ginevra-core' ),
                    'group'       => esc_html__( 'Info Style', 'ginevra-core' )
                )
            );
            $this->set_option(
                array(
                    'field_type'    => 'select',
                    'name'          => 'item_disable_title_break_words',
                    'title'         => esc_html__( 'Disable Title Line Break', 'ginevra-core' ),
                    'description'   => esc_html__( 'Enabling this option will disable title line breaks for screen size 1024 and lower', 'ginevra-core' ),
                    'options'       => ginevra_core_get_select_type_options_pool( 'no_yes', false ),
                    'default_value' => 'no',
                    'group'       => esc_html__( 'Info Style', 'ginevra-core' )
                )
            );
            $this->set_option(
                array(
                    'field_type'    => 'textarea',
                    'name'          => 'info_text',
                    'title'         => esc_html__( 'Info Text', 'ginevra-core' ),
                    'group'       => esc_html__( 'Info', 'ginevra-core' )
                )
            );
            $this->set_option(
                array(
                    'field_type' => 'color',
                    'name'       => 'info_text_color',
                    'title'      => esc_html__( 'Info Text Color', 'ginevra-core' ),
                    'group'      => esc_html__( 'Info Style', 'ginevra-core' )
                )
            );
            $this->set_option(
                array(
                    'field_type' => 'color',
                    'name'       => 'info_button_color',
                    'title'      => esc_html__( 'Info Button Color', 'ginevra-core' ),
                    'group'      => esc_html__( 'Info Style', 'ginevra-core' )
                )
            );
            $this->set_option(
                array(
                    'field_type' => 'color',
                    'name'       => 'info_section_color',
                    'title'      => esc_html__('Info Slide Background Color', 'ginevra-core'),
                    'group'      => esc_html__('Info Style', 'ginevra-color')
                )
            );
            $this->set_option(
                array(
                    'field_type' => 'color',
                    'name'       => 'scroll_back_slide_color',
                    'title'      => esc_html__('Scroll Back Slide Color', 'ginevra-core'),
                    'group'      => esc_html__('Scroll Back Slide Style', 'ginevra-color')
                )
            );
            $this->set_option(
                array(
                    'field_type' => 'color',
                    'name'       => 'scroll_back_button_color',
                    'title'      => esc_html__('Scroll Back Button Color', 'ginevra-core'),
                    'group'      => esc_html__('Scroll Back Slide Style', 'ginevra-color')
                )
            );
            for ( $i = 1; $i <= $this->num_of_items; $i ++ ) {
                $this->set_option(
                    array(
                        'field_type' => 'text',
                        'name'       => 'text_icon_' . $i,
                        'title'      => sprintf( esc_html__( 'Text Icon %s', 'ginevra-core' ), $i ),
                        'group'      =>  esc_html__('Social Networks', 'ginevra-core'),
                    )
                );
                $this->set_option(
                    array(
                        'field_type' => 'text',
                        'name'       => 'link_' . $i,
                        'title'      => sprintf( esc_html__( 'Link %s', 'ginevra-core' ), $i ),
                        'group'      =>  esc_html__('Social Networks', 'ginevra-core'),
                    )
                );
                $this->set_option(
                    array(
                        'field_type'    => 'select',
                        'name'          => 'target_' . $i,
                        'title'         => sprintf( esc_html__( 'Link %s Target', 'ginevra-core' ), $i ),
                        'options'       => ginevra_core_get_select_type_options_pool( 'link_target', false ),
                        'default_value' => '_blank',
                        'group'      =>  esc_html__('Social Networks', 'ginevra-core'),
                    )
                );
                $this->set_option(
                    array(
                        'field_type' => 'color',
                        'name'       => 'icon_color_' . $i,
                        'title'      => sprintf( esc_html__( 'Icon %s Color', 'ginevra-core' ), $i ),
                        'group'      =>  esc_html__('Social Networks Style', 'ginevra-core'),
                    )
                );
                $this->set_option(
                    array(
                        'field_type' => 'color',
                        'name'       => 'icon_hover_color_' . $i,
                        'title'      => sprintf( esc_html__( 'Icon %s Hover Color', 'ginevra-core' ), $i ),
                        'group'      =>  esc_html__('Social Networks Style', 'ginevra-core'),
                    )
                );
            }
			$this->map_query_options( array( 'post_type' => $this->get_post_type() ) );
			$this->map_extra_options();
		}
		
		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['query_result']   = new \WP_Query( ginevra_core_get_query_params( $atts ) );
			$atts['holder_classes'] = $this->get_holder_classes( $atts );
			$atts['item_classes']   = $this->get_item_classes( $atts );
			$atts['this_shortcode'] = $this;
			
			return ginevra_core_get_template_part( 'post-types/portfolio/shortcodes/horizontal-showcase', 'templates/horizontal-showcase', '', $atts );
		}
		
		private function get_holder_classes( $atts ) {
			$holder_classes = $this->init_holder_classes();
			
			$holder_classes[] = 'qodef-horizontal-showcase qodef-custom-horizontal-slider';

			return implode( ' ', $holder_classes );
		}

		public function get_slide_classes( $slide_atts ) {
			$classes = array( 'qodef-e-item', 'qodef-horizontal-slide' );

			return implode( ' ', $classes );
		}

		public function get_slide_styles( $slide_atts ) {
			$styles = array();

			if ( ! empty( $slide_atts['background_color'] ) ) {
				$styles[] = 'background-color: ' . $slide_atts['background_color'];
			}

			return $styles;
		}

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

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

			$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_featured_title_styles( $atts ) {
            $styles = array();

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

            return $styles;
        }

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

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

            return $styles;
        }

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

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

            return $styles;
        }

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

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

            return $styles;
        }

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

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

            return $styles;
        }
	}
}