<?php

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

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

if ( !class_exists( 'Pricely_Admin_Checkout_Prices_Panel_Options' ) ) {

    class Pricely_Admin_Checkout_Prices_Panel_Options {

        private $option_name = PRICELY_OPTION_NAME;

        public function __construct() {

            add_filter( 'pricely_admin_checkout-pricing_get-panel-option-fields', array( $this, 'get_fields' ), 10 );

            add_filter( 'reon_sanitize-' . $this->option_name . '-checkout_pricings-tooltip', array( $this, 'sanitize_kses' ), 10 );
            add_filter( 'pricely_admin_checkout-pricing_process-options', array( $this, 'process_options' ), 10 );
        }

        public function get_fields( $in_fields ) {
            
            if ( class_exists( 'Pricely_Pro_Admin_Checkout_Prices_Panel_Options' ) ) {

                return $in_fields;
            }

            $pricing_types = Pricely_Admin_Checkout_Prices_Modes::get_instance();

            $args = array(
                'module' => 'checkout-pricing',
                'format_text' => esc_html__( 'pricing calculation', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 8,
                'merge_fields' => false,
                'fields' => array(
                    array(
                        'id' => 'tooltip',
                        'type' => 'textarea',
                        'column_size' => 3,
                        'column_title' => esc_html__( 'Description (Tooltip)', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Add tooltip text to provide pricing info. on cart and checkout pages', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => '',
                        'rows' => 1,
                        'placeholder' => esc_html__( 'Tooltip....', 'pricely-dynamic-pricing-cart-discounts' ),
                        'width' => '100%',
                    ),
                    array(
                        'id' => 'group_by',
                        'type' => 'select2',
                        'column_size' => 2,
                        'column_title' => esc_html__( 'Quantity - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Specify how product quantity should be counted when calculating prices', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => 'product_id',
                        'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                        'options' => $pricing_types->get_quantity_based_on(),
                        'width' => '100%',
                        'fold' => array(
                            'target' => 'checkout_prices_mode',
                            'attribute' => 'value',
                            'value' => $pricing_types->get_quantity_visibility_ids( $args ),
                            'oparator' => 'eq',
                        ),
                    ),
                    array(
                        'id' => 'sort_by',
                        'type' => 'select2',
                        'column_size' => 2,
                        'column_title' => esc_html__( 'Sort Items', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Specify the sorting method for items in the cart when calculating prices', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => 'no',
                        'options' => array(
                            'no' => esc_html__( 'No, Do not sort items', 'pricely-dynamic-pricing-cart-discounts' ),
                        ),
                        'width' => '100%',
                        'fold' => array(
                            'target' => 'checkout_prices_mode',
                            'attribute' => 'value',
                            'value' => $pricing_types->get_quantity_visibility_ids( $args ),
                            'oparator' => 'eq',
                        ),
                    ),
                ),
            );

            foreach ( $pricing_types->get_mode_type_ids( $args ) as $mode_type ) {

                $in_fields = $pricing_types->get_mode_fields( $in_fields, $mode_type, $args );
            }


            $logic_types_product_filters = Pricely_Admin_Logic_Types_Product_Filters::get_instance();

            $fold_args = array(
                'target' => 'checkout_prices_mode',
                'attribute' => 'value',
                'value' => $pricing_types->get_products_visibility_ids( $args ),
                'oparator' => 'eq',
            );

            return $logic_types_product_filters->get_fields( $in_fields, $args, $fold_args );
        }

        public function sanitize_kses( $option ) {
            
            if ( class_exists( 'Pricely_Pro_Admin_Checkout_Prices_Panel_Options' ) ) {

                return $option;
            }

            $admin_page = Pricely_Admin_Page::get_instance();

            return $admin_page->sanitize_kses( $option );
        }

        public function process_options( $raw_options ) {
            
            if ( class_exists( 'Pricely_Pro_Admin_Checkout_Prices_Panel_Options' ) ) {

                return $raw_options;
            }

            $pricing_types = Pricely_Admin_Checkout_Prices_Modes::get_instance();

            $args = array(
                'module' => 'checkout-pricing',
            );

            $pricing_mode = $raw_options[ 'mode' ];

            $options = array(
                'pricing_id' => $raw_options[ 'pricing_id' ],
                'enable' => $raw_options[ 'enable' ],
                'apply_mode' => $raw_options[ 'apply_mode' ],
                'mode' => $pricing_mode,
                'admin_note' => $raw_options[ 'admin_note' ],
                'tooltip' => $raw_options[ 'tooltip' ],
            );

            $quantity_visibility_ids = $pricing_types->get_quantity_visibility_ids( $args );

            if ( in_array( $pricing_mode, $quantity_visibility_ids ) ) {

                $options[ 'group_by' ] = $raw_options[ 'group_by' ];
                $options[ 'sort_by' ] = $raw_options[ 'sort_by' ];
            }

            $filter_key = 'checkout-pricing_process-' . $pricing_mode . '-prcing-options';

            if ( has_filter( 'pricely_admin_' . $filter_key ) ) {

                $options = apply_filters( 'pricely_admin_' . $filter_key, $options, $raw_options, $args );
            }

            $products_visibility_ids = $pricing_types->get_products_visibility_ids( $args );

            if ( in_array( $pricing_mode, $products_visibility_ids ) && isset( $raw_options[ 'product_args' ] ) ) {

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

            return apply_filters( 'pricely_admin_checkout-pricing_process-pricing-options', $options, $raw_options, $args );
        }

    }

    new Pricely_Admin_Checkout_Prices_Panel_Options();
}