<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Text_Shadow;

use EcafeAddons\EcafeElementLoad;

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

class Ecafe_Post_Content extends Widget_Base {

	public function get_name() {
		return 'ecafe-post-content';
	}

	public function get_title() {
		return esc_html__( 'Post Content', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-post-content-icon';
	}

	public function get_categories() {
		return array( 'essential' );
	}

	public function get_keywords() {
		return array( 'post', 'content', 'excerpt' );
	}

	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}

	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	protected function register_controls() {

		$this->start_controls_section(
			'contentSection',
			array(
				'label' => esc_html__( 'Post Content', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'fetchContent',
			array(
				'type'    => Controls_Manager::SELECT,
				'label'   => esc_html__( 'Fetch', 'essential-classy-addons-for-elementor' ),
				'default' => 'content',
				'options' => array(
					'content' => esc_html__( 'Content', 'essential-classy-addons-for-elementor' ),
					'excerpt' => esc_html__( 'Excerpt', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'fetchEditor',
			array(
				'type'      => Controls_Manager::SELECT,
				'label'     => esc_html__( 'Editor', 'essential-classy-addons-for-elementor' ),
				'default'   => 'elementor',
				'options'   => array(
					'elementor' => esc_html__( 'Elementor', 'essential-classy-addons-for-elementor' ),
					'wordpress' => esc_html__( 'Wordpress', 'essential-classy-addons-for-elementor' ),
				),
				'condition' => array(
					'fetchContent' => 'content',
				),
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdoclink = esc_url( 'https://ecaddons.com/docs/post-content-blog-builder-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		/*content start*/
		$this->start_controls_section(
			'contentStyling',
			array(
				'label' => esc_html__( 'Content', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'conTextAlign',
			array(
				'label'     => esc_html__( 'Text Alignment', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}}' => 'text-align: {{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'conJustify',
			array(
				'label'     => esc_html__( 'Justify Align', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}}' => 'justify-content:{{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'conPadding',
			array(
				'label'      => esc_html__( 'Padding', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}}' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'conTypo',
				'selector' => '{{WRAPPER}}',
			)
		);
		$this->start_controls_tabs( 'tabsContyle' );
		$this->start_controls_tab(
			'tabConN',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'conColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}}' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'conBg',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}}',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'conBorder',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}}',
			)
		);
		$this->add_responsive_control(
			'conBr',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}}' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'conShadow',
				'selector' => '{{WRAPPER}}',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			array(
				'name'     => 'conTextShadow',
				'label'    => esc_html__( 'Text Shadow', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}}',
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabConH',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'conColorH',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}}:hover' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'conBgH',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}}:hover',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'conBorderH',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}}:hover',
			)
		);
		$this->add_responsive_control(
			'conBrH',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}}:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'conShadowH',
				'selector' => '{{WRAPPER}}:hover',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			array(
				'name'     => 'conTextShadowH',
				'label'    => esc_html__( 'Text Shadow', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}}:hover',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		/*content start*/
	}

	protected function render( $wrapper = false ) {

		$settings     = $this->get_settings_for_display();
		$fetchContent = ! empty( $settings['fetchContent'] ) ? $settings['fetchContent'] : 'content';

		$postId = get_the_ID();

		if ( ! empty( $fetchContent ) && $fetchContent == 'excerpt' ) {
			if ( is_archive() ) {
				$description = '';
				if ( is_category() ) {
					$term        = get_queried_object();
					$description = category_description( $term->term_id );
				} elseif ( is_tag() ) {
					$term        = get_queried_object();
					$description = tag_description( $term->term_id );
				} elseif ( is_tax() ) {
					$term        = get_queried_object();
					$description = term_description( $term->term_id, $term->taxonomy );
				} elseif ( is_author() ) {
					$author_id   = get_queried_object_id();
					$description = get_the_author_meta( 'description', $author_id );
				}
				echo $description;
			} else {
				the_excerpt( $postId );
			}
		} else {
			$fetchEditor = ! empty( $settings['fetchEditor'] ) ? $settings['fetchEditor'] : 'elementor';
			if ( ! empty( $fetchEditor ) ) {
				if ( $fetchEditor == 'elementor' ) {
					static $posts = array();
					$post         = get_post();
					if ( $post === null ) {
						return;
					}
					if ( post_password_required( $post->ID ) ) {
						echo get_the_password_form( $post->ID );
						return;
					}
					if ( isset( $posts[ $post->ID ] ) ) {
						return;
					}

					$posts[ $post->ID ] = true;

					if ( \Elementor\Plugin::$instance->preview->is_preview_mode( $post->ID ) ) {
						$fetchData = \Elementor\Plugin::$instance->preview->builder_wrapper( '' );
					} else {
						if ( \Elementor\Plugin::$instance->documents->get( $post->ID ) ) {
							$document = \Elementor\Plugin::$instance->documents->get( $post->ID );
							if ( $document ) {
								$preview_type = $document->get_settings( 'preview_type' );
								$preview_id   = $document->get_settings( 'preview_id' );

								if ( is_string( $preview_type ) && 0 === strpos( $preview_type, 'single' ) && ! empty( $preview_id ) ) {
									$post = get_post( $preview_id );
									if ( ! $post ) {
										return;
									}
								}
							}
						}

						\Elementor\Plugin::$instance->editor->set_edit_mode( false );

						$fetchData = \Elementor\Plugin::$instance->frontend->get_builder_content( $post->ID, true );

						if ( empty( $fetchData ) ) {
							\Elementor\Plugin::$instance->frontend->remove_content_filter();
							setup_postdata( $post );
							$fetchData = apply_filters( 'the_content', get_the_content() );
							wp_link_pages(
								array(
									'before'      => '<div class="page-links elementor-page-links"><span class="page-links-title elementor-page-links-title">' . __( 'Pages:', 'essential-classy-addons-for-elementor' ) . '</span>',
									'after'       => '</div>',
									'link_before' => '<span>',
									'link_after'  => '</span>',
									'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'essential-classy-addons-for-elementor' ) . ' </span>%',
									'separator'   => '<span class="screen-reader-text">, </span>',
								)
							);

							\Elementor\Plugin::$instance->frontend->add_content_filter();

							return;
						} else {
							$fetchData = apply_filters( 'the_content', $fetchData );
						}
					}

					\Elementor\Plugin::$instance->editor->set_edit_mode( \Elementor\Plugin::$instance->editor->is_edit_mode() );

					if ( $wrapper ) {
						echo '<div class="ec-post-content ec-post-content-elementor">' . balanceTags( $fetchData, true ) . '</div>';
					} else {
						echo $fetchData;
					}
				} elseif ( $fetchEditor == 'WordPress' ) {
					static $views_ids = array();
					$post_id          = get_the_ID();
					if ( ! isset( $post_id ) ) {
						return '';
					}

					$views_ids[ $post_id ] = true;

					global $current_screen;
					if ( isset( $current_screen ) && method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
						$fetchData = wp_strip_all_tags( get_the_content( '', true, $post ) );
					} else {
						$post = get_post( $post_id );
						if ( ! $post ) {
							return '';
						}
						if ( ( 'publish' !== $post->post_status && 'draft' !== $post->post_status && 'private' !== $post->post_status ) || ! empty( $post->post_password ) ) {
							return '';
						}

						$fetchData = apply_filters( 'the_content', $post->post_content );
						if ( $fetchData ) {
							echo '<div class="ec-post-content ec-post-content-wordpress">' . balanceTags( $fetchData, true ) . '</div>';
						}
					}
					unset( $views_ids[ $post_id ] );
				}
			}
		}
	}
}
