<?php

namespace Webnestors\Wsn\Snippets;

class SingleSaveTextSales extends \Webnestors\Wsn\Snippet {
    public $save_text = "Saved";

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

    public function title(): string {
        return esc_html__("Display (You Save) Sales Price", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__(
            "Display the amount of savings when there is a sales price",
            "enhancements-for-woocommerce"
        );
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Label", "enhancements-for-woocommerce"),
                "name" => "save_text",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        
        <h3>Description</h3>
        <p>The Display (You Save) Sales Price module shows customers the exact amount they save when purchasing a product at a discounted price. When a product is on sale, this feature calculates and displays the difference between the regular price and the sale price with a “You Save” label, such as “You Save $10” or “Save 20%.” This helps customers see the value of the discount directly, encouraging them to complete their purchase by emphasizing the savings.</p>
        <p>This module can be especially impactful for stores running promotions or seasonal sales, as it adds transparency and enhances the perception of value, making discounts feel more significant.</p>
        <h3>Settings</h3>
        <p>Write the text you want to display before the savings amount.</p>
        <h3>Appearance</h3>
        <p>You can style this with the css class <strong>you_save_price</strong>.</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_action("woocommerce_single_product_summary", [$this, "wn_you_save_text_for_product"], 12);
        add_filter("woocommerce_available_variation", [$this, "wn_variable_product_saving_amount"], 10, 3);
    }

    function wn_you_save_text_for_product() {
        global $product;

        $regular_price = $product->get_regular_price();
        $sale_price = $product->get_sale_price();

        if (!empty($sale_price)) {
            $saved_amount = $regular_price - $sale_price;

            echo '<p class="you_save_price">' .
                esc_html($this->save_text) .
                " " .
                esc_html(get_woocommerce_currency_symbol()) .
                "" .
                esc_html(number_format($saved_amount, 2, ".", "")) .
                "</p>";
        }
    }

    function wn_variable_product_saving_amount($data, $product, $variation) {
        if ($variation->is_on_sale()) {
            $saved_amount = $data["display_regular_price"] - $data["display_price"];
            $percentage = round(($saved_amount / $data["display_regular_price"]) * 100);

            $data["price_html"] .=
                '<p id="you_save_price">' .
                esc_html($this->save_text) .
                " : " .
                esc_html(wc_price($saved_amount)) .
                " (" .
                esc_html($percentage) .
                "%)</p>";
        }
        return $data;
    }
}
