<?php

namespace Webnestors\Wsn\Snippets;

class CheckoutRemoveCheckoutFields extends \Webnestors\Wsn\Snippet {
    public $billing_first_name = false;
    public $billing_last_name = false;
    public $billing_company = false;
    public $billing_address_1 = false;
    public $billing_address_2 = false;
    public $billing_city = false;
    public $billing_postcode = false;
    public $billing_country = false;
    public $billing_state = false;
    public $billing_phone = false;
    public $shipping_first_name = false;
    public $shipping_last_name = false;
    public $shipping_company = false;
    public $shipping_address_1 = false;
    public $shipping_address_2 = false;
    public $shipping_city = false;
    public $shipping_postcode = false;
    public $shipping_country = false;
    public $shipping_state = false;
    public $order_comments = false;

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

    public function title(): string {
        return __("Remove Checkout Fields", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return __("Choose to remove billing, shipping or order comment fields as you wish.", "enhancements-for-woocommerce");
    }

    public function meta(): array {
        return [
            [
                "label" => __("Billing First Name", "enhancements-for-woocommerce"),
                "name" => "billing_first_name",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing Last Name", "enhancements-for-woocommerce"),
                "name" => "billing_last_name",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing Company", "enhancements-for-woocommerce"),
                "name" => "billing_company",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing Address 1", "enhancements-for-woocommerce"),
                "name" => "billing_address_1",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing Address 2", "enhancements-for-woocommerce"),
                "name" => "billing_address_2",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing City", "enhancements-for-woocommerce"),
                "name" => "billing_city",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing Postal Code", "enhancements-for-woocommerce"),
                "name" => "billing_postcode",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing Country", "enhancements-for-woocommerce"),
                "name" => "billing_country",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing State", "enhancements-for-woocommerce"),
                "name" => "billing_state",
                "type" => "checkbox",
            ],
            [
                "label" => __("Billing Phone", "enhancements-for-woocommerce"),
                "name" => "billing_phone",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping First Name", "enhancements-for-woocommerce"),
                "name" => "shipping_first_name",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping Last Name", "enhancements-for-woocommerce"),
                "name" => "shipping_last_name",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping Company", "enhancements-for-woocommerce"),
                "name" => "shipping_company",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping Address 1", "enhancements-for-woocommerce"),
                "name" => "shipping_address_1",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping Address 2", "enhancements-for-woocommerce"),
                "name" => "shipping_address_2",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping City", "enhancements-for-woocommerce"),
                "name" => "shipping_city",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping Postal Code", "enhancements-for-woocommerce"),
                "name" => "shipping_postcode",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping Country", "enhancements-for-woocommerce"),
                "name" => "shipping_country",
                "type" => "checkbox",
            ],
            [
                "label" => __("Shipping State", "enhancements-for-woocommerce"),
                "name" => "shipping_state",
                "type" => "checkbox",
            ],
            [
                "label" => __("Order Commnets", "enhancements-for-woocommerce"),
                "name" => "order_comments",
                "type" => "checkbox",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Remove Checkout Fields module provides the ability to customize the checkout experience by allowing you to remove unnecessary or unwanted fields from the WooCommerce checkout page. With this feature, you can choose to remove billing, shipping, or order comment fields, streamlining the checkout process and making it faster and more user-friendly for your customers.</p>
        <p>This module is ideal for stores that don’t require extensive information from customers during checkout, such as digital product stores, businesses that offer in-store pickup, or stores that simply want to simplify the checkout experience to improve conversion rates.</p>
        <h3>Settings</h3>
        <p>Just check the field that you want to be removed</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_checkout_fields", [$this, "wn_custom_override_checkout_fields"]);
    }

    function wn_custom_override_checkout_fields($fields) {
        if ($this->billing_first_name == true) {
            unset($fields["billing"]["billing_first_name"]);
        }
        if ($this->billing_last_name == true) {
            unset($fields["billing"]["billing_last_name"]);
        }
        if ($this->billing_company == true) {
            unset($fields["billing"]["billing_company"]);
        }
        if ($this->billing_address_1 == true) {
            unset($fields["billing"]["billing_address_1"]);
        }
        if ($this->billing_address_2 == true) {
            unset($fields["billing"]["billing_address_2"]);
        }
        if ($this->billing_city == true) {
            unset($fields["billing"]["billing_city"]);
        }
        if ($this->billing_postcode == true) {
            unset($fields["billing"]["billing_postcode"]);
        }
        if ($this->billing_country == true) {
            unset($fields["billing"]["billing_country"]);
        }
        if ($this->billing_state == true) {
            unset($fields["billing"]["billing_state"]);
        }
        if ($this->billing_phone == true) {
            unset($fields["billing"]["billing_phone"]);
        }
        if ($this->shipping_first_name == true) {
            unset($fields["shipping"]["shipping_first_name"]);
        }
        if ($this->shipping_last_name == true) {
            unset($fields["shipping"]["shipping_last_name"]);
        }
        if ($this->shipping_company == true) {
            unset($fields["shipping"]["shipping_company"]);
        }
        if ($this->shipping_address_1 == true) {
            unset($fields["shipping"]["shipping_address_1"]);
        }
        if ($this->shipping_address_2 == true) {
            unset($fields["shipping"]["shipping_address_2"]);
        }
        if ($this->shipping_city == true) {
            unset($fields["shipping"]["shipping_city"]);
        }
        if ($this->shipping_postcode == true) {
            unset($fields["shipping"]["shipping_postcode"]);
        }
        if ($this->shipping_country == true) {
            unset($fields["shipping"]["shipping_country"]);
        }
        if ($this->shipping_state == true) {
            unset($fields["shipping"]["shipping_state"]);
        }
        if ($this->order_comments == true) {
            unset($fields["shipping"]["order_comments"]);
        }

        return $fields;
    }
}
