<?php
/**
 * Actions and Filters for Registration
 */

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

add_action( 'giveasap_register_user', 'sg_register_subscribe_extra_action', 20, 4 );

/**
 * Add the extra action so we can see that as entries as well.
 *
 * @param $subscriber_id
 * @param $giveaway_id
 * @param $email
 * @param $entry
 */
function sg_register_subscribe_extra_action( $subscriber_id, $giveaway_id, $email, $entry ) {
	$subscribe_action = new SG_Action_Subscribe();
	$subscribe_action->set_instance_id( -1 );
	$subscribe_action->set_giveaway_id( $giveaway_id );
	$subscribe_action->set_entries( $entry );
	$subscribe_action->set_subscriber_id( $subscriber_id );
	$subscribe_action->create_action( 'completed', $giveaway_id, array(
		'title' => __( 'Subscribed to the Giveaway', 'giveasap' ),
	) );
}

add_action( 'giveasap_register_user', 'sg_register_user_form_fields', 20, 2 );

/**
 * Saving all extra form fields
 *
 * @param integer $entry_id Entry ID.
 * @param integer $giveaway_id Giveaway ID.
 */
function sg_register_user_form_fields( $entry_id, $giveaway_id ) {
	$form_fields = sg_get_form_fields( $giveaway_id );
	
	$posted_fields = isset( $_POST['sg_form'] ) ? $_POST['sg_form'] : array();

	if ( ! $posted_fields ) {
		return;
	}

	foreach ( $posted_fields as $field_key => $field_value ) {
		// Not a configured field? Maybe someone will save it differently.
		if ( ! isset( $form_fields[ $field_key ] ) ) {
			continue;
		}

		// Email is saved in the table.
		if ( 'user_email' === $field_key ) {
			continue;
		}

		giveasap_update_meta( $entry_id, '_' . $field_key, sanitize_text_field( $field_value ) );
	}
}

add_action( 'giveasap_activated_user', 'sg_give_referrer_entries_when_user_activated' );

/**
 * Give the entries to a referrer if the user is activated.
 *
 * @param $user_id
 */
function sg_give_referrer_entries_when_user_activated( $user_id ) {
	$entries_to_give = giveasap_get_meta( $user_id, '_entries_to_give', true );
	if ( ! $entries_to_give ) {
		return;
	}

	$referrer = giveasap_get_referrer( $user_id );
	if ( ! $referrer ) {
		return;
	}

	$subscriber = new SG_Subscriber( $referrer );
	$subscriber->add_entries( $entries_to_give );
	giveasap_delete_meta( $user_id, '_entries_to_give' );
	do_action( 'sg_referrer_awarded_points', $referrer, $entries_to_give, $user_id, $subscriber->get_giveaway_id() );
}


add_action( 'giveasap_register_user', 'sg_reset_giveaway_leaderboard_transient', 20, 2 );

/**
 * Delete the transient when a new user registers to the giveaway.
 * @param $entry_id
 * @param $giveaway_id
 */
function sg_reset_giveaway_leaderboard_transient( $entry_id, $giveaway_id ) {
	sg_delete_leaderboard_transient( $giveaway_id );
}

add_action( 'giveasap_update_user_entry', 'sg_reset_giveaway_leaderboard_transient_on_entry_update' );

/**
 * Reset the leaaderboard transient when we update the user entry.
 *
 * @param $entry_id
 */
function sg_reset_giveaway_leaderboard_transient_on_entry_update( $entry_id ) {
	$user        = giveasap_get_user_by_id( $entry_id );
	$giveaway_id = $user->post_id;
	sg_delete_leaderboard_transient( $giveaway_id );
}

add_action( 'giveasap_update_user_entry', 'sg_maybe_make_subscriber_a_winner' );

/**
 * @param $entry_id
 */
function sg_maybe_make_subscriber_a_winner( $entry_id ) {
	$user = giveasap_get_user_by_id( $entry_id );
	if ( ! $user ) {
		return;
	}

	$subscriber  = sg_get_subscriber( $user );
	$giveaway_id = $subscriber->get_giveaway_id();

	if ( ! $giveaway_id ) {
		return;
	}

	$giveaway = sg_get_giveaway( $giveaway_id );

	if ( $giveaway->supports( 'winner_threshold' ) ) {
		$settings = $giveaway->get_prize_settings();
		$winner_threshold = isset( $settings['winner_threshold'] ) ? absint( $settings['winner_threshold'] ) : 0;

		if ( $winner_threshold && absint( $subscriber->get_entries() ) === $winner_threshold ) {
			sg_subscriber_make_winner( $subscriber, $giveaway );
		}
	}
}

add_action( 'sg_referrer_awarded_points', 'sg_notify_referrer_on_new_entries', 20, 3 );

/**
 * Send Referrer on new entries.
 * @param $referrer_id
 * @param $entries
 * @param $user_id
 */
function sg_notify_referrer_on_new_entries( $referrer_id, $entries, $user_id ) {
	$settings = giveasap_get_settings();
	$notify   = isset( $settings['send_referrer_email'] ) ? (bool) $settings['send_referrer_email'] : false;

	if ( $notify ) {
		sg_send_referrer_email( $referrer_id, $entries, $user_id );
	}
}

add_action( 'sg_referrer_awarded_points', 'sg_add_referrer_as_extra_action', 20, 3 );

/**
 * Send Referrer on new entries.
 * @param $referrer_id
 * @param $entries
 * @param $user_id
 */
function sg_add_referrer_as_extra_action( $referrer_id, $entries, $user_id ) {
	$referrer = sg_get_subscriber( $referrer_id );
	$subscribe_action = new SG_Action_Referred();
	$subscribe_action->set_instance_id( -1 );
	$subscribe_action->set_giveaway_id( $referrer->get_giveaway_id() );
	$subscribe_action->set_entries( $entries );
	$subscribe_action->set_subscriber_id( $referrer_id );
	$subscribe_action->create_action( 'completed', $referrer->get_giveaway_id(), array(
		'title' => __( 'Referred a user', 'giveasap' ),
		'user'  => sg_get_subscriber( $user_id ),
	) );
}

add_filter( 'sg_giveaway_redirect_entry_link', 'sg_add_form_fields_to_link', 20, 2 );

/**
 * Add Form Fields as Query string to link
 *
 * @param string $link
 * @param GiveASAP_Front $front
 */
function sg_add_form_fields_to_link( $link, $front ) {

	$giveaway = $front->get_giveaway_id();
	if ( $giveaway ) {
		$subscriber = $front->get_subscriber();
		if ( ! $subscriber ) {
			return $link;
		}

		$form_fields = sg_get_form_fields( $giveaway );
		$meta        = giveasap_get_meta( $subscriber->get_id() );
		if ( $meta && $form_fields ) {
			foreach ( $form_fields as $field_slug => $field ) {
				if ( ! isset( $field['query_string'] ) ) {
					continue;
				}

				if ( 1 !== absint( $field['query_string'] ) ) {
					continue;
				}

				if ( 'user_name' === $field_slug ) {
					$value = isset( $meta[ '_' . $field_slug ] ) ? $meta[ '_' . $field_slug ] : '';

					if ( ! $value ) {
						$value = isset( $meta[ '_name' ] ) ? $meta[ '_name' ] : '';
					}
				} else {
					$value = isset( $meta[ '_' . $field_slug ] ) ? $meta[ '_' . $field_slug ] : '';
				}

				if ( ! $value ) {
					continue;
				}

				if ( is_array( $value ) ) {
					$field_value = implode( ',', $value );
				} else {
					$field_value = $value;
				}

				$link  = add_query_arg( $field_slug, $field_value, $link );
			}
		}
	}

	return $link;
}