<?php

namespace Webnestors\Wsn\Snippets;

class CheckoutRenamePlaceOrderBacsCod extends \Webnestors\Wsn\Snippet {
    public $bacs_text = "View Bank Details";
    public $cod_text = "Confirm Cash on Delivery";

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

    public function title(): string {
        return esc_html__("Rename Place Order on Bacs or COD", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__("Rename the (Place order) button according to the chosen payment method.", "enhancements-for-woocommerce");
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Name for Bank Transfer", "enhancements-for-woocommerce"),
                "name" => "bacs_text",
                "type" => "text",
            ],
            [
                "label" => esc_html__("Name for Cash on Delivery", "enhancements-for-woocommerce"),
                "name" => "cod_text",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Rename Place Order on BACS or COD module enables you to dynamically customize the "Place Order" button text based on the selected payment method at checkout. This allows you to tailor the button label to reflect the specific action required for different payment options, such as "Confirm Order" for Bank Transfer (BACS) or "Place Cash on Delivery Order" for Cash on Delivery (COD).</p>
        <p>This feature helps improve clarity and enhance the user experience by providing context-specific instructions, ensuring that customers understand exactly what action they are taking when finalizing their purchase.</p>
        <h3>Settings</h3>
        <h4>Name for Bank Transfer</h4>
        <p>Write the label that you want to appear</p>
        <h4>Name for Cash on Delivery</h4>
        <p>Write the label that you want to appear</p>
        <h3>Appearance</h3>
        <p>There are no appearence options for this module</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_filter("woocommerce_available_payment_gateways", [$this, "wn_rename_place_order_button"]);
    }

    function wn_rename_place_order_button($gateways) {
        if (isset($gateways["bacs"])) {
            $gateways["bacs"]->order_button_text = $this->bacs_text;
        } elseif (isset($gateways["cod"])) {
            $gateways["cod"]->order_button_text = $this->cod_text;
        }
        return $gateways;
    }
}
