<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://flothemes.com/
 * @since      1.0.0
 *
 * @package    Flo_Flex_Builder
 * @subpackage Flo_Flex_Builder/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Flo_Flex_Builder
 * @subpackage Flo_Flex_Builder/public
 * @author     Flothemes <alexb@flothemes.com>
 */
class Flo_Flex_Builder_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;
	
	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of the plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	 
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;
		
		add_filter( 'flo_custom_blocks', function($block) {
			$block = array_merge($block, $this->get_custom_blocks());
			return $block;
		});

		
		add_filter( 'flo_custom_blocks_plugins', function($plugins_folders) {
			$plugins_folders[] = $this->plugin_name;
			return $plugins_folders;
		});

		// add_filter( 'flo_custom_blocks_plugins', function($plugins_folders) {
		// 	$plugins_folders[] = 'flo-pack-1';  // this is temporary only for this plugin, For new plugins use the commented code above
		// 	return $plugins_folders;
		// });

	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Flo_Flex_Builder_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Flo_Flex_Builder_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		$script_version = $this->version;
		
		if ( defined('FLO_ENVIROMENT') && FLO_ENVIROMENT == 'DEV') {
			$script_version = mt_rand(0, 99999);
		}

		// Start: Core Icons
		
			// wp_register_style( $this->plugin_name."-fontello-embedded-icons", plugin_dir_url( __FILE__ ) . "fonts/fontello/css/flo-flex-builder-icons-embedded.css", false, $script_version );
      // wp_enqueue_style($this->plugin_name."-fontello-embedded-icons");

    // End: Core Icons

		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/flo-flex-builder-public.min.css', array(), $script_version, 'all' );

	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Flo_Flex_Builder_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Flo_Flex_Builder_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		$script_version = $this->version;
		
		if ( defined('FLO_ENVIROMENT') && FLO_ENVIROMENT == 'DEV') {
			$script_version = mt_rand(0, 99999);
		}
		
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/flo-flex-builder-public.min.js', array( 'jquery' ), $script_version, false );

	}
	

	/**
	 *
	 * scan the blocks directory and add all the block names to an array
	 *
	 */	
	public function get_custom_blocks() {
		//flo_custom_blocks
		$blocks_dir = plugin_dir_path( __FILE__ ).'views/blocks';
		$blocks = scandir($blocks_dir);
		

		$available_blocks = array();

		foreach ($blocks as $key => $file) {

			if($file != '.' && $file != '..' && $file != '.DS_Store') {
				//$available_blocks[get_class($this)] = str_replace('.blade.php', '', $file);
				$available_blocks[str_replace('.blade.php', '', $file)] = get_class($this);
			}
		}
		
		// $available_blocks data example: 
		// array(1) { ["dummy"]=> string(17) "Flo_Flex_Builder_Public" }
		return $available_blocks;
	}

}

// support for https://flothemes.com template
if(!function_exists('flo_data')) {
	function flo_data($data, $key, $default = false) {
    if (isset($data[$key])) {
      return $data[$key];
    } else {
      return $default;
    }
  }
}

require_once( __DIR__ . '/get-elements.php' );
