<?php

/**
 * 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.
 *
 * @link       https://themeatelier.net
 * @since      1.0.0
 *
 * @package     chat-help
 * @subpackage  chat-help/src/WooCommerce
 * @author      ThemeAtelier<themeatelierbd@gmail.com>
 */

namespace ThemeAtelier\ChatHelp\Frontend;

use ThemeAtelier\ChatHelp\Frontend\Templates\WooButton;

// don't call the file directly.
if (! defined('ABSPATH')) {
    exit;
}

/**
 * The WooCommerce class to manage all public facing stuffs.
 *
 * @since 1.0.0
 */
class WooCommerce
{
    /**
     * 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()
    {
        $ch_wooCommerce = get_option('ch_wooCommerce');
        $shop_page_hide_add_to_cart_button = isset($ch_wooCommerce['shop_page_hide_add_to_cart_button']) ? $ch_wooCommerce['shop_page_hide_add_to_cart_button'] : '';
        $cart_page_button = isset($ch_wooCommerce['cart_page_button']) ? $ch_wooCommerce['cart_page_button'] : '';
        $shop_page_button = isset($ch_wooCommerce['shop_page_button']) ? $ch_wooCommerce['shop_page_button'] : '';
        $cart_page_hide_add_to_cart_button = isset($ch_wooCommerce['cart_page_hide_add_to_cart_button']) ? $ch_wooCommerce['cart_page_hide_add_to_cart_button'] : '';
        $this->shop_page_button();
        $this->product_page_button();
        $this->cart_page_button();
        $this->checkout_page_button();
        $this->thank_you_page_button();

        if ($shop_page_hide_add_to_cart_button && $shop_page_button) {
            add_action('wp_head', function () {
                if (is_shop() || is_product_category() || is_product_tag()) {
                    echo '<style>
                .add_to_cart_button,
                .product_type_simple,
                .product_type_variable,
                .product_type_external {
                    display:none !important;
                }
            </style>';
                }
            });
        }

        if ($cart_page_hide_add_to_cart_button && $cart_page_button) {
            add_action('wp_head', function () {
                if (is_cart()) {
                    echo '<style>
                .wc-proceed-to-checkout .checkout-button.button {
                    display:none !important;
                }
            </style>';
                }
            });
            add_action('woocommerce_proceed_to_checkout', function () {
                if (is_cart()) {
                    remove_action('woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20);
                }
            }, 1);
        }
    }

    public function shop_page_button()
    {
        $shopPageButton = new WooButton();
        $ch_wooCommerce = get_option('ch_wooCommerce');

        $shop_page_button = isset($ch_wooCommerce['shop_page_button']) ? $ch_wooCommerce['shop_page_button'] : '';
        $button_position = isset($ch_wooCommerce['shop_page_button_position']) ? $ch_wooCommerce['shop_page_button_position'] : 'after';
        $position = 12;
        if ("woocommerce_after_shop_loop_item" === $button_position) {
            $position = 6;
        } elseif ("woocommerce_before_shop_loop_item" === $button_position) {
            $position = 12;
        }

        $type_of_whatsapp_woo = isset($ch_wooCommerce['shop_page_button_type_of_whatsapp']) ? $ch_wooCommerce['shop_page_button_type_of_whatsapp'] : '';
        $shop_page_number = isset($ch_wooCommerce['shop_page_button_number']) ? $ch_wooCommerce['shop_page_button_number'] : '';
        $shop_page_group = isset($ch_wooCommerce['shop_page_button_group']) ? $ch_wooCommerce['shop_page_button_group'] : '';

        if ($shop_page_button) {
            if ('number' === $type_of_whatsapp_woo && !empty($shop_page_number) || ('group' === $type_of_whatsapp_woo && !empty($shop_page_group))) {
                add_action("woocommerce_after_shop_loop_item", array($shopPageButton, 'shop_page_button'), $position);
            }
        }
    }
    public function product_page_button()
    {
        $wooButton = new WooButton();
        $ch_wooCommerce = get_option('ch_wooCommerce');
        $product_page_button = isset($ch_wooCommerce['product_page_button']) ? $ch_wooCommerce['product_page_button'] : '';
        $button_position = isset($ch_wooCommerce['product_page_button_position']) ? $ch_wooCommerce['product_page_button_position'] : 'after';

        $type_of_whatsapp_woo = isset($ch_wooCommerce['product_page_button_type_of_whatsapp']) ? $ch_wooCommerce['product_page_button_type_of_whatsapp'] : '';
        $product_page_number = isset($ch_wooCommerce['product_page_button_number']) ? $ch_wooCommerce['product_page_button_number'] : '';
        $product_page_group = isset($ch_wooCommerce['product_page_button_group']) ? $ch_wooCommerce['product_page_button_group'] : '';

        if ($product_page_button) {
            if ('number' === $type_of_whatsapp_woo && !empty($product_page_number) || ('group' === $type_of_whatsapp_woo && !empty($product_page_group))) {
                add_action($button_position, array($wooButton, 'woo_button'));
            }
        }

        if ($product_page_button) {
            if ('woocommerce_short_description_after' == $button_position) {
                if ('number' === $type_of_whatsapp_woo && !empty($product_page_number) || ('group' === $type_of_whatsapp_woo && !empty($product_page_group))) {
                    add_filter('woocommerce_short_description', function ($description) use ($wooButton) {
                        ob_start();
                        $wooButton->woo_button(); // echoes
                        $button_html = ob_get_clean();
                        return $description . $button_html;
                    }, 20);
                }
            }
        }
    }
    public function cart_page_button()
    {
        $wooButton = new WooButton();
        $ch_wooCommerce = get_option('ch_wooCommerce');

        $cart_page_button = isset($ch_wooCommerce['cart_page_button']) ? $ch_wooCommerce['cart_page_button'] : '';
        $button_position = isset($ch_wooCommerce['cart_page_button_position']) ? $ch_wooCommerce['cart_page_button_position'] : 'after';

        $type_of_whatsapp_woo = isset($ch_wooCommerce['cart_page_button_type_of_whatsapp']) ? $ch_wooCommerce['cart_page_button_type_of_whatsapp'] : '';
        $cart_page_number = isset($ch_wooCommerce['cart_page_button_number']) ? $ch_wooCommerce['cart_page_button_number'] : '';
        $cart_page_group = isset($ch_wooCommerce['cart_page_button_group']) ? $ch_wooCommerce['cart_page_button_group'] : '';

        $position = 25;
        if ("after" === $button_position) {
            $position = 25;
        } elseif ("before" === $button_position) {
            $position = 0;
        }

        if ($cart_page_button) {
            if ('number' === $type_of_whatsapp_woo && !empty($cart_page_number) || ('group' === $type_of_whatsapp_woo && !empty($cart_page_group))) {
                add_action("woocommerce_proceed_to_checkout", array($wooButton, 'cart_page_button'), $position);
            }
        }
    }
    public function checkout_page_button()
    {
        $wooButton = new WooButton();
        $ch_wooCommerce = get_option('ch_wooCommerce');

        $checkout_page_button = isset($ch_wooCommerce['checkout_page_button']) ? $ch_wooCommerce['checkout_page_button'] : '';
        $button_position = isset($ch_wooCommerce['checkout_page_button_position']) ? $ch_wooCommerce['checkout_page_button_position'] : 'after';

        $type_of_whatsapp_woo = isset($ch_wooCommerce['checkout_page_button_type_of_whatsapp']) ? $ch_wooCommerce['checkout_page_button_type_of_whatsapp'] : '';
        $checkout_page_number = isset($ch_wooCommerce['checkout_page_button_number']) ? $ch_wooCommerce['checkout_page_button_number'] : '';
        $checkout_page_group = isset($ch_wooCommerce['checkout_page_button_group']) ? $ch_wooCommerce['checkout_page_button_group'] : '';

        if ($checkout_page_button) {
            if ('number' === $type_of_whatsapp_woo && !empty($checkout_page_number) || ('group' === $type_of_whatsapp_woo && !empty($checkout_page_group))) {
                add_action($button_position, array($wooButton, 'checkout_page_button'));
            }
        }
    }
    public function thank_you_page_button()
    {
        $wooButton = new WooButton();
        $ch_wooCommerce = get_option('ch_wooCommerce');

        $thank_you_page_button = isset($ch_wooCommerce['thank_you_page_button']) ? $ch_wooCommerce['thank_you_page_button'] : '';

        $type_of_whatsapp_woo = isset($ch_wooCommerce['thank_you_page_button_type_of_whatsapp']) ? $ch_wooCommerce['thank_you_page_button_type_of_whatsapp'] : '';
        $thank_you_page_number = isset($ch_wooCommerce['thank_you_page_button_number']) ? $ch_wooCommerce['thank_you_page_button_number'] : '';
        $thank_you_page_group = isset($ch_wooCommerce['thank_you_page_button_group']) ? $ch_wooCommerce['thank_you_page_button_group'] : '';

        if ($thank_you_page_button) {
            if ('number' === $type_of_whatsapp_woo && !empty($thank_you_page_number) || ('group' === $type_of_whatsapp_woo && !empty($thank_you_page_group))) {
                add_action('woocommerce_thankyou_order_received_text', array($wooButton, 'thank_you_page_button'), 10, 2);
            }
        }
    }
}
