<?php

namespace BwWinner;

class Brand_Shortcodes {

	public function __construct () {
		add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );

		add_shortcode( 'bw-brand-superheading', array( $this, 'brand_superheading_shortcode' ) );
		add_shortcode( 'bw-brand-heading', array( $this, 'brand_heading_shortcode' ) );
		add_shortcode( 'bw-brand-links', array( $this, 'brand_links_shortcode' ) );
		add_shortcode( 'bw-brand-description', array( $this, 'brand_description_shortcode' ) );
		add_shortcode( 'bw-brand-video', array( $this, 'brand_video_shortcode' ) );
		add_shortcode( 'bw-brand-posts', array( $this, 'brand_posts_shortcode' ) );
	}

	public function register_assets () {
		wp_register_style( 'brand_shortcode_styles', PLUGIN_URL . 'assets/css/brand-shortcode-styles.css', array(), '0.0.5' );
		wp_register_style( 'brand_posts_shortcode_styles', PLUGIN_URL . 'assets/css/brand-posts-shortcode.css', array(), '0.0.5' );
	}

	public function brand_superheading_shortcode ($atts) {

		global $bw_winners;

		$brand_id = get_the_ID();

		$logo_id = get_field( 'brand_logo' );

		$company_link = get_field( 'company_link' );

		$company_link = false;

		$brand = $bw_winners->entities->get_brand( $brand_id );
		if ( ! $brand ) return '';
		wp_enqueue_style( 'brand_shortcode_styles' );

		$company = $bw_winners->entities->get_company( $brand['company_id'] );

		$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 ( $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 brand_heading_shortcode ($atts) {

		global $bw_winners;

		$brand_id = get_the_ID();

		$brand_link = get_field( 'brand_link' );
		$brand_link = false;

		$brand = $bw_winners->entities->get_brand( $brand_id );
		if ( ! $brand ) return '';
		wp_enqueue_style( 'brand_shortcode_styles' );

		$brand_name = $brand['name'];

		ob_start();
		?>
			<div class="bw-winners-brand_heading_shortcode">
				<h1 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; ?>
				</h1>
			</div>
		<?php

		return ob_get_clean();
	}

	public function brand_links_shortcode ($atts) {

		global $bw_winners;


		$atts = shortcode_atts( array(
			'company_link_text' => 'Company'
		), $atts, 'bartag' );


		$brand_id = get_the_ID();

		$brand = $bw_winners->entities->get_brand( $brand_id );
		if ( ! $brand ) return '';

		$brand_name = $brand['name'];

		$company_link = get_field( 'company_link' );
		$brand_link = get_field( 'brand_link' );

		$has_link = $brand_link || $company_link;

		if ( ! $has_link ) return '';
		wp_enqueue_style( 'brand_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-brand_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; ?>
			</div>
		<?php

		return ob_get_clean();
	}

	public function brand_description_shortcode ($atts) {

		if ( ! is_singular( 'brand' ) ) return '';

		wp_enqueue_style( 'brand_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-brand_description_shortcode">
				<div class="bw-winners-description">
					<?php the_content(); ?>
				</div>
			</div>
		<?php

		return ob_get_clean();
	}

	public function brand_video_shortcode ($atts) {
		$video_link = get_field( 'video_link' );

		if ( empty( $video_link ) ) {
			return '';
		}
		wp_enqueue_style( 'brand_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-brand_video_shortcode">
				<?php echo wp_video_shortcode( array(
					'src' => $video_link,
					'height' => 720,
					'width' => 1280
				) ); ?>
			</div>
		<?php

		return ob_get_clean();
	}



	public function brand_posts_shortcode ($atts) {
		

		$atts = shortcode_atts( [
			'posts_per_page' => -1,
			'brand_id' => get_the_ID(),
			'heading' => 'Brand Posts'
		], $atts, 'bartag' );

		$query = [
			'post_type' => 'post',
			'posts_per_page' => $atts['posts_per_page'],
			'paged' => 1,
			'orderby' => [
				'date' => 'DESC',
				'menu_order' => 'ASC'
			],
			'brand_id' => $atts['brand_id']
		];

		$results = new \WP_Query($query);

		if ( !$results->have_posts() ) {
			return '';
		}

		wp_enqueue_style( 'brand_posts_shortcode_styles' );

		ob_start();
		?>
			<div class="bw-winners-brand_posts_shortcode">
				<h2><?php echo __( $atts['heading'], 'bw-winner' ); ?></h2>
				<div class="bw-posts">
					<?php while ( $results->have_posts() ) : $results->the_post(); ?>
						<article class="bw-post">
							<a class="bw-post-thumbnail" href="<?php echo the_permalink(); ?>">
								<div class="bw-post-thumbnail-inner">
									<?php the_post_thumbnail(); ?>
								</div>
							</a>
							<div class="bw-post-content-wrap">
								<header class="bw-post-header">
									<h3 class="bw-post-title">
										<a href="<?php echo the_permalink(); ?>">
											<?php the_title(); ?>
										</a>
									</h3>
									<div class="bw-post-meta">
										<span class="bw-posted-on">
											<time class="bw-date" datetime="<?php the_time( 'c' ); ?>">
												<?php the_time( 'j F Y' ); ?>
											</time>
										</span>
									</div>
								</header>
								<div class="bw-post-summary">
									<?php the_excerpt(); ?>
								</div>
								<footer class="bw-post-footer">
									<div class="bw-post-actions">
									<p class="bw-more-link-wrap">
										<a class="bw-more-link" href="<?php echo the_permalink(); ?>">
											Read More
										</a>
									</p>
								</div>
								</footer>
							</div>
						</article>
					<?php endwhile ?>
				</div>
			</div>
		<?php

		return ob_get_clean();
	}

	
	
}
