<?php
/**
 *  Elementor widget file.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/admin/ElementAddons
 */

/**
 * Elementor smart post show pro shortcode Widget.
 *
 * @since 2.4.1
 */
class Smart_Post_Show_Shortcode_Widget extends \Elementor\Widget_Base {
	/**
	 * Get widget name.
	 *
	 * @since 2.4.1
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'smart_post_show_pro_shortcode';
	}

	/**
	 * Get widget title.
	 *
	 * @since 2.4.1
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return __( 'Smart Post Pro', 'smart-post-show-pro' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 2.2.5
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'sps-icon-elementor';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 2.2.5
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return array( 'basic' );
	}

	/**
	 * Get all post list.
	 *
	 * @since 2.2.5
	 * @return array
	 */
	public function sp_pcp_post_list() {
		$post_list    = array();
		$sp_pcp_posts = new \WP_Query(
			array(
				'post_type'      => 'sp_post_carousel',
				'post_status'    => 'publish',
				'posts_per_page' => 10000,
			)
		);
		$posts        = $sp_pcp_posts->posts;
		foreach ( $posts as $post ) {
			$post_list[ $post->ID ] = $post->post_title;
		}
		krsort( $post_list );
		return $post_list;
	}

	/**
	 * Controls register.
	 *
	 * @return void
	 */
	protected function register_controls() {
		$this->start_controls_section(
			'content_section',
			array(
				'label' => __( 'Content', 'smart-post-show-pro' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'sp_smart_post_show_pro_shortcode',
			array(
				'label'       => __( 'Smart Post Pro Shortcode(s)', 'smart-post-show-pro' ),
				'type'        => \Elementor\Controls_Manager::SELECT2,
				'label_block' => true,
				'default'     => '',
				'options'     => $this->sp_pcp_post_list(),
			)
		);

		$this->end_controls_section();

	}

	/**
	 * Render smart post show pro shortcode widget output on the frontend.
	 *
	 * @since 2.4.1
	 * @access protected
	 */
	protected function render() {

		$settings         = $this->get_settings_for_display();
		$sp_pcp_shortcode = $settings['sp_smart_post_show_pro_shortcode'];

		if ( '' === $sp_pcp_shortcode ) {
			echo '<div style="text-align: center; margin-top: 0; padding: 10px" class="elementor-add-section-drag-title">Select a shortcode</div>';
			return;
		}

		$generator_id = (int) esc_attr( $sp_pcp_shortcode );

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$smart_show_id = $generator_id;
			// Preset Layouts.
			$layout = get_post_meta( $smart_show_id, 'sp_pcp_layouts', true );
			// All the visible options for the Shortcode like – Global, Filter, Display, Popup, Typography etc.
			$view_options  = get_post_meta( $smart_show_id, 'sp_pcp_view_options', true );
			$section_title = get_the_title( $smart_show_id );
			$layouts       = $layout;
			// Load dynamic style.
			$dynamic_style = Smart_Post_Show_Pro_Public::load_dynamic_style( $smart_show_id, $view_options, $layouts );
			$enqueue_fonts = Smart_Post_Show_Pro_Public::load_google_fonts( $dynamic_style['typography'] );
			// Enqueue Google fonts.
			if ( ! empty( $enqueue_fonts ) ) {
				echo '<link rel="stylesheet" href="' . esc_url( add_query_arg( 'family', rawurlencode( implode( '|', $enqueue_fonts ) ), '//fonts.googleapis.com/css' ) ) . '" media="all">'; //phpcs:ignore
			}
			echo '<style id="smart_post_show_dynamic_css' . esc_attr($smart_show_id) . '">' . wp_strip_all_tags($dynamic_style['dynamic_css'] ). '</style>';//phpcs:ignore
			SP_PCP_HTML::pcp_html_show( $view_options, $layout, $smart_show_id, $section_title );
			?>
			<script src="<?php echo esc_url( SP_PCP_URL . 'public/assets/js/isotope.min.js' ); ?>" ></script>
			<script src="<?php echo esc_url( SP_PCP_URL . 'public/assets/js/scripts.min.js' ); ?>" ></script>
			<?php
		} else {
			echo do_shortcode( '[smart_post_show id="' . $generator_id . '"]' );
		}
	}
}
