<?php

namespace Webnestors\Wsn\Snippets;

class CartDisplayOrderTotalWeight extends \Webnestors\Wsn\Snippet {
    public $weight_prefix = "";

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

    public function title(): string {
        return esc_html__("Display Order Total Weight", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__("Show the order total weight in you cart page", "enhancements-for-woocommerce");
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Weight Label Prefix", "enhancements-for-woocommerce"),
                "description" => esc_html__(
                    "The text to be shown before the weight, for example (Your orders is: 9kg)",
                    "enhancements-for-woocommerce"
                ),
                "name" => "weight_prefix",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Display Order Total Weight module adds a valuable feature to your WooCommerce store by showing the total weight of all items in the customer's cart. This feature is particularly useful for stores that calculate shipping costs based on weight or for customers who are concerned about the total weight of their order. By displaying the order's total weight directly on the cart page, customers have better visibility into the shipping factors, especially when shipping fees or methods depend on the package's weight.</p>
        <p>With this module, customers can easily see how much their combined items weigh, helping them make more informed decisions when adding or removing products from their cart. This is especially helpful for businesses selling heavy, bulky items, or offering tiered shipping rates based on weight.</p>
        <h3>Settings</h3>
        <h4>Weight Label Prefix</h4>
        <p>Write the text that will be shown on cart as a notification before the Procced to Checkout Button</p>
        <h3>Appearance</h3>
        <p>You can customize the appearence of the module by using custom css.</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_action("woocommerce_proceed_to_checkout", [$this, "wn_display_total_weight"]);
    }

    function wn_display_total_weight() {
        $notice =
            $this->weight_prefix .
            " " .
            WC()->cart->get_cart_contents_weight() .
            get_option("woocommerce_weight_unit");

        if (is_cart()) {
            wc_print_notice($notice, "notice");
        } else {
            wc_add_notice($notice, "notice");
        }
    }
}
