<?php

/**
 * Page to list email status for a given tournament
 */
class Ekc_Email_Status_Page {

  public function intercept_redirect() {
    $validation_helper = new Ekc_Validation_Helper();
    $page = $validation_helper->validate_get_text( 'page' );
    if ( $page !== 'ekc-email-status' ) {
      return;
    }
    
    $action = $validation_helper->validate_get_text( 'action' );
    if ( ! $action ) {
      $action = $validation_helper->validate_post_text( 'action' );
    }
    if ( $action === 'send-email'
      || $action === 'resend-email'
      || $action === 'cancel'
      || $action === 'sendnextbatch' ) {
        $this->create_email_status_page();
      }
  }

	public function create_email_status_page() {
    $nonce_helper = new Ekc_Nonce_Helper();
    $validation_helper = new Ekc_Validation_Helper();
    $admin_helper = new Ekc_Admin_Helper();
    $action = $validation_helper->validate_get_text( 'action' );
    $tournament_id = $validation_helper->validate_get_key( 'tournamentid' );
    $team_id = $validation_helper->validate_get_key( 'teamid' );
    if ( $tournament_id && ! current_user_can( Ekc_Role_Helper::CAPABILITY_EKC_MANAGE_TOURNAMENTS, $tournament_id ) ) {
      return;
    }

    if ( $action === 'status' ) {
			$this->show_email_status( $tournament_id );
    }
    elseif ( $action === 'send-email' ) {
      if ( $nonce_helper->validate_nonce( $nonce_helper->nonce_text( $action, 'team', $team_id ) ) ) {
        $this->send_shareable_link_by_mail( $tournament_id, $team_id  );
      }
      $admin_helper->email_status_redirect( $tournament_id );
		}
		elseif ( $action === 'resend-email' ) {
      if ( $nonce_helper->validate_nonce( $nonce_helper->nonce_text( $action, 'team', $team_id ) ) ) {
        $this->send_shareable_link_by_mail( $tournament_id, $team_id  );
      }
      $admin_helper->email_status_redirect( $tournament_id );
    }
    elseif ( $action === 'cancel' ) {
      if ( $nonce_helper->validate_nonce( $nonce_helper->nonce_text( $action, 'team', $team_id ) ) ) {
        $this->cancel_scheduled_email( $tournament_id, $team_id );
      }
      $admin_helper->email_status_redirect( $tournament_id );
		}
    elseif ( $action === 'sendnextbatch' ) {
      if ( $nonce_helper->validate_nonce( $nonce_helper->nonce_text( $action, 'tournament', $tournament_id ) ) ) {
        $this->send_next_batch( $tournament_id );
      }
      $admin_helper->email_status_redirect( $tournament_id );
		}
  }

  private function show_email_status( $tournament_id ) {
    $db = new Ekc_Database_Access();
    $tournament = $db->get_tournament_by_id( $tournament_id );
    $this->show_wp_header( $tournament );
    $this->show_email_status_table( $tournament_id );
    $this->close_wp_content(); 
  }


  private function show_wp_header( $tournament ) {
    $nonce_helper = new Ekc_Nonce_Helper();
    $validation_helper = new Ekc_Validation_Helper();
    $page = $validation_helper->validate_get_text( 'page' );
    $tournament_id = $tournament->get_tournament_id();

    $send_next_batch_url = sprintf( '?page=%s&action=%s&tournamentid=%s', $page, 'sendnextbatch', $tournament_id );
    $send_next_batch_url = $nonce_helper->nonce_url( $send_next_batch_url, $nonce_helper->nonce_text( 'sendnextbatch', 'tournament', $tournament_id ) );
  
    ?>
    <div class="wrap">
    
      <h1 class="wp-heading-inline"><?php printf( '%s %s', esc_html( $tournament->get_name() ), esc_html__( 'Email Status', 'ekc-tournament-manager' ) ) ?></h1>
      <a href="<?php echo esc_url( $send_next_batch_url ) ?>" class="page-title-action"><?php esc_html_e( 'Send next batch', 'ekc-tournament-manager' ) ?></a>
      <hr class="wp-header-end">
    <?php 
  }

  /**
   * Close the css class 'wrap' div from wordpress
   */
  private function close_wp_content() {
    ?>
    </div><!-- .wrap -->
    <?php
  }
  
	public function show_email_status_table( $tournament_id ) {
		$email_status_table = new Ekc_Email_Status_Table( $tournament_id );
    $email_status_table->prepare_items();
    $email_status_table->display();
	}

  private function send_shareable_link_by_mail( $tournament_id, $team_id ) {
		$helper = new Ekc_Shareable_Links_Helper();
		$helper->send_shareable_link_by_mail( $tournament_id, $team_id );
  }

  private function cancel_scheduled_email( $tournament_id, $team_id ) {
		$helper = new Ekc_Shareable_Links_Helper();
		$helper->cancel_scheduled_email( $tournament_id, $team_id );
  }

  private function send_next_batch( $tournament_id ) {
    $helper = new Ekc_Shareable_Links_Helper();
    $helper->send_next_scheduled_batch( $tournament_id );
  }
}
