<?php

namespace Simple_Giveaways;

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

/**
 * Abstract class for creating sharing methods
 */
abstract class GA_Sharer {

	/**
	 * Link used for sharing
	 *
	 * @var string
	 */
	public $sharer_link = '';

	/**
	 * Icons class
	 *
	 * @var string
	 */
	public $sharer_icon = '';

	/**
	 * FontAwesome Class
	 *
	 * @var string
	 */
	public $sharer_icon_class = 'fab';

	/**
	 * Settings.
	 *
	 * @var array
	 */
	public $settings = array();

	/**
	 * @var null|SG_Giveaway
	 */
	protected $giveaway = null;

	/**
	 * If true, the share link should be opened in a popup window.
	 *
	 * @var bool
	 */
	public $use_popup = false;

	/**
	 * Method title.
	 *
	 * @var string
	 */
	protected $method_title = '';

	public function __construct( $settings = array() ) {
		$this->settings = $settings;
	}

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

	/**
	 * Return the giveaway.
	 *
	 * @return null|SG_Giveaway
	 */
	public function get_giveaway() {
		return $this->giveaway;
	}

	/**
	 * Return the Method Title (Used on the Admin part).
	 */
	public function get_method_title() {
		return $this->method_title;
	}

	/**
	 * Get the method title from settings (Used on the front part).
	 *
	 * @return mixed|string
	 */
	public function get_title() {
		return isset( $this->settings['title'] ) ? $this->settings['title'] : '';
	}

	/**
	 * Get the method type.
	 *
	 * @return mixed|string
	 */
	public function get_type() {
		return isset( $this->settings['type'] ) ? $this->settings['type'] : '';
	}

	/**
	 * Returning the HTML for the link
	 *
	 * @param  string $url
	 * @param  string $text
	 * @param  array  $display Display options
	 * @return string
	 */
	public function get_link( $url, $text, $display ) {
		$url       = urlencode( $this->prepare_url( $url ) );
		$atts      = $this->get_extra_attributes();
		$link_atts = array();
		if ( $atts ) {
			foreach ( $atts as $attribute => $value ) {
				$link_atts[] = $attribute . '="' . $value . '"';
			}
		}
		return '<a ' . implode( ' ', $link_atts ) . ' data-popup="' . absint( $this->use_popup ) . '" target="_blank" href="' . $this->sharer_link . $this->sharing_attributes( $url, $text, $display ) . '">' . $this->get_sharer_icon() . esc_html( sanitize_text_field( $this->get_title() ) ) . $this->get_extra_content() . '</a>';
	}

	/**
	 * Get some extra content provided through a filter.
	 *
	 * return string
	 */
	protected function get_extra_content() {
		return apply_filters( 'sg_sharer_extra_content', '', $this );
	}

	/**
	 * Get extra attributes.
	 *
	 * @return array
	 */
	protected function get_extra_attributes() {
		$atts      = array();
		$bg_color  = isset( $this->settings['bg_color'] ) ? $this->settings['bg_color'] : '';
		$txt_color = isset( $this->settings['txt_color'] ) ? $this->settings['txt_color'] : '';
		$style     = '';
		if ( $bg_color ) {
			$style .= 'background-color:' . $bg_color . ';';
		}
		if ( $txt_color ) {
			$style .= 'color:' . $txt_color . ';';
		}

		if ( $style ) {
			$atts['style'] = $style;
		}

		return apply_filters( 'ss_sharing_method_extra_attributes', $atts, $this );
	}

	/**
	 * Prepare the URL by filtering it so others can change or add their attributes (not link, but URL to giveaway)
	 *
	 * @since 2.12.0
	 *
	 * @param $url
	 *
	 * @return string
	 */
	protected function prepare_url( $url ) {
		return apply_filters( 'sg_sharer_prepare_url', $url, $this );
	}

	/**
	 * Setting the sharing attributes
	 *
	 * @param  string $url
	 * @param  string $text
	 * @param  array  $display Display options
	 * @return string
	 */
	public function sharing_attributes( $url, $text, $display ) {
		return '';
	}

	/**
	 * Returning the text for the button
	 *
	 * @return string
	 */
	public function get_sharer_text() {
		return '';
	}

	/**
	 * Returning the sharer icon
	 *
	 * @return [type] [description]
	 */
	public function get_sharer_icon() {
		if ( $this->sharer_icon === false ) {
			return '';
		}
		return '<span class="' . $this->sharer_icon_class . ' fa-' . $this->sharer_icon . '"></span>';
	}
}
