<?php
namespace admin\Elementor;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Text_Shadow;
use WP_Query;
use WP_Post;

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

/**
 * Class Forumax_Topics
 * 
 * Forum topics widget from Docy theme
 *
 * @package admin\Elementor
 */
class Forumax_Topics extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.0.0
	 * @return string Widget name.
	 */
	public function get_name() {
		$theme      = wp_get_theme();
		$theme_name = strtolower( $theme->get( 'Name' ) );
		
		if ( 'docy' === $theme_name ) {
			return 'docy_forum_topics';
		} elseif ( 'docly' === $theme_name ) {
			return 'Docly_forum_posts';
		}
		
		return 'ama_forum_posts';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.0.0
	 * @return string Widget title.
	 */
	public function get_title() {
		return __( 'Topics Forum (Forumax)', 'forumax' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.0.0
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-post-list';
	}

	/**
	 * Get style dependencies.
	 *
	 * @since 1.0.0
	 * @return array Style dependencies.
	 */
	public function get_style_depends() {
		return array( 'bbpc-el-widgets' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.0.0
	 * @return array Widget keywords.
	 */
	public function get_keywords() {
		return array( 'topics', 'replies' );
	}

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

	/**
	 * Register widget controls.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	protected function register_controls() {
		
		// Preset Skins Section
		$this->start_controls_section(
			'style_sec',
			array(
				'label' => esc_html__( 'Preset Skins', 'forumax' ),
			)
		);

		$this->add_control(
			'style',
			array(
				'label'   => esc_html__( 'Skin', 'forumax' ),
				'type'    => Controls_Manager::VISUAL_CHOICE,
				'options' => array(
					'1' => array(
						'image' => FORUMAX_ASSETS . '/admin/images/skins/topic1.jpg',
						'title' => esc_html__( 'List', 'forumax' ),
					),
					'2' => array(
						'image' => FORUMAX_ASSETS . '/admin/images/skins/topic2.jpg',
						'title' => esc_html__( 'List with excerpt', 'forumax' ),
					),
				),
				'default' => '1',
				'toggle'  => false,
			)
		);

		$this->end_controls_section();

		// Filter Options Section
		$this->start_controls_section(
			'filter_opt',
			array(
				'label' => __( 'Filter Options', 'forumax' ),
			)
		);

		$this->add_control(
			'ppp',
			array(
				'label'       => esc_html__( 'Show Topics', 'forumax' ),
				'type'        => Controls_Manager::NUMBER,
				'label_block' => true,
				'default'     => 5,
			)
		);

		$this->add_control(
			'author_avatar_count',
			array(
				'label'       => esc_html__( 'Author Avatars', 'forumax' ),
				'type'        => Controls_Manager::NUMBER,
				'label_block' => true,
				'default'     => 2,
				'condition'   => array(
					'style' => array( '2' ),
				),
			)
		);

		$this->add_control(
			'excerpt_length',
			array(
				'label'       => esc_html__( 'Excerpt Length', 'forumax' ),
				'description' => esc_html__( 'Enter the excerpt length in words count.', 'forumax' ),
				'type'        => Controls_Manager::NUMBER,
				'label_block' => true,
				'default'     => 12,
				'condition'   => array(
					'style' => array( '2' ),
				),
			)
		);

		$this->add_control(
			'order',
			array(
				'label'   => esc_html__( 'Order', 'forumax' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'ASC'  => 'ASC',
					'DESC' => 'DESC',
				),
				'default' => 'ASC',
			)
		);

		$this->end_controls_section();

		// Bottom Content Section
		$this->start_controls_section(
			'btm_content_sec',
			array(
				'label' => __( 'Bottom Content', 'forumax' ),
			)
		);

		$this->add_control(
			'btm_content',
			array(
				'label' => esc_html__( 'Bottom Text', 'forumax' ),
				'type'  => Controls_Manager::WYSIWYG,
			)
		);

		$this->end_controls_section();

		// Style Items Section
		$this->start_controls_section(
			'style_items',
			array(
				'label' => __( 'Style Items', 'forumax' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'spacing-item',
			array(
				'label'       => __( 'Spacing', 'forumax' ),
				'description' => __( 'Spacing between the topic items', 'forumax' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors'   => array(
					'{{WRAPPER}} .topic-item' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'padding-item',
			array(
				'label'      => __( 'Padding', 'elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .topic-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// Style Section
		$this->start_controls_section(
			'style_shapes',
			array(
				'label' => __( 'Style Section', 'forumax' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'shape1',
			array(
				'label'     => esc_html__( 'Shape 01', 'forumax' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array(
					'url' => plugins_url( 'inc/forum-topics/images/community_bg_shap_one.png', __FILE__ ),
				),
				'condition' => array(
					'style' => array( '2' ),
				),
			)
		);

		$this->add_control(
			'shape2',
			array(
				'label'     => esc_html__( 'Shape 02', 'forumax' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array(
					'url' => plugins_url( 'inc/forum-topics/images/community_bg_shap_two.png', __FILE__ ),
				),
				'condition' => array(
					'style' => array( '2' ),
				),
			)
		);

		$this->add_responsive_control(
			'padding_sec',
			array(
				'label'      => __( 'Section Padding', 'elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .bew-topics' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	protected function render() {
		$settings = $this->get_settings();
		
		$forum_posts = new WP_Query(
			array(
				'post_type'      => 'topic',
				'posts_per_page' => ! empty( $settings['ppp'] ) ? $settings['ppp'] : -1,
				'order'          => $settings['order'],
			)
		);
		
		// Whitelist valid style values to prevent Local File Inclusion
		$allowed_styles = array( '1', '2' );
		$style          = isset( $settings['style'] ) && in_array( $settings['style'], $allowed_styles, true ) ? $settings['style'] : '1';
		
		include __DIR__ . "/inc/forum-topics/forum-topics-{$style}.php";
	}
}
