<?php
/**
 * Subscriber class to handle the entry.
 */

namespace Simple_Giveaways;

/**
 * Class SG_Subscriber
 */
class SG_Subscriber {

	/**
	 * Entry ID.
	 */
	public $entry_id = 0;

	/**
	 * Giveaway ID.
	 *
	 * @var int
	 */
	public $giveaway_id = 0;

	/**
	 * Giveaway.
	 *
	 * @var SG_Giveaway
	 */
	public $giveaway = null;

	/**
	 * This is the main data (from table)
	 *
	 * @var array
	 */
	public $data = null;

	/**
	 * Meta
	 *
	 * @var array
	 */
	public $meta = [];

	/**
	 * SG_Subscriber constructor.
	 *
	 * @param integer|object $entry Entry ID.
	 */
	public function __construct( $entry = 0, $giveaway_id = 0 ) {
		if ( ! $entry ) {
			return; }

		if ( is_numeric( $giveaway_id ) ) {
			$this->giveaway_id = absint( $giveaway_id );
		}

		if ( is_numeric( $entry ) ) {
			$this->entry_id = $entry;
		} elseif ( is_object( $entry ) ) {
			$this->data        = (array) $entry;
			$this->entry_id    = $this->data['id'];
			$this->giveaway_id = $this->data['post_id'];
		}
	}

    public function exists() {
        return $this->entry_id > 0;
    }

	/**
	 * Get the page from which the subscriber has subscribed.
	 *
	 * @return bool|false|mixed|string
	 */
	public function get_subscribed_from_page() {
		$subscribed_page = giveasap_get_meta( $this->get_id(), '_from_page', true );

		if ( $subscribed_page ) {
			return $subscribed_page;
		}

		if ( $this->get_giveaway() ) {
			$giveaway = $this->get_giveaway();
			return $giveaway->get_main_page_link();
		}

		return false;
	}

	/**
	 * Return the Subscriber ID.
	 *
	 * @return int|mixed
	 */
	public function get_id() {
		return $this->entry_id;
	}

	/**
	 * Return the Subscriber Reference ID.
	 *
	 * @return int|mixed
	 */
	public function get_ref_id() {
		$data = $this->get_data();
		return isset( $data['ref_id'] ) ? $data['ref_id'] : '';
	}

	/**
	 * Return the WP User ID if signed.
	 *
	 * @return int
	 */
	public function get_wp_user_id() {
		if ( ! $this->entry_id ) {
			return 0;
		}

		$ret = giveasap_get_meta( $this->entry_id, 'user_id', true );
		return $ret ? absint( $ret ) : 0;
	}

	/**
	 * Get Data
	 */
	public function get_data() {
		if ( ! $this->entry_id ) {
			return array(); }

		if ( null === $this->data ) {
			$data = giveasap_get_user_by_id( $this->entry_id, $this->giveaway_id );
			if ( $data ) {
				$this->data = (array) $data;
			}
		}

		return $this->data;
	}

    public function is_unsubscribed_from_emails() {
        return (bool) $this->get_meta( '_is_unsubscribed_from_emails' );
    }

	/**
	 * Get the status
	 *
	 * @return mixed|string
	 */
	public function get_status() {
		$data = $this->get_data();

		if ( ! $data ) {
			return 'active'; }

		return isset( $data['status'] ) ? $data['status'] : 'active';
	}

	/**
	 * Get current entries for a subscriber.
	 */
	public function get_entries() {
		$data = $this->get_data();

		if ( ! $data ) {
			return 0; }

		return isset( $data['entries'] ) ? absint( $data['entries'] ) : 0;
	}

	/**
	 * Add entries to a Subscriber
	 *
	 * @param int $add Entries to Add.
	 */
	public function add_entries( $add = 0 ) {
		$entries = $this->get_entries();

		$entries += $add;

		giveasap_update_user_entry( $this->entry_id, $entries );
	}

	/**
	 * Remove entries from a Subscriber
	 *
	 * @param int $remove Entries to remove.
	 */
	public function remove_entries( $remove = 0 ) {
		$entries = $this->get_entries();

		$entries -= $remove;

		if ( $entries < 0 ) {
			$entries = 0;
		}

		giveasap_update_user_entry( $this->entry_id, $entries );
	}

	/**
	 * Get email.
	 *
	 * @return mixed|string
	 */
	public function get_email() {
		$data = $this->get_data();

		return isset( $data['email'] ) ? $data['email'] : '';
	}

	/**
	 * Activate the Subscriber
	 */
	public function activate() {
		if ( ! $this->get_id() ) {
			return false;
		}

		return giveasap_activate_user( $this->get_id() );
	}

	/**
	 * Block the subscriber.
	 *
	 * @return bool
	 */
	public function block() {
		if ( ! $this->get_id() ) {
			return false;
		}

		return giveasap_block_subscriber( $this->get_id() );
	}

	/**
	 * Get the meta data of the subscriber
	 *
	 * @param      $key
	 * @param bool $single
	 */
	public function get_meta( $key, $single = true ) {
		if ( ! isset( $this->meta[ $key ] ) ) {
			$this->meta[ $key ] = giveasap_get_meta( $this->get_id(), $key, $single );
		}

		return $this->meta[ $key ];
	}

	/**
	 * Get the meta data of the subscriber
	 *
	 * @param      $key
	 * @param mixed $value
	 */
	public function set_meta( $key, $value ) {
		return $this->meta[ $key ] = $value;
	}

	/**
	 * Unset the meta data of the subscriber
	 *
	 * @since 2.30.0
	 *
	 * @param      $key
	 */
	public function unset_meta( $key ) {
		unset( $this->meta[ $key ] );
	}

	/**
	 * Update the meta.
	 *
	 * @param $key
	 * @param $value
	 */
	public function update_meta( $key, $value ) {
		giveasap_update_meta( $this->get_id(), $key, $value );
		$this->set_meta( $key, $value );
	}

	/**
	 * Add the meta.
	 *
	 * @since 2.30.0
	 *
	 * @param $key
	 * @param $value
	 */
	public function add_meta( $key, $value ) {
		giveasap_add_meta( $this->get_id(), $key, $value );
		$this->set_meta( $key, $value );
	}

	/**
	 * Delete the meta
	 *
	 * @since 2.30.0
	 *
	 * @param $key
	 */
	public function delete_meta( $key ) {
		giveasap_delete_meta( $this->get_id(), $key );
		$this->unset_meta( $key );
	}

	/**
	 * Get the share link.
	 *
	 * @return string
	 */
	public function get_share_link() {
		$giveaway = $this->get_giveaway();

		if ( ! $giveaway ) {
			return '';
		}

		$sharing_link = add_query_arg( 'share', $this->get_id(), $giveaway->get_main_page_link() );

		return $sharing_link;
	}

	/**
	 * @return false|SG_Giveaway
	 */
	public function get_giveaway() {
		if ( null === $this->giveaway ) {
			$giveaway_id = $this->get_giveaway_id();
			if ( $giveaway_id ) {
				$this->giveaway = giveasap_get_giveaway( $giveaway_id );
			} else {
				$this->giveaway = false;
			}
		}

		return $this->giveaway;
	}

	/**
	 * Get Giveaway ID
	 */
	public function get_giveaway_id() {
		if ( ! $this->giveaway_id ) {
			$data              = $this->get_data();
			$this->giveaway_id = absint( $data['post_id'] );
		}

		return $this->giveaway_id;
	}

	/**
	 * Get the giveaway link.
	 *
	 * @param string $link Where to redirect the user if not empty.
	 * @return string
	 */
	public function get_giveaway_link( $link = '' ) {
		if ( ! $link ) {
			$giveaway = $this->get_giveaway();
			$link     = $giveaway->get_main_page_link();
		}

		$link = add_query_arg( 'ref', $this->get_ref_id(), $link );

		return $link;
	}
}
