<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Amount_Types' ) ) {

    Reon_Utils::get_instance()->recursive_require( dirname( __FILE__ ), array( 'amount-types.php' ) );

    class Pricely_Admin_Amount_Types {

        private $amount_types;
        private $products_required_ids;
        private static $instance = null;

        public static function get_instance(): self {

            if ( null == self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function __construct() {

            $this->amount_types = array();
            $this->products_required_ids = array();
        }

        public function get_types( $args ) {

            if ( isset( $this->amount_types[ $args[ 'module' ] ] ) ) {

                return $this->amount_types[ $args[ 'module' ] ];
            }

            $type_groups = apply_filters( 'pricely_admin_get-amount-type-groups', array(), $args );

            foreach ( $type_groups as $key => $type_group ) {

                $this->amount_types[ $args[ 'module' ] ][ $key ][ 'label' ] = $type_group;

                $this->amount_types[ $args[ 'module' ] ][ $key ][ 'options' ] = apply_filters( 'pricely_admin_get-amount-group-types-' . $key, array(), $args );
            }

            return $this->amount_types[ $args[ 'module' ] ];
        }

        public function get_type_fields( $args ) {

            $type_fields = array();

            $target = $args[ 'fold_id' ];

            $amount_type_ids = $this->get_type_ids( $args );

            $disabled_list = Pricely_Admin_Page::get_instance()->get_disabled_list( array(), $amount_type_ids );

            foreach ( $amount_type_ids as $amount_type_id ) {

                if ( in_array( $amount_type_id, $disabled_list ) ) {

                    continue;
                }

                $args[ 'amount_type' ] = $amount_type_id;

                $type_flds = apply_filters( 'pricely_admin_get-amount-type-' . $amount_type_id . '-fields', array(), $args );

                foreach ( $type_flds as $type_fld ) {

                    $type_fld[ 'fold' ] = array(
                        'target' => $target,
                        'attribute' => 'value',
                        'value' => $amount_type_id,
                        'oparator' => 'eq',
                        'clear' => false,
                    );

                    $type_fields[] = $type_fld;
                }
            }

            return $type_fields;
        }

        public function get_other_fields( $in_fields, $args ) {

            return $this->get_product_args_fields( apply_filters( 'pricely_admin_get-amount-type-fields', $in_fields, $args ), $args );
        }

        public function get_amount_add_methods( $args ) {

            $options = array(
                'add' => esc_html__( 'Add to previous amounts', 'pricely-dynamic-pricing-cart-discounts' )
            );

            return apply_filters( 'pricely_admin_get-amount-add-methods', $options, $args );
        }

        public function get_type_ids( $args ) {

            $amount_type_ids = array();

            foreach ( $this->get_types( $args ) as $key => $amount_type ) {

                if ( isset( $amount_type[ 'options' ] ) ) {

                    foreach ( $amount_type[ 'options' ] as $sub_key => $a_type ) {

                        $amount_type_ids[] = $sub_key;
                    }
                } else {

                    $amount_type_ids[] = $key;
                }
            }

            return $amount_type_ids;
        }

        public function get_amount_box_fields( $in_fields, $args ) {

            $in_fields[] = array(
                'id' => 'based_on',
                'type' => 'select2',
                'column_size' => 4,
                'column_title' => esc_html__( 'Percentage - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Select the basis for calculating the percentage-based amount', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '2234343',
                'data' => $this->get_cart_totals_type( $args ),
                'width' => '100%',
                'fold' => array(
                    'target' => 'amount_type',
                    'attribute' => 'value',
                    'value' => $this->get_based_on_required_ids( $args ),
                    'oparator' => 'eq',
                    'clear' => true,
                    'empty' => '2234343',
                ),
            );

            $item_based_on_default = (class_exists( 'Pricely_Pro_Admin_Amount_Types' )) ? 'price_in_cart' : 'sale_price';

            $in_fields[] = array(
                'id' => 'item_based_on',
                'type' => 'select2',
                'column_size' => 4,
                'column_title' => esc_html__( 'Percentage - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Select the basis for calculating the percentage-based amount', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => $item_based_on_default,
                'options' => $this->get_item_based_on_options( $args ),
                'disabled_list_filter' => 'pricely_admin_get-disabled-grouped-list',
                'width' => '100%',
                'fold' => array(
                    'target' => 'amount_type',
                    'attribute' => 'value',
                    'value' => $this->get_item_based_on_required_ids( $args ),
                    'oparator' => 'eq',
                    'clear' => true,
                    'empty' => 'sale_price',
                ),
            );

            $fields = apply_filters( 'pricely_admin_get-amount-box-fields', array(), $args );

            foreach ( $fields as $field ) {

                $in_fields[] = $field;
            }

            return $in_fields;
        }

        public function process_options( $raw_options, $args ) {

            $amount_type = $raw_options[ 'amount_type' ];

            $options = array(
                'id' => 0,
                'amount_type' => $amount_type,
                'adjustment_type' => $raw_options[ 'adjustment_type' ],
                'apply_conditions' => $raw_options[ 'apply_conditions' ],
                'amount' => 0,
            );

            if ( is_numeric( $raw_options[ 'id' ] ) ) {

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


            if ( is_numeric( $raw_options[ 'amount' ] ) ) {

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

            $filter_key = 'process-' . $amount_type . '-amount-options';

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

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

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

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

            $products_required_ids = $this->get_products_required_ids( $args );

            if ( in_array( $amount_type, $products_required_ids ) && isset( $raw_options[ 'product_args' ] ) ) {

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

            $other_filter_key = 'process-amount-options';

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

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

            return $options;
        }

        private function get_based_on_required_ids( $args ) {

            return apply_filters( 'pricely_admin_get-based-on-required-ids', array(), $args );
        }

        private function get_item_based_on_required_ids( $args ) {

            return apply_filters( 'pricely_admin_get-item-based-on-required-ids', array(), $args );
        }

        private function get_products_required_ids( $args ) {

            if ( isset( $this->products_required_ids[ $args[ 'module' ] ] ) ) {

                return $this->products_required_ids[ $args[ 'module' ] ];
            }

            $this->products_required_ids[ $args[ 'module' ] ] = apply_filters( 'pricely_admin_get-products-required-ids', array(), $args );

            return $this->products_required_ids[ $args[ 'module' ] ];
        }

        private function get_item_based_on_options( $args ) {

            if ( class_exists( 'Pricely_Pro_Admin_Amount_Types' ) ) {

                return Pricely_Pro_Admin_Amount_Types::get_instance()->get_item_based_on_options( $args );
            }

            $options = array(
                'item_prices' => array(
                    'label' => esc_html__( 'Product Prices', 'pricely-dynamic-pricing-cart-discounts' ),
                    'options' => array(
                        'regular_price' => esc_html__( 'Product Prices - Regular price', 'pricely-dynamic-pricing-cart-discounts' ),
                        'sale_price' => esc_html__( 'Product Prices - Sale price', 'pricely-dynamic-pricing-cart-discounts' ),
                    ),
                ),
            );

            return $options;
        }

        private function get_product_args_fields( $in_fields, $args ) {

            $logic_types_product_filters = Pricely_Admin_Logic_Types_Product_Filters::get_instance();

            $fold_args = array(
                'target' => $args[ 'fold_id' ],
                'attribute' => 'value',
                'value' => $this->get_products_required_ids( $args ),
                'oparator' => 'eq',
            );

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

        private function get_conditions_fields( $in_fields, $args ) {

            if ( !class_exists( 'Pricely_Pro_Admin_Logic_Types_Conditions' ) ) {

                return $in_fields;
            }

            return Pricely_Pro_Admin_Logic_Types_Conditions::get_instance()->get_amount_fields( $in_fields, $args );
        }

        private function get_cart_totals_type( $args ) {

            if ( !class_exists( 'Pricely_Pro_Admin_Amount_Types' ) ) {

                return 'pricely:cart_discounts_totals';
            }

            return Pricely_Pro_Admin_Amount_Types::get_instance()->get_cart_totals_type( $args );
        }

    }

}