<?php

/**
 * Admin section.
 *
 * @package    Extra_Product_Options_For_WooCommerce
 * @subpackage Extra_Product_Options_For_WooCommerce/includes
 */
// If this file is called directly, abort.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * EPOFW_Woocs_Compatiblity class.
 */
if ( !class_exists( 'EPOFW_Woocs_Compatiblity' ) ) {
    /**
     * EPOFW_Woocs_Compatiblity class.
     */
    class EPOFW_Woocs_Compatiblity {
        /**
         * The object of class.
         *
         * @since    1.0.0
         * @var      string $instance instance object.
         */
        protected static $instance = null;

        /**
         * The name of this plugin.
         *
         * @var      string $plugin_name The ID of this plugin.
         *
         * @since    1.0.0
         */
        public function __construct() {
            $this->epofw_woocs_cmp_init();
        }

        /**
         * Define the plugins name and versions and also call admin section.
         *
         * @since 1.0.0
         */
        public static function instance() {
            if ( is_null( self::$instance ) ) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        /**
         * Function will call filters.
         */
        public function epofw_woocs_cmp_init() {
            // phpcs:ignore.
            global $WOOCS;
            // phpcs:ignore.
            $default_currency = $WOOCS->default_currency;
            $woocs_raw_woocommerce_price_currency = filter_input( INPUT_GET, 'woocs_raw_woocommerce_price_currency', FILTER_SANITIZE_SPECIAL_CHARS );
            $current_currency = ( !empty( $woocs_raw_woocommerce_price_currency ) ? sanitize_text_field( wp_unslash( $woocs_raw_woocommerce_price_currency ) ) : get_woocommerce_currency() );
            if ( $current_currency !== $default_currency ) {
                add_filter(
                    'epofw_price_filter',
                    array($this, 'epofw_price_filter_callback'),
                    10,
                    1
                );
                remove_action( 'woocommerce_before_calculate_totals', array(EPOFW_Front::instance(), 'epofw_before_calculate_totals') );
                add_action(
                    'woocommerce_before_calculate_totals',
                    array($this, 'epofw_woocs_before_calculate_totals'),
                    10,
                    1
                );
            }
            if ( is_admin() ) {
                add_filter(
                    'epofw_price_filter',
                    array($this, 'epofw_price_filter_callback'),
                    10,
                    1
                );
            }
            add_action( 'wp_enqueue_scripts', array($this, 'epofw_front_woocs_enqueue_scripts_fn') );
        }

        /**
         * Function will enqueue styles and scripts.
         */
        public function epofw_front_woocs_enqueue_scripts_fn() {
            $get_woocs_option = get_option( 'woocs' );
            $woocs_raw_woocommerce_price_currency = filter_input( INPUT_GET, 'woocs_raw_woocommerce_price_currency', FILTER_SANITIZE_SPECIAL_CHARS );
            $current_currency = ( !empty( $woocs_raw_woocommerce_price_currency ) ? sanitize_text_field( wp_unslash( $woocs_raw_woocommerce_price_currency ) ) : get_woocommerce_currency() );
            if ( isset( $get_woocs_option[$current_currency] ) ) {
                wp_enqueue_script( 'epofw-front-js' );
                global $post;
                $product_data = ( !empty( $post->ID ) ? wc_get_product( $post->ID ) : false );
                if ( $product_data ) {
                    // Get tax information for product page display.
                    $tax_rate = 0;
                    $tax_label = '';
                    $is_taxable = false;
                    $prices_incl_tax = wc_prices_include_tax();
                    $tax_display_shop = get_option( 'woocommerce_tax_display_shop', 'excl' );
                    $all_tax_rates = array();
                    $base_location = wc_get_base_location();
                    // Get product-specific tax information.
                    if ( $product_data->is_taxable() ) {
                        $is_taxable = true;
                        $tax_class = $product_data->get_tax_class();
                        // Get product's tax rate.
                        $tax_rates = WC_Tax::find_rates( array(
                            'country'   => $base_location['country'],
                            'state'     => $base_location['state'],
                            'tax_class' => $tax_class,
                        ) );
                        if ( !empty( $tax_rates ) ) {
                            $first_rate = reset( $tax_rates );
                            $tax_rate = ( isset( $first_rate['rate'] ) ? floatval( $first_rate['rate'] ) : 0 );
                            $tax_label = ( isset( $first_rate['label'] ) ? $first_rate['label'] : __( 'Tax', 'extra-product-options-for-woocommerce' ) );
                        }
                    }
                    // Always populate all_tax_rates when WooCommerce tax is enabled.
                    // This allows addons to have their own tax class even if the product is non-taxable.
                    if ( wc_tax_enabled() ) {
                        // Get all tax rates for addon-specific tax display.
                        $standard_rates = WC_Tax::find_rates( array(
                            'country'   => $base_location['country'],
                            'state'     => $base_location['state'],
                            'tax_class' => '',
                        ) );
                        if ( !empty( $standard_rates ) ) {
                            $first_standard = reset( $standard_rates );
                            $all_tax_rates['standard'] = array(
                                'rate'  => ( isset( $first_standard['rate'] ) ? floatval( $first_standard['rate'] ) : 0 ),
                                'label' => ( isset( $first_standard['label'] ) ? $first_standard['label'] : __( 'Tax', 'extra-product-options-for-woocommerce' ) ),
                            );
                            // 'bops' uses product's tax rate if taxable, else standard.
                            $all_tax_rates['bops'] = ( $is_taxable && $tax_rate > 0 ? array(
                                'rate'  => $tax_rate,
                                'label' => $tax_label,
                            ) : $all_tax_rates['standard'] );
                        }
                        $reduced_rates = WC_Tax::find_rates( array(
                            'country'   => $base_location['country'],
                            'state'     => $base_location['state'],
                            'tax_class' => 'reduced-rate',
                        ) );
                        if ( !empty( $reduced_rates ) ) {
                            $first_reduced = reset( $reduced_rates );
                            $all_tax_rates['reduced-rate'] = array(
                                'rate'  => ( isset( $first_reduced['rate'] ) ? floatval( $first_reduced['rate'] ) : 0 ),
                                'label' => ( isset( $first_reduced['label'] ) ? $first_reduced['label'] : __( 'Reduced Rate', 'extra-product-options-for-woocommerce' ) ),
                            );
                        }
                        $all_tax_rates['zero-rate'] = array(
                            'rate'  => 0,
                            'label' => __( 'Zero Rate', 'extra-product-options-for-woocommerce' ),
                        );
                    }
                    wp_localize_script( 'epofw-front-js', 'epofw_front_var', array(
                        'ajaxurl'                          => admin_url( 'admin-ajax.php' ),
                        'current_post_id'                  => $post->ID,
                        'product_price'                    => epofw_display_product_price( $product_data, 'shop' ),
                        'currency'                         => get_woocommerce_currency_symbol(),
                        'position'                         => ( isset( $get_woocs_option[$current_currency]['position'] ) ? $get_woocs_option[$current_currency]['position'] : get_option( 'woocommerce_currency_pos' ) ),
                        'decimal_separator'                => wc_get_price_decimal_separator(),
                        'thousand_separator'               => wc_get_price_thousand_separator(),
                        'decimals'                         => wc_get_price_decimals(),
                        'tax_rate'                         => $tax_rate,
                        'tax_label'                        => $tax_label,
                        'is_taxable'                       => $is_taxable,
                        'prices_include_tax'               => $prices_incl_tax,
                        'tax_display_shop'                 => $tax_display_shop,
                        'all_tax_rates'                    => $all_tax_rates,
                        'including_tax_text'               => __( 'Including %s', 'extra-product-options-for-woocommerce' ),
                        'timepicker_select_validation'     => __( 'Please select a valid time.', 'extra-product-options-for-woocommerce' ),
                        'timepicker_change_validation'     => __( 'Please enter a valid time.', 'extra-product-options-for-woocommerce' ),
                        'datepicker_select_validation'     => __( 'Please select a valid date.', 'extra-product-options-for-woocommerce' ),
                        'datepicker_change_validation'     => __( 'Please enter a valid date.', 'extra-product-options-for-woocommerce' ),
                        'colorpicker_select_validation'    => __( 'Please select a valid color code.', 'extra-product-options-for-woocommerce' ),
                        'colorpicker_change_validation'    => __( 'Please enter a valid color code.', 'extra-product-options-for-woocommerce' ),
                        'switch_on_text'                   => __( 'On', 'extra-product-options-for-woocommerce' ),
                        'switch_off_text'                  => __( 'Off', 'extra-product-options-for-woocommerce' ),
                        'step'                             => __( 'step', 'extra-product-options-for-woocommerce' ),
                        'steps'                            => __( 'steps', 'extra-product-options-for-woocommerce' ),
                        'is_archive'                       => is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy(),
                        'remove_uploaded_images_ajax'      => wp_create_nonce( 'epofw_remove_uploaded_images_ajax' ),
                        'uploaded_images_ajax'             => wp_create_nonce( 'epofw_uploaded_images_ajax' ),
                        'configure_text'                   => ( defined( 'EPOFW_CONFIGURE_TEXT' ) ? EPOFW_CONFIGURE_TEXT : __( 'Click to configure options', 'extra-product-options-for-woocommerce' ) ),
                        'options_configured_text_singular' => __( '%s option configured', 'extra-product-options-for-woocommerce' ),
                        'options_configured_text_plural'   => __( '%s options configured', 'extra-product-options-for-woocommerce' ),
                    ) );
                }
            }
        }

        /**
         * Function will return price based on currency switcher.
         *
         * @param mixed $epofw_price Price of option.
         *
         * @return float|int|string
         */
        public function epofw_price_filter_callback( $epofw_price ) {
            if ( class_exists( 'WOOCS' ) ) {
                // phpcs:ignore.
                global $WOOCS;
                // phpcs:ignore.
                return $WOOCS->woocs_exchange_value( floatval( $epofw_price ) );
            }
            return $epofw_price;
        }

        /**
         * Function will calculate addon price based on currency switcher.
         *
         * @param object $cart_object Cart object data.
         */
        public function epofw_woocs_before_calculate_totals( $cart_object ) {
            if ( class_exists( 'WOOCS' ) ) {
                if ( is_admin() && !defined( 'DOING_AJAX' ) ) {
                    return;
                }
                if ( method_exists( $cart_object, 'get_cart' ) ) {
                    $cart_contents = $cart_object->get_cart();
                } else {
                    $cart_contents = $cart_object->cart_contents;
                }
                if ( !empty( $cart_contents ) ) {
                    foreach ( $cart_contents as $cart_key => $cart_item ) {
                        if ( isset( $cart_item['epofw_data'] ) ) {
                            if ( isset( $cart_item['product_id'] ) ) {
                                $product = wc_get_product( $cart_item['product_id'] );
                                if ( $product ) {
                                    $has_regular_price = is_numeric( $cart_item['data']->get_regular_price( 'edit' ) );
                                    if ( $has_regular_price ) {
                                        $product_price = (float) $cart_item['data']->get_regular_price( 'edit' );
                                    }
                                    $has_sale_price = is_numeric( $cart_item['data']->get_sale_price( 'edit' ) );
                                    if ( $has_sale_price ) {
                                        $product_price = (float) $cart_item['data']->get_sale_price( 'edit' );
                                    }
                                }
                            } else {
                                $product_price = 0;
                            }
                            $total_price = $product_price;
                            $cart_item['data']->set_price( $total_price );
                        }
                        $cart_contents[$cart_key] = $cart_item;
                    }
                }
                if ( method_exists( $cart_object, 'set_cart_contents' ) ) {
                    $cart_object->set_cart_contents( $cart_contents );
                } else {
                    $cart_object->cart_contents = $cart_contents;
                }
            }
        }

    }

}