<?php

namespace Webnestors\Wsn\Snippets;

class CartCustomMessageOnSpecificProducts extends \Webnestors\Wsn\Snippet {
    public $products_ids = "1";
    public $products_message = "";
    public $cart_position = "woocommerce_before_cart_table";

    public function tab(): string {
        return "cart";
    }

    public function title(): string {
        return esc_html__("Message on Specific Products", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__(
            "Show a custom message when at least one of the chosen products are in the cart.",
            "enhancements-for-woocommerce"
        );
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Product Ids", "enhancements-for-woocommerce"),
                "description" => esc_html__("The products ids, seperated by commas.", "enhancements-for-woocommerce"),
                "name" => "products_ids",
                "type" => "text",
            ],
            [
                "label" => esc_html__("Your Message", "enhancements-for-woocommerce"),
                "name" => "products_message",
                "type" => "text",
            ],
            [
                "label" => esc_html__("Position", "enhancements-for-woocommerce"),
                "description" => esc_html__("The message's position on the page.", "enhancements-for-woocommerce"),
                "name" => "cart_position",
                "type" => "select",
                "options" => [
                    "woocommerce_before_cart_table",
                    "woocommerce_before_cart_contents",
                    "woocommerce_after_cart_contents",
                    "woocommerce_after_cart_table",
                    "woocommerce_cart_totals_before_shipping",
                    "woocommerce_before_shipping_calculator",
                    "woocommerce_after_shipping_calculator",
                    "woocommerce_cart_totals_after_shipping",
                    "woocommerce_cart_totals_before_order_total",
                    "woocommerce_cart_totals_after_order_total",
                    "woocommerce_proceed_to_checkout",
                    "woocommerce_after_cart_totals",
                ],
                "option_labels" => [
                    "Before cart table",
                    "Before cart contents",
                    "After cart contents",
                    "After cart table",
                    "Before totals shipping",
                    "Before shipping calculator",
                    "After shipping calculator",
                    "After totals shipping",
                    "Before order total",
                    "After order total",
                    "Before checkout button",
                    "After cart totals",
                ],
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Message on Specific Products module provides a tailored communication solution for WooCommerce stores by displaying a custom message when certain products are added to the cart. This feature allows store owners to highlight specific information, offers, or notices related to individual products or a group of products directly on the cart page. It ensures that customers are aware of any important details regarding specific items in their cart, helping to improve transparency, increase engagement, and encourage specific actions.</p>
        <p>This module is particularly useful for promoting product-specific discounts, special shipping conditions, limited-time offers, or important product-related notices. By showing a targeted message based on the products in the cart, you can personalize the shopping experience and convey product-specific information at a key stage in the purchase process.</p>
        <h3>Settings</h3>
        <h4>Product Ids</h4>
        <p>Place here the product ids that want the message to appear when they are in the cart (At least one of them).Seperate many products with a comma like 1234, 54678, 56412 etc</p>
        <h4>Your Message</h4>
        <p>The message that you want to be shown</p>
        <h4>Position</h4>
        <p>Choose where you want to place the message</p>
        <h4>Note: The position may not always work if your theme is not using the standard WooCommerce cart Positions</h4>
        <h4>Tip: You can find the product ids by going on the products table and hover over the title, under the title you will see the product id</h4>
        <h3>Appearance</h3>
        <p>You can customize this module using custom css and the class "woocommerce-info".</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_action($this->cart_position, [$this, "wn_cart_message_for_specific_product"]);
    }

    function wn_cart_message_for_specific_product() {
        $product_ids = explode(",", trim($this->products_ids));

        foreach (WC()->cart->get_cart() as $item) {
            if (array_intersect($product_ids, [$item["product_id"], $item["variation_id"]])) {
                wc_print_notice($this->products_message, "notice");
                break;
            }
        }
    }
}

