<?php

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

if ( !class_exists( 'Reon' ) ) {
    return;
}

if ( !class_exists( 'Pricely_Admin_Checkout_Prices_Modes_Range' ) ) {

    class Pricely_Admin_Checkout_Prices_Modes_Range {

        private $args;

        public function __construct() {

            add_filter( 'pricely_admin_checkout-pricing_get-volume-mode-types', array( $this, 'get_modes' ), 10, 2 );

            add_filter( 'pricely_admin_checkout-pricing_get-mode-type-bulk-fields', array( $this, 'get_fields' ), 10, 2 );

            add_filter( 'reon_get-repeater-field-pricely-quantity_ranges-templates', array( $this, 'get_ranges_templates' ), 10, 2 );

            add_filter( 'reon_get-repeater-template-pricely-quantity_ranges-range_option-fields', array( $this, 'get_ranges_options_fields' ), 10, 2 );

            add_filter( 'pricely_admin_checkout-pricing_process-bulk-prcing-options', array( $this, 'process_options' ), 10, 3 );
        }

        public function get_modes( $modes, $args ) {

            $modes[ 'bulk' ] = esc_html__( 'Volume - Bulk pricing', 'pricely-dynamic-pricing-cart-discounts' );

            return $modes;
        }

        public function get_fields( $in_fields, $args ) {

            $this->args = $args;

            $in_fields[] = array(
                'id' => 'ranges',
                'filter_id' => 'pricely-quantity_ranges',
                'type' => 'repeater',
                'white_repeater' => false,
                'repeater_size' => 'smaller',
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Quantity Ranges', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( 'Use these settings to set up quantity-based pricing ranges', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => array( array( 'pricing_type' => 'fixed_price' ) ),
                'collapsible' => false,
                'accordions' => false,
                'buttons_sep' => false,
                'delete_button' => true,
                'clone_button' => false,
                'css_class' => array( 'pricely_list', 'pricely_list_inner', 'rn-round-small' ),
                'width' => '100%',
                'auto_expand' => array(
                    'all_section' => true,
                    'new_section' => true,
                    'default_section' => true,
                    'cloned_section' => true,
                ),
                'sortable' => array(
                    'enabled' => false,
                ),
                'template_adder' => array(
                    'position' => 'right',
                    'show_list' => false,
                    'button_text' => esc_html__( 'Add Range', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'fold' => array(
                    'target' => 'checkout_prices_mode',
                    'attribute' => 'value',
                    'value' => array( 'bulk' ),
                    'oparator' => 'eq',
                ),
            );

            return $in_fields;
        }

        public function get_ranges_templates( $in_templates, $repeater_args ) {

            $in_templates[] = array(
                'id' => 'range_option',
                'head' => array(
                    'title' => '',
                )
            );

            return $in_templates;
        }

        public function get_ranges_options_fields( $in_fields, $repeater_args ) {

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 12,
                'merge_fields' => false,
                'field_css_class' => array( 'rn-first' ),
                'fields' => $this->get_range_fields( $in_fields, $this->args ),
            );

            return $in_fields;
        }

        public function process_options( $options, $raw_options, $args ) {

            if ( isset( $raw_options[ 'ranges' ] ) ) {

                $options[ 'ranges' ] = $raw_options[ 'ranges' ];
            }

            return $options;
        }

        private function get_range_fields( $in_fields, $args ) {

            $pricing_types = Pricely_Admin_Checkout_Prices_Modes::get_instance();

            $in_fields[] = array(
                'id' => 'min',
                'type' => 'textbox',
                'input_type' => 'number',
                'column_size' => 2,
                'column_title' => esc_html__( 'From - Quantity', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify the minimum number of items for the range', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '1',
                'placeholder' => esc_html__( '0', 'pricely-dynamic-pricing-cart-discounts' ),
                'attributes' => array(
                    'min' => '1',
                    'step' => '1',
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'max',
                'type' => 'textbox',
                'input_type' => 'number',
                'column_size' => 2,
                'column_title' => esc_html__( 'To - Quantity', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify the maximum number of items for the range', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '',
                'placeholder' => esc_html__( 'To - No Limit', 'pricely-dynamic-pricing-cart-discounts' ),
                'attributes' => array(
                    'min' => '0',
                    'step' => '1',
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'pricing_type',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Pricing - Type', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Choose how the price should be calculated for this range', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'fixed_price_item',
                'disabled_list_filter' => 'pricely_admin_get-disabled-grouped-list',
                'options' => $pricing_types->get_pricing_types( 'range' ),
                'width' => '100%',
                'fold_id' => 'range_pricing_type',
            );

            $in_fields[] = array(
                'id' => 'amount',
                'type' => 'textbox',
                'input_type' => 'number',
                'column_size' => 2,
                'column_title' => esc_html__( 'Amount', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify an amount for price calculation based on the chosen "Pricing - Type"', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '0.00',
                'placeholder' => esc_html__( '0.00', 'pricely-dynamic-pricing-cart-discounts' ),
                'attributes' => array(
                    'min' => '0',
                    'step' => '0.01',
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'per_based_on',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Percentage - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Choose the basis for the percentage-based price calculation', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'regular_price',
                'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                'options' => $pricing_types->get_pricing_based_on_types( $args ),
                'width' => '100%',
                'fold' => array(
                    'target' => 'range_pricing_type',
                    'attribute' => 'value',
                    'value' => array( 'per_price' ),
                    'oparator' => 'eq',
                ),
            );

            return $in_fields;
        }

    }

    new Pricely_Admin_Checkout_Prices_Modes_Range();
}