<?php
/**
 * Functions related to subscribers.
 */

if ( ! defined( 'ABSPATH') ) {
	return;
}

/**
 * Activate the user.
 *
 * @since 2.11.0
 *
 * @param $user_id
 */
function giveasap_activate_user( $user_id ) {
	global $wpdb;

	$ret = $wpdb->update(
		$wpdb->giveasap_entries,
		array(
			'status' => 'active'
		),
		array( 'id' => $user_id ),
		array(
			'%s',	// value1
		),
		array( '%d' )
	);

	/**
	 * @since 2.21.0
	 */
	if ( false !== $ret ) {
		do_action( 'giveasap_activated_user', $user_id );
	}

	// If returned 0, the user is already active.
	return $ret === false ? false : true;
}

/**
 * Block the user.
 *
 * @since 2.19.0
 *
 * @param $user_id
 */
function sg_block_subscriber( $user_id ) {
	global $wpdb;

	$ret = $wpdb->update(
		$wpdb->giveasap_entries,
		array(
			'status' => 'blocked'
		),
		array( 'id' => $user_id ),
		array(
			'%s',	// value1
		),
		array( '%d' )
	);

	// If returned 0, the user is already blocked.
	$blocked = $ret === false ? false : true;

	// We don't want to trigger this if the subscriber was already blocked.
	if ( $blocked && $ret > 0 ) {
		do_action( 'sg_block_subscriber', $user_id );
	}

	return $blocked;
}

/**
 * Unblock the user.
 *
 * @since 2.24.0
 *
 * @param $user_id
 */
function sg_unblock_subscriber( $user_id ) {
	global $wpdb;

	$ret = $wpdb->update(
		$wpdb->giveasap_entries,
		array(
			'status' => 'pending'
		),
		array( 'id' => $user_id ),
		array(
			'%s',	// value1
		),
		array( '%d' )
	);

	// If returned 0, the user is already blocked.
	$blocked = $ret === false ? false : true;

	// We don't want to trigger this if the subscriber was already blocked.
	if ( $blocked && $ret > 0 ) {
		do_action( 'sg_unblock_subscriber', $user_id );
	}

	return $blocked;
}

/**
 * Get Subscriber Statuses.
 *
 * @return array
 */
function sg_subscriber_statuses() {
	return apply_filters( 'sg_subscriber_statuses', array(
		'blocked' => __( 'Blocked', 'giveasap' ),
		'pending' => __( 'Pending', 'giveasap' ),
		'active'  => __( 'Active', 'giveasap' ),
	) );
}

/**
 * @param integer|SG_Subscriber       $subscriber
 * @param boolean|integer|SG_Giveaway $giveaway
 */
function sg_subscriber_make_winner( $subscriber, $giveaway = false ) {

	if ( is_numeric( $subscriber ) ) {
		$subscriber = sg_get_subscriber( $subscriber );
	}

	if ( ! $giveaway ) {
		$giveaway = sg_get_giveaway( $subscriber->get_giveaway_id() );
	} elseif ( is_numeric( $giveaway ) ) {
		$giveaway = sg_get_giveaway( $giveaway );
	}

	$winners = giveasap_get_winners( $giveaway->get_id() );

	if ( ! $winners ) {
		$winners = array();
	}

	$already_winner = false;
	foreach ( $winners as $winner ) {
		if ( $winner['email'] === $subscriber->get_email() ) {
			$already_winner = true;
			break;
		}
	}

	if ( $already_winner ) {
		return true;
	}

	$winners[] = array( 'id' => $subscriber->get_id(), 'email' => $subscriber->get_email(), 'emailed' => 'no' );
	giveasap_update_meta( $subscriber->get_id(), 'winner', '1' );

	$winners = apply_filters( 'sg_selected_winners', $winners, $giveaway->get_id(), $giveaway );
	giveasap_update_winners( $winners, $giveaway->get_id() );
	do_action( 'giveasap_winner_made', $subscriber->get_id(), $subscriber, $giveaway );
	giveasap_notify_winners( $winners, $giveaway->get_id(), $subscriber->get_email() );
}

/**
 * Edit the user.
 *
 * @since 2.24.0
 *
 * @param $user_id
 */
function sg_edit_subscriber( $id, $data ) {
	global $wpdb;

	if ( ! $data ) {
		return false;
	}

	$format = array();

	foreach ( $data as $column => $value ) {
		$format[] = '%s';
	}

	$ret = $wpdb->update(
		$wpdb->giveasap_entries,
		$data,
		array( 'id' => $id ),
		$format,
		array( '%d' )
	);

	// If returned 0, the user is already blocked.
	$updated = $ret === false ? false : true;

	return $updated;
}