<?php
/**
 * Forumax Forums Widget
 *
 * @package Forumax
 * @subpackage Elementor
 */

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_Forums
 * Forums widget from Docy theme
 *
 * @package admin\Elementor
 */
class Forumax_Forums extends Widget_Base {


	/**
	 * Get widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		$theme      = wp_get_theme();
		$theme_name = strtolower( $theme->get( 'Name' ) );

		if ( 'docy' === $theme_name ) {
			return 'docy_forums';
		} elseif ( 'docly' === $theme_name ) {
			return 'Docly_forums';
		}
		return 'ama_forums';
	}

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

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

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

	/**
	 * Get script dependencies.
	 *
	 * @return array Script dependencies.
	 */
	public function get_script_depends() {
		return array( 'bbpc-ajax' );
	}

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

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

		$this->add_control(
			'style',
			array(
				'label'   => esc_html__( 'Forums Style', 'forumax' ),
				'type'    => Controls_Manager::VISUAL_CHOICE,
				'options' => array(
					'1' => array(
						'image' => FORUMAX_ASSETS . '/admin/images/skins/docy-forums-1.jpg',
						'title' => esc_html__( '01 : Forums', 'forumax' ),
					),
					'2' => array(
						'image' => FORUMAX_ASSETS . '/admin/images/skins/docy-forums-2.jpg',
						'title' => esc_html__( '02 : Forums', 'forumax' ),
					),
				),
				'default' => '1',
				'toggle'  => false,
			)
		);

		$this->end_controls_section(); // End Style.

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

		$this->add_control(
			'ppp',
			array(
				'label'       => esc_html__( 'Show Forums', 'forumax' ),
				'description' => esc_html__( 'Show the forums count at the initial view. Default is 5 forums in a row.', 'forumax' ),
				'type'        => Controls_Manager::NUMBER,
				'label_block' => true,
				'default'     => 6,
			)
		);

		$this->add_control(
			'ppp2',
			array(
				'label'       => esc_html__( 'Hidden Forums', 'forumax' ),
				'description' => esc_html__( 'Hidden forums will show on clicking on the More button.', 'forumax' ),
				'type'        => Controls_Manager::NUMBER,
				'label_block' => true,
				'default'     => 10,
			)
		);

		$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();

		// --- Read More Options
		$this->start_controls_section(
			'button_opt',
			array(
				'label' => __( 'Read More Options', 'forumax' ),
			)
		);

		$this->add_control(
			'more_txt',
			array(
				'label'       => esc_html__( 'Read More Text', 'forumax' ),
				'type'        => Controls_Manager::TEXT,
				'label_block' => true,
				'default'     => 'More Communities',
			)
		);

		$this->add_control(
			'less_txt',
			array(
				'label'       => esc_html__( 'Read Less Text', 'forumax' ),
				'type'        => Controls_Manager::TEXT,
				'label_block' => true,
				'default'     => 'Less Communities',
			)
		);

		$this->end_controls_section();

		// Background shape Image.
		$this->start_controls_section(
			'style_shapes',
			array(
				'label' => __( 'Shape Images', 'forumax' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'shape1',
			array(
				'label'   => esc_html__( 'Shape', 'forumax' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array(
					'url' => plugins_url( 'inc/forums/images/shap_white.png', __FILE__ ),
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render widget output.
	 *
	 * @return void
	 */
	protected function render() {
		$settings = $this->get_settings();

		$forums = new WP_Query(
			array(
				'post_type'      => 'forum',
				'posts_per_page' => ! empty( $settings['ppp'] ) ? $settings['ppp'] : 5,
				'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';
		// File Include.
		include __DIR__ . "/inc/forums/forums-{$style}.php";
	}
}
