<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Condition_Type_Cart_Total' ) ) {

    class Pricely_Admin_Condition_Type_Cart_Total {

        private $group_id;
        private $calc_prefix;

        public function __construct() {

            $this->group_id = 'cart_total';
            $this->calc_prefix = 'cart_totals_';
        }

        public function get_groups( $args ) {

            $excl_modueles = array( 'product-pricing', 'checkout-pricing', 'product-promotions' );

            if ( in_array( $args[ 'module' ], $excl_modueles ) ) {

                return false;
            }

            return array(
                $this->group_id => esc_html__( 'Cart Total', 'pricely-dynamic-pricing-cart-discounts' )
            );
        }

        public function get_conditions( $group_id, $args ) {

            if ( $group_id !== $this->group_id ) {

                return false;
            }

            $currency = get_woocommerce_currency_symbol( get_option( 'woocommerce_currency' ) );

            $conditions = array();

            foreach ( $this->get_cart_total_types( $args ) as $key => $label ) {

                /* translators: 1: cart total label 2: currency symbol */
                $conditions[ $key ] = sprintf( esc_html__( 'Cart Total - %1$s (%2$s)', 'pricely-dynamic-pricing-cart-discounts' ), $label, $currency );
            }

            return $conditions;
        }
        
        public function get_fields( $condition_id, $args ) {

            if ( !in_array( $condition_id, $this->get_cart_total_type_ids( $args ) ) ) {

                return false;
            }

            return $this->get_totals_fields( array() );
        }

        private function get_totals_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'compare',
                'type' => 'select2',
                'default' => '--',
                'options' => array(
                    '--' => esc_html__( 'Between', 'pricely-dynamic-pricing-cart-discounts' ),
                    '>=' => esc_html__( 'More than or equal to', 'pricely-dynamic-pricing-cart-discounts' ),
                    '>' => esc_html__( 'More than', 'pricely-dynamic-pricing-cart-discounts' ),
                    '<=' => esc_html__( 'Less than or equal to', 'pricely-dynamic-pricing-cart-discounts' ),
                    '<' => esc_html__( 'Less than', 'pricely-dynamic-pricing-cart-discounts' ),
                    '==' => esc_html__( 'Equal to', 'pricely-dynamic-pricing-cart-discounts' ),
                    '!=' => esc_html__( 'Not equal to', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '98%',
                'box_width' => '44%',
                'fold_id' => 'cart_totals',
            );

            $in_fields[] = array(
                'id' => 'totals',
                'type' => 'textbox',
                'input_type' => 'number',
                'default' => '0.01',
                'placeholder' => esc_html__( '0.00', 'pricely-dynamic-pricing-cart-discounts' ),
                'attributes' => array(
                    'min' => '0',
                    'step' => '0.01',
                ),
                'width' => '100%',
                'box_width' => '56%',
                'fold' => array(
                    'target' => 'cart_totals',
                    'attribute' => 'value',
                    'value' => array( '--' ),
                    'oparator' => 'neq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'from_totals',
                'type' => 'textbox',
                'input_type' => 'number',
                'default' => '0',
                'placeholder' => esc_html__( '0.00', 'pricely-dynamic-pricing-cart-discounts' ),
                'attributes' => array(
                    'min' => '0',
                    'step' => '0.01',
                ),
                'width' => '98%',
                'box_width' => '28%',
                'fold' => array(
                    'target' => 'cart_totals',
                    'attribute' => 'value',
                    'value' => array( '--' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'to_totals',
                'type' => 'textbox',
                'input_type' => 'number',
                'default' => '0.01',
                'placeholder' => esc_html__( '0.00', 'pricely-dynamic-pricing-cart-discounts' ),
                'attributes' => array(
                    'min' => '0',
                    'step' => '0.01',
                ),
                'width' => '100%',
                'box_width' => '28%',
                'fold' => array(
                    'target' => 'cart_totals',
                    'attribute' => 'value',
                    'value' => array( '--' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            return $in_fields;
        }
        
        private function get_cart_total_type_ids( $args ) {

            return array_keys( $this->get_cart_total_types( $args ) );
        }

        private function get_cart_total_types( $args ) {

            $cart_total_types = array();

            $module_id = $args[ 'module' ];

            foreach ( $this->get_options() as $option ) {

                if ( 'any' !== $option[ 'calc_for' ] && $option[ 'calc_for' ] !== $module_id ) {

                    continue;
                }

                $total_type_id = $this->calc_prefix . $option[ 'option_id' ];

                $total_type_label = $option[ 'admin_note' ];

                if ( empty( $total_type_label ) ) {

                    $total_type_label = esc_html__( 'Cart Totals Calculator', 'pricely-dynamic-pricing-cart-discounts' );
                }

                $cart_total_types[ $total_type_id ] = $total_type_label;
            }

            return $cart_total_types;
        }

        private function get_options() {

            global $pricely_settings;

            if ( isset( $pricely_settings[ 'cart_totals_calcs' ] ) ) {

                return $pricely_settings[ 'cart_totals_calcs' ];
            }

            return $this->get_default_options();
        }

        private function get_default_options() {

            return array(
                array(
                    'admin_note' => esc_html__( 'Subtotal Excluding Tax', 'pricely-dynamic-pricing-cart-discounts' ),
                    'calc_for' => 'any',
                    'option_id' => '2234343',
                ),
                array(
                    'admin_note' => esc_html__( 'Subtotal Including Tax', 'pricely-dynamic-pricing-cart-discounts' ),
                    'calc_for' => 'any',
                    'option_id' => '2234344',
                ),
            );
        }

    }

}