<?php

namespace BwWinner;

class Product_Shortcodes {

	public function __construct () {
		add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );

		add_shortcode( 'bw-product-superheading', array( $this, 'product_superheading_shortcode' ) );
		add_shortcode( 'bw-product-heading', array( $this, 'product_heading_shortcode' ) );
		add_shortcode( 'bw-product-links', array( $this, 'product_links_shortcode' ) );
		add_shortcode( 'bw-product-subheading', array( $this, 'product_subheading_shortcode' ) );
		add_shortcode( 'bw-product-description', array( $this, 'product_description_shortcode' ) );
		add_shortcode( 'bw-product-miscellaneous', array( $this, 'product_miscellaneous_shortcode' ) );
		add_shortcode( 'bw-product-video', array( $this, 'product_video_shortcode' ) );
		add_shortcode( 'bw-product-individual-awards', array( $this, 'product_individual_awards_shortcode' ) );
	}

	public function register_assets () {
		wp_register_style( 'product_shortcode_styles', PLUGIN_URL . 'assets/css/product-shortcode-styles.css', array(), '0.0.5' );
	}

	public function product_superheading_shortcode ($atts) {

		global $bw_winners;

		$product_id = get_the_ID();

		$logo_id = get_field( 'brand_logo' );

		$brand_link = get_field( 'brand_link' );
		$brand_link = false;
		$company_link = get_field( 'company_link' );
		$company_link = false;

		$product = $bw_winners->entities->get_product( $product_id );
		if ( ! $product ) return '';
		wp_enqueue_style( 'product_shortcode_styles' );


		$brand = $bw_winners->entities->get_brand( $product['brand_id'] );
		$company = $bw_winners->entities->get_company( $product['company_id'] );


		$brand_name = $brand['name'];
		$company_name = $company['name'];


		ob_start();
		?>
			<div class="bw-winners-superheading_shortcode">
				<?php if ( $logo_id && $logo = wp_get_attachment_image( $logo_id, 'medium' ) ) : ?>
					<div class="bw-winners-brand-logo">
						<?php echo $logo; ?>
					</div>
				<?php endif; ?>
				<h2 class="bw-winners-brand-heading">
					<?php if ( $brand_link ) : ?><a href="<?php echo $brand_link; ?>" rel="nofollow" target="_blank"><?php endif; ?>
						<?php echo $brand_name; ?>
					<?php if ( $brand_link ) : ?></a><?php endif; ?>
					<span class="bw-dash">&nbsp;&nbsp;&horbar;&nbsp;&nbsp;</span>
					<?php if ( $company_link ) : ?><a href="<?php echo $company_link; ?>" rel="nofollow" target="_blank"><?php endif; ?>
						<?php echo $company_name; ?>
					<?php if ( $company_link ) : ?></a><?php endif; ?>
				</h2>
			</div>
		<?php

		return ob_get_clean();
	}

	public function product_heading_shortcode ($atts) {

		global $bw_winners;

		$product_id = get_the_ID();

		$product_link = get_field( 'product_link' );
		$product_link = false;

		$product = $bw_winners->entities->get_product( $product_id );
		if ( ! $product ) return '';
		wp_enqueue_style( 'product_shortcode_styles' );

		$product_name = $product['name'];

		ob_start();
		?>
			<div class="bw-winners-product_heading_shortcode">
				<h1 class="bw-winners-brand-heading">
					<?php if ( $product_link ) : ?><a href="<?php echo $product_link; ?>" rel="nofollow" target="_blank"><?php endif; ?>
						<?php echo $product_name; ?>
					<?php if ( $product_link ) : ?></a><?php endif; ?>
				</h1>
			</div>
		<?php

		return ob_get_clean();
	}

	public function product_links_shortcode ($atts) {

		global $bw_winners;


		$atts = shortcode_atts( array(
			'company_link_text' => 'Company'
		), $atts, 'bartag' );


		$product_id = get_the_ID();

		$product = $bw_winners->entities->get_product( $product_id );
		if ( ! $product ) return '';

		$brand = $bw_winners->entities->get_brand( $product['brand_id'] );

		$brand_name = $brand['name'];

		$company_link = get_field( 'company_link' );
		$brand_link = get_field( 'brand_link' );
		$product_link = get_field( 'product_link' );

		$has_link = $brand_link || $company_link || $product_link;

		if ( ! $has_link ) return '';
		wp_enqueue_style( 'product_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-product_links_shortcode">
				<?php if ( $company_link ) : ?>
					<span>
						<a href="<?php echo $company_link; ?>" rel="nofollow" target="_blank">
							<?php echo $atts['company_link_text']; ?>			
						</a>
					</span>
				<?php endif; ?>
				<?php if ( $brand_link ) : ?>
					<span>
						<a href="<?php echo $brand_link; ?>" rel="nofollow" target="_blank">
							<i>
								<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="currentColor" version="1.1" id="Capa_1" width="800px" height="800px" viewBox="0 0 466.337 466.337" xml:space="preserve" style="height: 1em;width: 1em;">
									<g>
										<path d="M233.168,0C104.604,0,0,104.604,0,233.168c0,128.565,104.604,233.169,233.168,233.169   c128.565,0,233.169-104.604,233.169-233.169C466.337,104.604,361.733,0,233.168,0z M223.984,441.874   c-22.321,0-46.405-41.384-59.045-107.815h118.067C270.371,400.49,246.316,441.874,223.984,441.874z M161.114,310.144   c-2.738-19.991-4.437-41.781-4.881-65.018H291.74c-0.443,23.237-2.148,45.027-4.869,65.018H161.114z M24.521,245.126h107.704   c0.443,21.883,2.09,43.859,4.887,65.018H38.768C30.693,289.826,25.818,267.966,24.521,245.126z M223.984,24.464   c21.982,0,45.687,40.14,58.484,104.877h-116.97C178.286,64.604,201.996,24.464,223.984,24.464z M286.463,153.245   c2.978,20.785,4.811,43.596,5.277,67.966H156.222c0.467-24.37,2.295-47.169,5.272-67.966H286.463z M132.226,221.211H24.521   c1.354-23.926,6.568-46.836,15.332-67.966h97.656C134.462,175.32,132.681,198.312,132.226,221.211z M315.749,245.126h126.065   c-1.296,22.84-6.188,44.7-14.246,65.018H310.855C313.646,288.985,315.305,267.009,315.749,245.126z M315.749,221.211   c-0.468-22.898-2.254-45.891-5.29-67.966h116.023c8.77,21.13,13.978,44.04,15.332,67.966H315.749z M414.596,129.33H306.617   c-7.894-42.067-20.727-78.844-38.195-102.222C330.952,37.799,384.06,76.205,414.596,129.33z M176.073,32.036   c-15.7,23.459-27.348,58.1-34.699,97.305H51.741C78.657,82.505,123.064,47.1,176.073,32.036z M49.96,334.058h90.895   c7.311,40.403,19.133,76.205,35.219,100.26C121.944,418.904,76.672,382.378,49.96,334.058z M268.41,439.222   c17.865-23.938,30.874-61.889,38.697-105.164h109.274C386.15,388.743,332.12,428.339,268.41,439.222z"/>
									</g>
								</svg>
							</i>
							Visit the <?php echo $brand_name; ?> Website			
						</a>
					</span>
				<?php endif; ?>
				<?php if ( $product_link ) : ?>
					<span>
						<a href="<?php echo $product_link; ?>" rel="nofollow" target="_blank">
							Visit the Product Page			
						</a>
					</span>
				<?php endif; ?>
			</div>
		<?php

		return ob_get_clean();
	}

	public function product_subheading_shortcode ($atts) {
		$atts = shortcode_atts( array(
			'local' => 'en_US',
			'currency' => 'USD'
		), $atts, 'bartag' );

		$fmt = new \NumberFormatter( $atts['local'], \NumberFormatter::CURRENCY );


		$retail_price = get_field( 'retail_price' );
		$purchase_link = get_field( 'purchase_link' );

		if ( empty( $purchase_link ) && empty( $retail_price ) ) {
			return '';
		}
		wp_enqueue_style( 'product_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-product_subheading_shortcode">
				<?php if ( $retail_price ) : ?>
					<div>
						<?php echo $fmt->formatCurrency( $retail_price, $atts['currency'] ); ?>
					</div>
				<?php endif; ?>
				<?php if ( $purchase_link ) : ?>
					<div>
						<a class="button button-size-small" href="<?php echo $purchase_link; ?>" rel="nofollow" target="_blank">
							Purchase Online
						</a>
					</div>
				<?php endif; ?>
			</div>
		<?php

		return ob_get_clean();
	}

	public function product_description_shortcode ($atts) {

		if ( ! is_singular( 'bw-product' ) ) return '';

		wp_enqueue_style( 'product_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-product_description_shortcode">
				<div class="bw-winners-tasting-notes">
					<?php the_content(); ?>
				</div>
			</div>
		<?php

		return ob_get_clean();
	}

	public function product_miscellaneous_shortcode ($atts) {

		$elements = array();

		// APV
		$alcohol_by_volume = get_field( 'alcohol_by_volume' );
		if ( $alcohol_by_volume != false ) {
			$elements[] = "<div>{$alcohol_by_volume}% Alcohol By Volume</div>";
		}

		if ( empty( $elements ) ) {
			return '';
		}
		wp_enqueue_style( 'product_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-product_miscellaneous_shortcode">
				<?php echo implode( '', $elements ); ?>
			</div>
		<?php

		return ob_get_clean();
	}

	public function product_video_shortcode ($atts) {
		$video_link = get_field( 'video_link' );

		if ( empty( $video_link ) ) {
			return '';
		}
		wp_enqueue_style( 'product_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-product_video_shortcode">
				<?php echo wp_video_shortcode( array(
					'src' => $video_link,
					'height' => 720,
					'width' => 1280
				) ); ?>
			</div>
		<?php

		return ob_get_clean();
	}

	public function product_individual_awards_shortcode ($atts) {

		$individual_awards = get_field( 'individual_awards' );
		if ( ! $individual_awards ) return '';

		ob_start();
		?>
			<div class="bw-winners-product_individual_awards_shortcode">
				<?php echo $individual_awards; ?>
			</div>
		<?php

		return ob_get_clean();
	}
}
