<?php

/**
 * Class to Add Column Names
 */
class GA_Column {

	/**
	 * Default definitions
	 * @var array
	 */
	public $defaults = array(
		'post_type' => 'post',
		'column_key' => '',
		'column_title' => '',
		'column_display' => 'column_key'
		);

	/**
	 * Container of our options for the column
	 * @var array
	 */
	public $options = array();

	/**
	 * Merge added options with the defaults. If the column key is not defined throw and exception.
	 * If we do not have a title for the column, create one from the column key
	 * @param array $options 
	 */
	public function __construct( $options ){

		$this->options = array_merge( $this->defaults, $options );

		if( $this->options['column_key'] == '' ){
			$message = __( 'Column key is not defined', 'yourtextdomain' );
			throw new Exception( $message );
		}

		if( $this->options['column_title'] == '' ) {
			$this->options['column_title'] = ucfirst( $this->options['column_key'] );
		}
	}

	/**
	 * Attach this column by using WordPress filters and actions
	 * If the post type is different from 'post' and 'page' then add a new word to filters and actions to dynamically target our columns
	 * If the post type is a page, then change the for_type variable so that the column is added to the pages section
	 * @return void 
	 */
	public function attach() {
		$post_type = '';
		if( $this->options['post_type'] != 'post' && $this->options['post_type'] != 'page'   ){
			$post_type = '_' . $this->options['post_type'];
		}

		$for_type = 'posts';
		if( $this->options['post_type'] == 'page' ) {
			$for_type = 'pages';
		}
		add_filter('manage' . $post_type . '_' . $for_type . '_columns' , array( $this, 'add_column' ) );

		add_action( 'manage' . $post_type . '_' . $for_type . '_custom_column', array( $this, 'column_data' ), 10, 2);

	}

	public function deattach() {
		$post_type = '';
		if( $this->options['post_type'] != 'post' && $this->options['post_type'] != 'page'   ){
			$post_type = '_' . $this->options['post_type'];
		}

		$for_type = 'posts';
		if( $this->options['post_type'] == 'page' ) {
			$for_type = 'pages';
		}
		add_filter('manage' . $post_type . '_' . $for_type . '_columns' , array( $this, 'remove_column' ) );

		add_action( 'manage' . $post_type . '_' . $for_type . '_custom_column', array( $this, 'column_data' ), 10, 2);
	}

	/**
	 * Add the column to the columns array
	 * @param array $columns
	 */
	public function add_column( $columns ) {
		$columns[ $this->options['column_key'] ] = $this->options['column_title'];
		return $columns;
	}

	/**
	 * Add the column to the columns array
	 * @param array $columns
	 */
	public function remove_column( $columns ) {

		unset( $columns[ $this->options['column_key'] ] ); 
		return $columns;
	}

	/**
	 * Render a column
	 * @param  string $column  Column slug/key
	 * @param  string $post_id 
	 * @return void      
	 */
	public function column_data( $column, $post_id ){

		if( $column == $this->options['column_key'] ){
			if( $this->options['column_display'] == 'column_key' ){
				echo get_post_meta( $post_id, $this->options['column_key'], true );
			} else {
				$function_name = $this->options['column_display'];
				call_user_func_array($function_name, array( $post_id, $this->options['column_key'] ) );
			}
		}
	}

}

$shortcode_column = new GA_Column(array(
	'post_type' => 'giveasap',
	'column_key' => '_shortcode',
	'column_title' => __( 'Shortcode', 'giveasap' ),
	'column_display' => 'giveasap_shortcode_html'
));
$shortcode_column->attach();

$subscribed_count_column = new GA_Column(array(
	'post_type' => 'giveasap',
	'column_key' => '_subscribed',
	'column_title' => __( 'Susbcribed', 'giveasap' ),
	'column_display' => 'giveasap_column_subscribed_html'
));
$subscribed_count_column->attach();


$date_column = new GA_Column(array(
	'post_type' => 'giveasap',
	'column_key' => 'date'
));

$date_column->deattach();

$dates_column = new GA_Column(array(
	'post_type' => 'giveasap',
	'column_key' => '_dates',
	'column_title' => __( 'Dates', 'giveasap' ),
	'column_display' => 'giveasap_column_dates_html'
));

$dates_column->attach();

$action_column = new GA_Column(array(
	'post_type' => 'giveasap',
	'column_key' => '_actions',
	'column_title' => __( 'Actions', 'giveasap' ),
	'column_display' => 'giveasap_column_actions_html'
));

$action_column->attach();

/**
 * Show Shortcode HTML for column
 * @return html 
 */
function giveasap_shortcode_html( $post_id, $key ) {
	echo '<p><code>[giveaway id=' . $post_id . ']</code></p>';
	echo '<p><code>[giveaway_winners id=' . $post_id . ']</code></p>';
	echo '<p><code>[giveaway_leaderboard id=' . $post_id . ']</code></p>';
	echo '<p><code>[giveaway_popup id=' . $post_id . ']</code></p>';
}

/**
 * Column for showing how much subscribers a giveaway has
 * @param  int $post_id
 * @param  string $key    
 * @return int         
 */
function giveasap_column_subscribed_html( $post_id, $key ) {
	$count_users = giveasap_get_subscribed_count( $post_id );
	echo $count_users;
 	if( $count_users > 0 ) { ?>
        <a href="<?php echo admin_url( 'admin.php?page=giveasap-users&id=' . $post_id ); ?>"  class="button button-small"><?php _e( 'List Users', 'giveasap' ); ?></a>
        <a href="<?php echo admin_url( 'admin.php?page=giveasap-subscriber-entries&giveaway=' . $post_id ); ?>"  class="button button-small"><?php _e( 'Check Entries', 'giveasap' ); ?></a>
    <?php

 	}

 	$winners = giveasap_get_winners( $post_id );

 	if ( $winners ) {
 	    $emails = wp_list_pluck( $winners, 'email' );
 	    echo '<hr/><p><strong>' . esc_html__( 'Winner(s)', 'giveasap' ) . '</strong></p>';
 	    echo implode( ', ', $emails  );
    }
}

/**
 * Showing all the dates for the Giveaway
 *
 * @since  2.6.2 
 * 
 * @param  int $post_id
 * @param  string $key    
 * @return string         
 */
function giveasap_column_dates_html( $post_id, $key ) {
	$schedule = get_post_meta( $post_id, 'giveasap_schedule', true );

	if( ! $schedule ) { return; }
	
	$ga_schedule = new GA_Schedule( $schedule );

	_e( 'Start:', 'giveasap' ); echo ' '; giveasap_the_formatted_datetime( $ga_schedule->get_start_timestamp(), false );
	?><hr/><?php
	_e( 'End:', 'giveasap' ); echo ' '; giveasap_the_formatted_datetime( $ga_schedule->get_end_timestamp(), false );
	?><hr/><?php
	_e( 'Winner:', 'giveasap' ); echo ' '; giveasap_the_formatted_datetime( $ga_schedule->get_winner_timestamp(), false );
}

/**
 * Adding buttons
 *
 * @since  2.6.2 
 * 
 * @param  int $post_id
 * @param  string $key    
 * @return string         
 */
function giveasap_column_actions_html( $post_id, $key ) {
	$status = get_post_status( $post_id );

	switch ( $status ) {
		case 'publish':
			?>
			<button type="submit" name="giveasap_end" value="<?php echo $post_id; ?>" class="button button-secondary button-small"><?php _e( 'End Giveaway', 'giveasap' ); ?></button>
            <button type="submit" name="giveasap_remind" value="<?php echo $post_id; ?>" class="button button-secondary button-small"><?php _e( 'Remind Subscribers', 'giveasap' ); ?></button>
			<?php
			break;
		case 'giveasap_ended':
			?>
			<button type="submit"  name="giveasap_select" value="<?php echo $post_id; ?>" class="button button-secondary button-small"><?php _e( 'Select Winner(s)', 'giveasap' ); ?></button>
			<?php
			break;
		case 'giveasap_winners':
			?>
            <button type="submit"  name="giveasap_select" value="<?php echo $post_id; ?>" class="button button-secondary button-small"><?php _e( 'Reselect Winner(s)', 'giveasap' ); ?></button>
            <button type="submit" name="giveasap_notify" value="<?php echo $post_id; ?>" class="button button-secondary button-small"><?php _e( 'Notify Winner(s)', 'giveasap' ); ?></button>
			<?php
			break;
		default:
			# code...
			break;
	}
	?>
    <button type="submit" name="sg_clone_giveaway" value="<?php echo $post_id; ?>" class="button button-secondary button-small"><?php _e( 'Clone Giveaway', 'giveasap' ); ?></button>
    <button type="submit" name="giveasap_export" value="<?php echo $post_id; ?>" class="button button-secondary button-small"><?php _e( 'Download CSV', 'giveasap' ); ?></button>
    <button type="submit" name="giveasap_export_winners" value="<?php echo $post_id; ?>" class="button button-secondary button-small"><?php _e( 'Download Winners CSV', 'giveasap' ); ?></button>
	<p style="font-size:smaller;" class="description"><?php _e( 'CSV will not be created if there are no subscribers', 'giveasap' );?></p>
	<?php
}
