<?php
/**
 * Sharing class.
 */
namespace Simple_Giveaways;

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

/**
 * Class for creating and rendering all the sharing methods
 */
class GA_Sharing {

	public $sharing_methods = array();

	public $text = '';

	public $url = '';

	public $display_settings = '';

	/**
	 * Giveaway for whom we will show the sharing.
	 *
	 * @since 2.12.0
	 *
	 * @var null|SG_Giveaway
	 */
	private $giveaway = null;

	/**
	 * GA_Sharing constructor.
	 *
	 * @param $url
	 * @param $text
	 * @param $display
	 */
	public function __construct( $url, $text, $display ) {
		$this->text             = $text;
		$this->url              = $url;
		$this->display_settings = $display;
	}

	/**
	 * Set the Sharing URL
	 *
	 * @param $url
	 */
	public function set_sharing_url( $url ) {
		$this->url = $url;
	}

	/**
	 * @return string
	 */
	public function get_encoded_text() {
		return htmlspecialchars( urlencode( html_entity_decode( $this->text, ENT_COMPAT, 'UTF-8' ) ), ENT_COMPAT, 'UTF-8' );
	}

	/**
	 * @param SG_Giveaway $giveaway
	 */
	public function set_giveaway( SG_Giveaway $giveaway ) {
		$this->giveaway = $giveaway;
	}

	/**
	 * Render the Methods.
	 */
	public function render() {
		$this->sharing_methods = giveasap_get_sharing_methods();

		if ( is_array( $this->sharing_methods ) && ! empty( $this->sharing_methods ) ) {
			echo '<h3 class="giveasap-entries-title">' . esc_html__( 'Share & collect entries', 'giveasap' ) . '</h3>';
			echo '<p class="giveasap-sharing-description">' . esc_html__( 'For each referred subscriber, you will collect entries.', 'giveasap' ) . '</p>';
			echo '<ul class="giveasap_sharing row">';
			foreach ( $this->sharing_methods as $sharing_id => $sharing_object ) {
				if ( null !== $this->giveaway ) {
					$sharing_object->set_giveaway( $this->giveaway );
				}
				echo '<li class="sharing_method ' . esc_attr( $sharing_object->get_type() ) . '">' . $sharing_object->get_link( $this->url, $this->get_encoded_text(), $this->display_settings ) . '</li>';
			}
			echo '</ul>';
		}
	}

}
