<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'PCHFW_Front' ) ) :
	
	/**
	 * PCHFW_Front
	 */
	class PCHFW_Front {
		public $rules_data;
		
		/**
		 * Method __construct
		 *
		 * @return void
		 */
		public function __construct() {
			$this->rules_data = $this->pchfw_get_rules();
			add_filter('woocommerce_loop_add_to_cart_link', array( $this, 'pchfw_replace_loop_add_to_cart_link' ), 12, 2);
			add_filter('woocommerce_get_price_html', array( $this, 'pchfw_hide_woocommerce_price_html' ), 300, 2);
			add_action('woocommerce_after_add_to_cart_button', array( $this, 'pchfw_button_show_single' ), 30);
			add_action('init', array( $this, 'pchfw_add_product_page_hooks' ));
		}

		public function pchfw_add_product_page_hooks() {
			add_action('woocommerce_simple_add_to_cart', array( $this, 'pchfw_hide_cart_single' ), 1, 0);
			add_action('woocommerce_variable_add_to_cart', array( $this, 'pchfw_hide_cart_single' ), 1, 0);
			add_action('woocommerce_single_product_summary', array( $this, 'pchfw_hide_cart_single' ), 1, 0);
		}

		public function pchfw_hide_cart_single() {
			global $product;
		 
			foreach ( $this->rules_data as $rule ) {

				$rule_data = get_post_meta($rule->ID, 'pchfw_rule_data', true);
                $hide_cart_button   = isset( $rule_data['hide_cart_button'] ) ? $rule_data['hide_cart_button'] : '';

				if (!$this->wc_pchfw_check_rule_for_product($product->get_id(), $rule->ID) ) {
					continue;
				}
				 
				if ('1' === $hide_cart_button) {
					remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
					remove_action('woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30);	
					add_action('woocommerce_single_product_summary', array( $this, 'pchfw_button_show_single' ), 30);
				}
			}
		}
		
		public function pchfw_button_show_single() {
			global $product;
		 
			foreach ( $this->rules_data as $rule ) {

				$rule_data = get_post_meta($rule->ID, 'pchfw_rule_data', true);
                $hide_cart_button   = isset( $rule_data['hide_cart_button'] ) ? $rule_data['hide_cart_button'] : '';
                $show_custom_button   = isset( $rule_data['show_custom_button'] ) ? $rule_data['show_custom_button'] : '';
                $custom_button_label = isset( $rule_data['custom_button_label'] ) ? $rule_data['custom_button_label'] : 'Button';
                $custom_button_link  = isset( $rule_data['custom_button_link'] ) ? $rule_data['custom_button_link'] : '';

				if (!$this->wc_pchfw_check_rule_for_product($product->get_id(), $rule->ID) ) {
					continue;
				}
				 
				if ('1' === $show_custom_button) {
					echo '<a href="' . esc_url( $custom_button_link ) . '" rel="nofollow" data-product_id="' . intval( $product->get_ID() ) . '" data-product_sku="' . esc_attr( $product->get_sku() ) . '" class="button">' . esc_html( $custom_button_label ) . '</a>'; 
				}
			}
		}

		public function pchfw_hide_woocommerce_price_html( $price, $product ) {

			if ( ( is_admin() && ! wp_doing_ajax() ) || is_checkout() || !is_object($product) || is_account_page() || is_cart() ) {
				return $price;
			}
			if ('subscription_variation' === $product->get_type() || 'variation' === $product->get_type() ) {
				$product = wc_get_product($product->get_parent_id());
			}

			foreach ( $this->rules_data as $rule ) {

				$rule_data = get_post_meta($rule->ID, 'pchfw_rule_data', true);
				$hide_price = isset($rule_data['hide_price']) ? $rule_data['hide_price'] : '0';
				$hide_price_text = isset($rule_data['hide_price_text']) ? $rule_data['hide_price_text'] : '' ;

				if (!$this->wc_pchfw_check_rule_for_product($product->get_id(), $rule->ID) ) {
					continue;
				}

				return $hide_price_text	;
			}

			return $price;
		}


        public function pchfw_replace_loop_add_to_cart_link( $html, $product ) {
			if ('simple' !== $product->get_type() && 'subscription' !== $product->get_type() ) {
				return $html;
			}

			foreach ( $this->rules_data as $rule ) {

				$rule_data = get_post_meta($rule->ID, 'pchfw_rule_data', true);
                $hide_cart_button   = isset( $rule_data['hide_cart_button'] ) ? $rule_data['hide_cart_button'] : '';
                $show_custom_button   = isset( $rule_data['show_custom_button'] ) ? $rule_data['show_custom_button'] : '';
                $custom_button_label = isset( $rule_data['custom_button_label'] ) ? $rule_data['custom_button_label'] : 'Button';
                $custom_button_link  = isset( $rule_data['custom_button_link'] ) ? $rule_data['custom_button_link'] : '';

				if (!$this->wc_pchfw_check_rule_for_product($product->get_id(), $rule->ID) ) {
					continue;
				}
				
				if ( 1 == $show_custom_button && 1 != $hide_cart_button ) {
					$html .= '<div data-block-name="woocommerce/product-button" data-text-align="center" data-is-descendent-of-query-loop="true" data-font-size="small" class="wp-block-button wc-block-components-product-button has-text-align-center">';
					return $html . '<a href="' . esc_url( $custom_button_link ) . '" rel="nofollow" data-product_id="' . intval( $product->get_ID() ) . '" data-product_sku="' . esc_attr( $product->get_sku() ) . '" class="button">' . esc_html( $custom_button_label ) . '</a>';
				} elseif ( 1 == $hide_cart_button && 1 == $show_custom_button  ) {
					$data = '<div data-block-name="woocommerce/product-button" data-text-align="center" data-is-descendent-of-query-loop="true" data-font-size="small" class="wp-block-button wc-block-components-product-button has-text-align-center">';
					return $data . '<a href="' . esc_url( $custom_button_link ) . '" rel="nofollow" data-product_id="' . intval( $product->get_ID() ) . '" data-product_sku="' . esc_attr( $product->get_sku() ) . '" class="button">' . esc_html( $custom_button_label ) . '</a>';
				} elseif (  1 == $hide_cart_button ) {
					return;
				}

			}

			return $html;
		}

        public function wc_pchfw_check_rule_for_product( $product_id, $rule_id ) {
			if (is_admin() && ! wp_doing_ajax() ) {
				return false;
			}
			$product = wc_get_product($product_id);
			
			if ( !isset($product) || empty($product) ) {
				return;
			}
			
			if ('variation' === $product->get_type() || 'subscription_variation' === $product->get_type() ) {
				$product = wc_get_product($product->get_parent_id());
			}
		 
			$product_id = $product->get_id();
			$rule_data = get_post_meta(intval($rule_id), 'pchfw_rule_data', true);
			$select_type = isset( $rule_data['select_type'] ) ? $rule_data['select_type'] : 'product';
            $include_product = isset( $rule_data[ 'include_' . $select_type ]['toggle'] ) ? $rule_data[ 'include_' . $select_type ]['toggle'] : 'all';
            $include_product_ids = isset( $rule_data[ 'include_' . $select_type ][ $include_product ] ) ? $rule_data[ 'include_' . $select_type ][ $include_product ] : array();
            $excluded_product_ids = ! empty( $rule_data[ 'exclude_' . $select_type ] ) ? $rule_data[ 'exclude_' . $select_type ] : array();
			// print_r($excluded_product_ids);
            //     die;
            if ('category' == $select_type ||  'tag' == $select_type  ) {
				$args = array(
				'limit'            => -1,
				'status'           => 'publish',
				'return'           => 'ids',
				$select_type => $excluded_product_ids,
				);
				$excluded_product_ids = wc_get_products($args);

				if (in_array($product_id, $excluded_product_ids) ) {
					return false;
				}
			}

			if (in_array($product_id, $excluded_product_ids) ) {
				return false;
			}

			if ('all' == $include_product ) {
				return true;
			}

			if ('category' == $select_type  ||  'tag' == $select_type ) {
				$args = array(
				'limit'            => -1,
				'status'           => 'publish',
				'return'           => 'ids',
				$select_type => $include_product_ids,
				);
				$include_product_ids = wc_get_products($args);

				if (in_array($product_id, $include_product_ids) ) {
					return true;
				}
			}
		 
			if (in_array($product_id, $include_product_ids) ) {
				return true;
			}

			return false;
		}

		public function pchfw_get_rules() {

			$args = array(
				'post_type'        => 'pchfw',
				'post_status'      => 'publish',
				'numberposts'      => -1,
				'orderby'          => 'menu_order',
				'order'            => 'ASC',
			);

			return get_posts( $args );
		}
	}

	new PCHFW_Front();
endif;