<?php

//-----------------------------------
// Stores

class FC_Shortcode_Stores_Dropdown {

	static function init() {
		add_shortcode('fp-stores-dropdown', array(__CLASS__, 'handle_shortcode'));
		add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_script'), 0);
	}

	static function handle_shortcode($attrs) {
		$a = shortcode_atts( array(
			'dropdown_default_value' => 'Select',
			'show_catering_link' => 'false'), $attrs );

		$dropdownDefaultValue = $a['dropdown_default_value'];
		$showCateringLink = $a['show_catering_link'];

		ob_start();
		include (THEME_SHORTCODE_DIR . 'includes/stores_dropdown.php');
		return ob_get_clean();
	}

	static function enqueue_script() {
		enqueue_and_version_script('stores_dropdown_script', '/functions/shortcodes/includes/stores_dropdown.js');
	}

} FC_Shortcode_Stores_Dropdown::init();

// -------------------------------------------
// Generic Shortcode Class

class FC_Shortcode {
	protected $filename = null,
	  $script_js = null;

	public function __construct($filename, $shortcode, $script_js = null) {
		$this->filename = $filename;
		add_shortcode($shortcode, array($this, 'handle_shortcode'));

		if($script_js != null){
			$this->script_js = $script_js;
			add_action('wp_enqueue_scripts', array($this, 'enqueue_script'), 0);
		}
	}

	public function handle_shortcode($attrs) {
		ob_start();
		if($this->filename != null){
			include (THEME_SHORTCODE_DIR . "includes/$this->filename");
		}
		return ob_get_clean();
	}

	public function enqueue_script() {
    $fp = Freshop_Manager::get_instance();
		$scripts = $this->script_js;
		$arrlength = count($scripts);

		for($x = 0; $x < $arrlength; $x++) {
			$script = $name = $scripts[$x];
      if(false !== strpos($name, '/')) $name = $fp->trim_right(end(explode('/', $name)), '.js');
      else $script = "/functions/shortcodes/includes/$name.js";

			enqueue_and_version_script($name, $script);
		}
	}
}

new FC_Shortcode(null, 'fp-store-landing', array('store-landing'));					// Store Landing Page
new FC_Shortcode('newsletter.php', 'fc-newsletter'); // Site newsletter shortcode
new FC_Shortcode('site_name.php', 'fc-site-name', null);                            // Site Name
new FC_Shortcode('drink_calculator.php', 'fp-drink-calculator', array('drink-calculator'));           // Drink Calculator
new FC_Shortcode('site_banner.php', 'fc-site-banner', null);                   // Site Banner
new FC_Shortcode('hero_slider.php', 'fp-hero-slider', array('/resources/vendor/tiny-slider/tiny-slider.js'));                   // Hero Slider

// enqueue_and_version_script('tiny-slider', '/resources/vendor/tiny-slider/tiny-slider.js');
//-----------------------------------
// CTA

class FC_Shortcode_CTA {

    static function init() {
        add_shortcode('fc-cta', array(__CLASS__, 'handle_shortcode'));
        add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_script'), 0);
    }

    static function handle_shortcode($atts) {

        global $post;
        $args = array( 'posts_per_page' => 1, 'post_type' => "cta-ad", 'orderby' => 'date' );
        $posts_query = new WP_Query( $args );


        // Let wordpress know we are using this query now
        global $wp_query;
        // Put default query object in a temp variable
        $tmp_query = $wp_query;
        // Now wipe it out completely
        $wp_query = null;
        // Re-populate the global with our custom query
        $wp_query = $posts_query;

        ob_start();

        include (THEME_SHORTCODE_DIR . 'includes/cta.php');

        // Restore original query object
        $wp_query = $tmp_query;
        // Be kind; rewind
        wp_reset_postdata();

        return ob_get_clean();
    }

    static function enqueue_script() {

    }

} FC_Shortcode_CTA::init();

//-----------------------------------
// Buckets
class FC_Shortcode_Buckets {

  static function init() {
    add_shortcode('fc-buckets', array(__CLASS__, 'handle_shortcode'));
  }

  static function handle_shortcode($atts) {

    global $post;
    $args = array( 'posts_per_page' => 1, 'post_type' => "buckets", 'orderby' => 'date' );
    $posts_query = new WP_Query( $args );

    // Let wordpress know we are using this query now
    global $wp_query;
    // Put default query object in a temp variable
    $tmp_query = $wp_query;
    // Now wipe it out completely
    $wp_query = null;
    // Re-populate the global with our custom query
    $wp_query = $posts_query;

    ob_start();

    include (THEME_SHORTCODE_DIR . 'includes/buckets.php');

    // Restore original query object
    $wp_query = $tmp_query;
    // Be kind; rewind
    wp_reset_postdata();

    return ob_get_clean();
  }

} FC_Shortcode_Buckets::init();
