<?php
namespace Forumax\WpWidgets;
use WP_Widget;
use WP_Query;

// Creating the widget
class Forumax_Topic_Info extends WP_Widget
{
	function __construct()
	{
		parent::__construct(
			'bbpress_forum_topic_info_widget', // Base ID of your widget
			__('(Forumax) Forum Topic Info', 'forumax'), // Widget name
			array('description' => __('Displays information about a Forumax forum topic', 'forumax'), ) // Widget description
		);
	}

	// Creating widget front-end
	public function widget($args, $instance)
	{
		global $post;

		if (!$post || !isset($post->ID) || $post->post_type != 'topic') {
			return;
		}

		$topic_id = $post->ID;
		$topic = get_post($topic_id);

		if (empty($topic) || $topic->post_type != 'topic') {
			return;
		}

		$forum_id = get_post_meta($topic_id, '_bbp_forum_id', true);
		$forum_url = get_permalink($forum_id);
		$forum_title = get_the_title($forum_id);
		$status = bbp_get_topic_status($topic_id);
		$replies = bbp_get_topic_reply_count($topic_id);
		$voices = bbp_get_topic_voice_count($topic_id);

		echo wp_kses_post($args['before_widget']);

		if (!empty($instance['title'])) {
			echo wp_kses_post($args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title']);
		}

		echo '<ul class="forumax-topic-info-widget">';
		echo '<li><span class="forumax-topic-info-label">' . esc_html__('Forum:', 'forumax') . '</span><span class="forumax-topic-info-value"><a href="' . esc_url($forum_url) . '">' . esc_html($forum_title) . '</a></span></li>';
		echo '<li><span class="forumax-topic-info-label">' . esc_html__('Topic:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html($topic->post_title) . '</span></li>';
		echo '<li><span class="forumax-topic-info-label">' . esc_html__('Author:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html(get_the_author_meta('display_name', $topic->post_author)) . '</span></li>';
		echo '<li><span class="forumax-topic-info-label">' . esc_html__('Date:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html(get_the_date('', $topic->ID)) . '</span></li>';
		echo '<li><span class="forumax-topic-info-label">' . esc_html__('Status:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html($status) . '</span></li>';
		echo '<li><span class="forumax-topic-info-label">' . esc_html__('Replies:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html($replies) . '</span></li>';
		echo '<li><span class="forumax-topic-info-label">' . esc_html__('Voices:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html($voices) . '</span></li>';
		echo '</ul>';

		echo wp_kses_post($args['after_widget']);
	}

	// Widget Backend
	public function form($instance)
	{
		$title = $instance['title'] ?? '';
		$topic_id = $instance['topic_id'] ?? '';
		// Widget admin form
		?>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
				<?php esc_html_e('Title:', 'forumax'); ?>
			</label>
			<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>"
				name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text"
				value="<?php echo esc_attr($title); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id('topic_id')); ?>">
				<?php esc_html_e('Topic ID:', 'forumax'); ?>
			</label>
			<input class="widefat" id="<?php echo esc_attr($this->get_field_id('topic_id')); ?>"
				name="<?php echo esc_attr($this->get_field_name('topic_id')); ?>" type="text"
				value="<?php echo esc_attr($topic_id); ?>" />
		</p>
		<?php
	}

	// Updating widget replacing old instances with new
	public function update($new_instance, $old_instance)
	{
		$instance = array();
		$instance['title'] = isset($new_instance['title']) ? sanitize_text_field($new_instance['title']) : '';
		$instance['topic_id'] = isset($new_instance['topic_id']) ? absint($new_instance['topic_id']) : '';

		return $instance;
	}
}

