<?php

/*
 * Football Pool WordPress plugin
 *
 * @copyright Copyright (c) 2026 Antoine Hurkmans
 * @link https://wordpress.org/plugins/football-pool/
 * @license https://plugins.svn.wordpress.org/football-pool/trunk/COPYING
 *
 * This file is part of Football pool.
 *
 * Football pool is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * Football pool is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Football pool.
 * If not, see <https://www.gnu.org/licenses/>.
 */

/**
 * Widget: Ranking Widget
 */

defined( 'ABSPATH' ) or die( 'Cannot access widgets directly.' );
add_action( 'widgets_init', function() { register_widget( 'Football_Pool_Ranking_Widget' ); } );

class Football_Pool_Ranking_Widget extends Football_Pool_Widget {
	protected $widget = [];

	public function __construct() {
		$this->widget = array(
			'name' => __( 'Ranking Widget', 'football-pool' ),
			'description' => __( 'this widget displays the top X players in the pool.', 'football-pool' ),
			'do_wrapper' => true,

			'fields' => array(
				array(
					'name' => __( 'Title', 'football-pool' ),
					'desc' => '',
					'id' => 'title',
					'type' => 'text',
					'std' => __( 'standing', 'football-pool' )
				),
				array(
					'name'    => __( 'Ranking', 'football-pool' ),
					'desc' => '',
					'id'      => 'ranking_id',
					'type'    => 'select',
					'options' => [] // get data from the database later on
				),
				array(
					'name' => __( 'Number of users to show', 'football-pool' ),
					'desc' => '',
					'id' => 'num_users',
					'type' => 'text',
					'std' => '5'
				),
				array(
					'name'    => __( 'Show players from this league', 'football-pool' ),
					'desc' => '',
					'id'      => 'league',
					'type'    => 'select',
					'options' => [] // get data from the database later on
				),
			)
		);

		$classname = str_replace( '_', '', get_class( $this ) );
		parent::__construct(
			$classname,
			$this->widget['name'] ?? $classname,
			$this->widget['description']
		);
	}

	public function initiate_widget_dynamic_fields() {
		$pool = footballpool();
		// get the ranking-options from the database
		$rankings = $pool->get_rankings();
		$options = [];
		foreach ( $rankings as $ranking ) {
			$options[$ranking['id']] = Football_Pool_Utils::xssafe( $ranking['name'] );
		}
		$this->widget['fields'][1]['options'] = $options;
		// get the league-options from the database
		$leagues = $pool->get_leagues();
		$options = [];
		foreach ( $leagues as $league ) {
			$options[$league['league_id']] = Football_Pool_Utils::xssafe( $league['league_name'] );
		}
		$this->widget['fields'][3]['options'] = $options;
	}

	public function html( string $title, array $args, array $instance ) {
		$pool = footballpool();

		extract( $args );
		
		$num_users = isset( $instance['num_users'] ) ? (int) $instance['num_users'] : 5;
		$league = ! empty( $instance['league'] ) ? (int) $instance['league'] : FOOTBALLPOOL_LEAGUE_ALL;
		$ranking_id = ! empty( $instance['ranking_id'] ) ? (int) $instance['ranking_id'] : FOOTBALLPOOL_RANKING_DEFAULT;
		
		if ( $title !== '' ) {
			/** @var string $before_title */
			/** @var string $after_title */
			echo $before_title, $title, $after_title;
		}
		
		$user_id = get_current_user_id();

//		$userpage = Football_Pool::get_page_link( 'user' );
		
		$ranking = $pool->get_pool_ranking_limited( $league, $num_users, $ranking_id );
		if ( count( $ranking ) > 0 ) {
			$users = [];
			foreach ( $ranking as $row ) $users[] = $row['user_id'];
			
			echo $pool->print_pool_ranking( $league, $user_id, $users, $ranking, $ranking_id, 'widget', $num_users );
		} else {
			printf( '<p>%s</p>', __( 'No match data available.', 'football-pool' ) );
		}
	}
}
