<?php
/**
 * The public-facing functionality of the plugin.
 *
 * @link       tarokkina_free/fer
 * @since      1.0.0
 *
 * @package    Tarokkina_free
 * @subpackage Tarokkina_free/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    Tarokkina_free
 * @subpackage Tarokkina_free/public
 * @author     tarokkina_free <tarokkina_free@gmailcom>
 */
class Tarokkina_free_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 freeperties.
	 *
	 * @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;

	}


	// Css only in shortcode page
	function TkNa_free_css_shortcode() {
		global $post;
		if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'tarot' ) ) {
			wp_enqueue_style( 'tarokina-free', plugin_dir_url(dirname(__FILE__)). 'css/3cards.css', array(), TAROKKINA_FREE_VERSION, 'all' );
		}
	}

	
   // Vista del shortcode
	public function TkNa_free_shortcode(){
		
		add_shortcode( 'tarot', 'TkNa_add_shortcode_free');

		function TkNa_add_shortcode_free($atts){

			extract(shortcode_atts( array(
				'name' => ''
			), $atts));

			$tarotsNames = get_option('tarokina_tarokina_free');
			$tarotOptions = ($tarotsNames) ? $tarotsNames['tarokina_tarokina_free'][0] : array() ;

			if ($tarotOptions !== array() && $name == 'tarot_free') {

				$deck_free = $tarotOptions['tkta_free_barajas'] ?? '';

				// Creando los arrays de las cartas
				$checkCartas = get_option('_tarokki_checkCartas_free');
				$http_ssl =  get_option('_tkna_free_http_ssl') ?? false;

				if ( $http_ssl == 'yes') {
					$Ids_cards = (get_option($deck_free .'_tkna_free_deck_ssl')) ? get_option($deck_free .'_tkna_free_deck_ssl') : get_option( $deck_free .'_tkna_free_deck') ;
				}else{
					$Ids_cards = (get_option( $deck_free .'_tkna_free_deck')) ? get_option( $deck_free .'_tkna_free_deck') : false ;
				}

				
				if (!$checkCartas ) {
					update_option('_tarokki_checkCartas_free','yes' );
				}


				$start = $tarotOptions['tkta_free_start'] ?? 'selector';

				if ($start == 'selector') {
					$start_dis_1 = 'display:none;';
					$start_dis_2 = 'display:block;';
				}else{
					$start_dis_1 = 'display:flex;';
					$start_dis_2 = 'display:none;';
				}

				$type = $tarotOptions['tkta_free_spread'];
				$type = ($type) ? $type : '' ;
				$type_N = (int) 3;

				$sp_title_color = $tarotOptions['tkta_free_title_spread_color'] ?? '';
				$btn_TextColor = $tarotOptions['tkta_free_texto_spread_color'] ?? '';
				$sp_text_button = $tarotOptions['tkta_free_text_button'] ?? '';

	
				$deck_free = $tarotOptions['tkta_free_barajas'] ?? '';
				$backface = '';
				$mode = $tarotOptions['tkta_free_mode'];
				$mode = ($mode) ? $mode : '' ;	

				$the_1card_repres = $tarotOptions['tkta_free_1st'] ?? '';
				$the_2card_repres = $tarotOptions['tkta_free_2st'] ?? '';
				$the_3card_repres = $tarotOptions['tkta_free_3st'] ?? '';


				ob_start();
				include plugin_dir_path(dirname(__DIR__)). 'tarokina-free/public/template/template.php';
	
				if (! is_admin()) {
					wp_enqueue_script( 'anime', plugin_dir_url( __FILE__ ) . 'js/anime.min.js', array(), TAROKKINA_FREE_VERSION, true );
					wp_register_script( 'tarokinafree', plugin_dir_url( __FILE__ ) . 'js/tarot.js', array('anime','jquery'), TAROKKINA_FREE_VERSION, true );
					wp_enqueue_script( 'tarokinafree' );
			    }

			    wp_localize_script( 'tarokinafree', 'ajax_tarot', array(
					'url'    => admin_url( 'admin-ajax.php' ),
					'nonce'  => wp_create_nonce( 'tarot-nonce' ),
					'action' => 'e-tarotfree'
				) );

				$output = ob_get_contents();
				ob_end_clean();
				return $output;
			}

		}
	}


////////////////////   Ajax Tarot 2 ////////////////////


function TkNa_free_tarotAjax2() {

	  global $wpdb;
	   $IdsPost_free = explode(",", sanitize_text_field($_POST['cards']));
	   $IdsPost_free_N = count($IdsPost_free);
	   $http_ssl =  get_option('_tkna_free_http_ssl') ?? false;
	   $tarotsArr =  get_option('tarokina_tarokina_free');
	   $tarotsArr = ($tarotsArr) ? $tarotsArr['tarokina_tarokina_free'][0] : array() ;
	   $id_unique = trim($tarotsArr['tkta_free_id']) ?? '';
	   $sp_title_color = $tarotsArr['tkta_free_title_spread_color'] ?? '';
	   $btn_TextColor = $tarotsArr['tkta_free_texto_spread_color'] ?? '';
	   $text_button_volver = $tarotsArr['tkta_free_text_button_volver'] ?? '';
	   $tabsResult = $tarotsArr['tkta_result_tabs'] ?? 'one';
  
	  $num= 1;
	  $i = 0;
	  $rotar = '';?>
	   <div class="row"><div class="col"><div class="tabs_result">
	  <?php foreach ($IdsPost_free as $id) :

		$cardID = get_post_thumbnail_id( $id );
		$cardImg = wp_get_attachment_image_src( $cardID, 'tarokkina_free-mini');


		if ($cardImg == false ) {
			$image = ($http_ssl == 'yes') ? str_replace('http://','https://',plugin_dir_url(dirname( __FILE__ ) ).'img/no-card.png') : plugin_dir_url(dirname( __FILE__ ) ).'img/no-card.png' ;
		  }else{
			$image = ($http_ssl == 'yes') ? str_replace('http://','https://',$cardImg[0]) : $cardImg[0];
		  }


		switch ($tabsResult) {
			case 'one':
				$checked =  ($num == 1) ? 'checked' : '' ;
				break;
			case 'none':
				$checked =  '' ;
				break;
			case 'all':
				$checked =  'checked' ;
				break;
		}


		$tarotsTexts = (get_post_meta( $id, '_tkna_free_text_'.$id_unique.'basic0') == []) ? '' : get_post_meta( $id, '_tkna_free_text_'.$id_unique.'basic0')[0];
	
		$ult = '';
		$pen = '';
		if ($num == $IdsPost_free_N) {
			$ult = 'ult';
		} elseif ($num == $IdsPost_free_N -1) {
			$pen = 'pen';
		}
	  
	 ?>
  
		<div class="tab_result <?php echo esc_attr($ult.$pen)?>">
			<input type="checkbox" id="rd<?php echo esc_attr($num) ?>" name="rd" <?php echo $checked;?>>
			<a style="color:<?php echo esc_attr(($sp_title_color !== '') ?  $sp_title_color : '')?>" href="#"><label class="tab-label_result" for="rd<?php echo esc_attr($num) ?>">
			<?php echo esc_html($num) ?>.&nbsp;<?php echo esc_html(get_the_title( $id)) ?>
			</label></a>
			<div class="tab-content_result">
				<img id="i-<?php echo esc_attr($num) ?>" class="card" src="<?php echo esc_url($image);?>" alt="card-<?php echo esc_attr($num) ?>">	
				<?php if (empty($tarotsTexts)) {
					}else{
					echo '<div style="margin-top:10px">' .apply_filters('the_content', $tarotsTexts).'</div>';
				} ?>
			</div>
		</div>
  
	 <?php
  
	  $num ++;
	  $i ++;

	  endforeach; ?>
	
		  <form id="bloq-re-throw" method="post" action="<?php echo '#tknaF_preloader'?>">
		  <button style="<?php echo ($sp_title_color !== '') ?  'background:'.esc_attr($sp_title_color).';' : ''; echo ($btn_TextColor !== '') ?  'color:'.esc_attr($btn_TextColor).'!important;' : '';?>" class="re-throw" type="submit"><?php echo esc_html($text_button_volver)?></button>
		  </form>

	<?php wp_die(); 
}
/////////////////////////////////////////

     // Remove Shortcodes from vista Category
	 function TkNa_free_remove_shortcode() {
		if ( !is_admin() && is_home() || !is_admin() && is_category() ) {
			add_shortcode( 'tarot', '__return_false' );
		}    		
	}
}
