<?php
/**
 * Changelog Widget
 *
 * @package PCChangelogManager
 */

// Prevent direct access to the file.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Changelog Widget class.
 */
class PC_CLM_Widget extends WP_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'pc_clm_changelog_widget',
			__( 'Changelog', 'pc-changelog-manager-abc123' ),
			array(
				'description' => __( 'Display recent changelog entries.', 'pc-changelog-manager-abc123' ),
				'customize_selective_refresh' => true,
			)
		);
	}

	/**
	 * Output the widget content.
	 *
	 * @param array $args     Display arguments.
	 * @param array $instance Widget instance.
	 */
	public function widget( $args, $instance ) {
		// Enqueue styles.
		if ( file_exists( PC_CLM_PLUGIN_DIR . 'public/css/public-style.css' ) ) {
			wp_enqueue_style( 'pc-clm-public-style', PC_CLM_PLUGIN_URL . 'public/css/public-style.css', array(), PC_CLM_VERSION );
		}

		$title       = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Changelog', 'pc-changelog-manager-abc123' );
		$limit       = ! empty( $instance['limit'] ) ? intval( $instance['limit'] ) : 5;
		$show_date   = ! empty( $instance['show_date'] ) ? $instance['show_date'] : 'yes';
		$show_category = ! empty( $instance['show_category'] ) ? $instance['show_category'] : 'yes';

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

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

		// Query changelog entries.
		$query_args = array(
			'post_type'      => 'pc_changelog',
			'post_status'    => 'publish',
			'posts_per_page' => $limit,
			'orderby'        => 'date',
			'order'          => 'DESC',
		);

		$query = new WP_Query( $query_args );

		if ( $query->have_posts() ) {
			?>
			<ul class="pc-clm-widget-list">
				<?php
				while ( $query->have_posts() ) :
					$query->the_post();

					$post_id       = get_the_ID();
					$version       = get_post_meta( $post_id, '_pc_clm_version_number', true );
					$release_date  = get_post_meta( $post_id, '_pc_clm_release_date', true );
					$category      = get_post_meta( $post_id, '_pc_clm_category', true );
					$categories    = array(
						'new-features'   => __( 'New Features', 'pc-changelog-manager-abc123' ),
						'bug-fixes'      => __( 'Bug Fixes', 'pc-changelog-manager-abc123' ),
						'improvements'   => __( 'Improvements', 'pc-changelog-manager-abc123' ),
						'security'       => __( 'Security', 'pc-changelog-manager-abc123' ),
						'deprecated'     => __( 'Deprecated', 'pc-changelog-manager-abc123' ),
						'removed'        => __( 'Removed', 'pc-changelog-manager-abc123' ),
					);
					?>
					<li class="pc-clm-widget-entry">
						<div class="pc-clm-widget-entry-header">
							<?php if ( $version ) : ?>
								<span class="pc-clm-widget-version"><?php echo esc_html( $version ); ?></span>
							<?php endif; ?>

							<?php if ( 'yes' === $show_date && $release_date ) : ?>
								<span class="pc-clm-widget-date"><?php echo esc_html( date_i18n( 'M j, Y', strtotime( $release_date ) ) ); ?></span>
							<?php endif; ?>
						</div>

						<a href="<?php the_permalink(); ?>" class="pc-clm-widget-entry-title">
							<?php the_title(); ?>
						</a>

						<?php if ( 'yes' === $show_category && $category && isset( $categories[ $category ] ) ) : ?>
							<span class="pc-clm-widget-category pc-clm-category-<?php echo esc_attr( $category ); ?>">
								<?php echo esc_html( $categories[ $category ] ); ?>
							</span>
						<?php endif; ?>
					</li>
					<?php
				endwhile;
				?>
			</ul>
			<?php
			wp_reset_postdata();
		} else {
			?>
			<p class="pc-clm-widget-empty"><?php esc_html_e( 'No changelog entries found.', 'pc-changelog-manager-abc123' ); ?></p>
			<?php
		}

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

	/**
	 * Output the widget form in the admin.
	 *
	 * @param array $instance Current instance.
	 */
	public function form( $instance ) {
		$title       = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Changelog', 'pc-changelog-manager-abc123' );
		$limit       = ! empty( $instance['limit'] ) ? intval( $instance['limit'] ) : 5;
		$show_date   = ! empty( $instance['show_date'] ) ? $instance['show_date'] : 'yes';
		$show_category = ! empty( $instance['show_category'] ) ? $instance['show_category'] : 'yes';
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
				<?php esc_html_e( 'Title:', 'pc-changelog-manager-abc123' ); ?>
			</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( 'limit' ) ); ?>">
				<?php esc_html_e( 'Number of entries to show:', 'pc-changelog-manager-abc123' ); ?>
			</label>
			<input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'limit' ) ); ?>" type="number" min="1" max="20" value="<?php echo esc_attr( $limit ); ?>" />
		</p>

		<p>
			<input class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" type="checkbox" value="yes" <?php checked( $show_date, 'yes' ); ?> />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>">
				<?php esc_html_e( 'Show release date', 'pc-changelog-manager-abc123' ); ?>
			</label>
		</p>

		<p>
			<input class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_category' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_category' ) ); ?>" type="checkbox" value="yes" <?php checked( $show_category, 'yes' ); ?> />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_category' ) ); ?>">
				<?php esc_html_e( 'Show category', 'pc-changelog-manager-abc123' ); ?>
			</label>
		</p>
		<?php
	}

	/**
	 * Save widget settings.
	 *
	 * @param array $new_instance New instance.
	 * @param array $old_instance Old instance.
	 * @return array
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = array();

		$instance['title']          = ! empty( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '';
		$instance['limit']          = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : 5;
		$instance['show_date']      = ! empty( $new_instance['show_date'] ) ? 'yes' : 'no';
		$instance['show_category']  = ! empty( $new_instance['show_category'] ) ? 'yes' : 'no';

		return $instance;
	}
}
