<?php

class Ekc_Email_Status_Table extends WP_List_Table {

	protected $tournament_id;

	public function __construct( $tournament_id ) {
		parent::__construct();
		$this->tournament_id = $tournament_id;
	}

	protected function get_table_classes() {
		$table_classes = parent::get_table_classes();
		// remove 'fixed' css class
		return array_filter( $table_classes, fn( $table_class ) => $table_class !== 'fixed' );
	}

	function get_columns() {
		$columns = array(
			'status_time' => esc_html__( 'Timestamp', 'ekc-tournament-manager' ),
			'email_status' => esc_html__( 'Status', 'ekc-tournament-manager' ),
			'team' => esc_html__( 'Team', 'ekc-tournament-manager' ),
			'is_active' => esc_html__( 'Active', 'ekc-tournament-manager' ),
			'email' => esc_html__( 'Email', 'ekc-tournament-manager' ),
			'email_message_id' => esc_html__( 'MessageID', 'ekc-tournament-manager' ),
			'scheduled_batch' => esc_html__( 'Batch', 'ekc-tournament-manager' ),
			'status_message' => esc_html__( 'Status message', 'ekc-tournament-manager' )
		);
		return $columns;
	}

	function get_sortable_columns() {
		$sortable_columns = array(
			'status_time' => array( 'status_time', true ),
			'email_status' => array( 'email_status', false ),
			'team' => array( 'team', false ),
			'is_active' => array( 'is_active', false ),
			'email'	=> array( 'email', false ),
			'email_message_id' => array( 'email_message_id', false ),
			'scheduled_batch' => array( 'batch', false ),
			'status_message' => array( 'status_message', false )
		);
		return $sortable_columns;
	  }

	function prepare_items() {
		$columns = $this->get_columns();
		$hidden = array();
		$sortable = $this->get_sortable_columns();
		$this->_column_headers = array($columns, $hidden, $sortable);

		$validation_helper = new Ekc_Validation_Helper();
		$db = new Ekc_Database_Access();
		$order_by_column = $validation_helper->validate_get_text( 'orderby' );
		$order = $validation_helper->validate_get_text( 'order' );
		// $order_by_column and $order are validated in get_email_status_as_table
		$email_status = $db->get_email_status_as_table( $this->tournament_id, $order_by_column, $order, $this->get_filter() );
		$this->items = $email_status;
	}

	private function get_filter() {
		$validation_helper = new Ekc_Validation_Helper();
		$filter = array();
		
		$filter_status =  $validation_helper->validate_get_text( 'filter-status' );
		if ( in_array( $filter_status, array_keys( Ekc_Drop_Down_Helper::EMAIL_STATUS ) ) ) {
			$filter['email_status'] = $filter_status;
		}
	
		return $filter;
	}

	function column_status_time( $item ) {
		$actions = array();
		$nonce_helper = new Ekc_Nonce_Helper();
		$validation_helper = new Ekc_Validation_Helper();
		$page = $validation_helper->validate_get_text( 'page' );
		$tournament_id = $validation_helper->validate_get_key( 'tournamentid' );
		$team_id = $item['team_id'];
		$status = $item['email_status'];

		if ( $status === Ekc_Drop_Down_Helper::EMAIL_STATUS_SCHEDULED ) {
			// menus send and cancel for status scheduled
			$send_url = sprintf( '?page=%s&action=%s&teamid=%s&tournamentid=%s', $page, 'send-email', $team_id, $tournament_id );
			$send_url = $nonce_helper->nonce_url( $send_url, $nonce_helper->nonce_text( 'send-email', 'team', $team_id ) );
			$actions['send-email'] = sprintf( '<a href="%s">%s</a>', esc_url( $send_url ), esc_html__( 'Send', 'ekc-tournament-manager' ) );

			$cancel_url = sprintf( '?page=%s&action=%s&teamid=%s&tournamentid=%s', $page, 'cancel', $team_id, $tournament_id );
			$cancel_url = $nonce_helper->nonce_url( $cancel_url, $nonce_helper->nonce_text( 'cancel', 'team', $team_id ) );
			$actions['cancel'] = sprintf( '<a href="%s">%s</a>', esc_url( $cancel_url ), esc_html__( 'Cancel', 'ekc-tournament-manager' ) );
		}
		else {
			// menu re-send for status sent and failed
			$resend_url = sprintf( '?page=%s&action=%s&teamid=%s&tournamentid=%s', $page, 'resend-email', $team_id, $tournament_id );
			$resend_url = $nonce_helper->nonce_url( $resend_url, $nonce_helper->nonce_text( 'resend-email', 'team', $team_id ) );
			$actions['resend-email'] = sprintf( '<a href="%s">%s</a>',  esc_url( $resend_url ), esc_html__( 'Re-send', 'ekc-tournament-manager' ) );
		}

		return sprintf( '%s %s', esc_html( $item['status_time'] ), $this->row_actions( $actions ) );
	}

	function column_default( $item, $column_name ) {
		switch( $column_name ) {
			case 'email_status':
			case 'team':
			case 'is_active':
			case 'email':
			case 'email_message_id':
			case 'scheduled_batch':
			case 'status_message':
			return esc_html( $item[ $column_name ] );
			default:
			return '';
		}
	}


	function no_items() {
		esc_html_e( 'No data available.', 'ekc-tournament-manager' );
	}

	/**
	 * Complete override of pagination method of super class.
	 * We dont use pagination, but simply display the total number of items.
	 */
	protected function pagination( $which ) {
		$total_items = !$this->items ? 0 : count( $this->items );
?>
		<div class='tablenav-pages one-page'><span class='displaying-num'>
		<?php esc_html_e( sprintf(
			_n( '%s item', '%s items', $total_items ),
			number_format_i18n( $total_items ))) ?>
		</span></div>
<?php
	}
}

