<?php

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

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

if ( !class_exists( 'Pricely_Admin_Checkout_Prices_Modes' ) ) {

    require_once (dirname( __FILE__ ) . '/checkout-price-mode-simple.php');
    require_once (dirname( __FILE__ ) . '/checkout-price-mode-range.php');

    class Pricely_Admin_Checkout_Prices_Modes {

        private $mode_types = array();
        private $products_visibility_ids;
        private $quantity_visibility_ids;
        private $pricing_types;
        private static $instance;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function get_modes( $args ) {

            $module_key = $args[ 'module' ];

            if ( isset( $this->mode_types[ $module_key ] ) ) {

                return $this->mode_types[ $module_key ];
            }

            $default_group_types = array(
                'simple' => esc_html__( 'Simple Pricing', 'pricely-dynamic-pricing-cart-discounts' ),
                'volume' => esc_html__( 'Volume Pricing', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $group_types = apply_filters( 'pricely_admin_checkout-pricing_get-mode-groups', $default_group_types, $args );


            foreach ( $group_types as $group_type_id => $group_type ) {

                $this->mode_types[ $module_key ][ $group_type_id ][ 'label' ] = $group_type;

                $this->mode_types[ $module_key ][ $group_type_id ][ 'options' ] = apply_filters( 'pricely_admin_checkout-pricing_get-' . $group_type_id . '-mode-types', array(), $args );
            }

            return $this->mode_types[ $module_key ];
        }

        public function get_mode_type_ids( $args ) {

            $mode_type_ids = array();

            foreach ( $this->get_modes( $args ) as $group_type ) {

                $mode_type_ids = $this->get_group_type_option_ids( $mode_type_ids, $group_type );
            }

            return $mode_type_ids;
        }

        public function get_mode_fields( $in_fields, $mode_type, $args ) {

            $fields = apply_filters( 'pricely_admin_checkout-pricing_get-mode-type-' . $mode_type . '-fields', array(), $args );

            foreach ( $fields as $field ) {

                if ( !isset( $field[ 'fold' ] ) ) {

                    $field[ 'fold' ] = array(
                        'target' => 'checkout_prices_mode',
                        'attribute' => 'value',
                        'value' => $mode_type,
                        'oparator' => 'eq',
                    );
                }

                $in_fields[] = $field;
            }

            return $in_fields;
        }

        public function get_pricing_based_on_types( $args ) {

            $based_on_types = array(
                'regular_price' => esc_html__( 'Product Prices - Regular price', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return apply_filters( 'pricely_admin_checkout-pricing_get-pricing-based-on-types', $based_on_types, $args );
        }

        public function get_pricing_types( $mode_id ) {

            if ( !$this->pricing_types ) {

                $this->pricing_types = apply_filters( 'pricely_admin_checkout-pricing_get-pricing-types', $this->prepare_pricing_types() );
            }

            if ( isset( $this->pricing_types[ $mode_id ] ) ) {

                return $this->pricing_types[ $mode_id ];
            }

            if ( isset( $this->pricing_types[ 'default' ] ) ) {

                return $this->pricing_types[ 'default' ];
            }

            return array();
        }

        public function get_quantity_based_on() {

            $based_on_options = array(
                'product_id' => esc_html__( 'Individual product', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return apply_filters( 'pricely_admin_checkout-pricing_get-quantity-based-on', $based_on_options );
        }

        public function get_quantity_visibility_ids( $args ) {

            if ( $this->quantity_visibility_ids ) {

                return $this->quantity_visibility_ids;
            }

            $group_ids = $this->get_mode_type_ids( $args );

            $ex_group_ids = apply_filters( 'pricely_admin_checkout-pricing_get-excl-quantity-ids', array(), $args );

            $visibility_ids = array();

            foreach ( $group_ids as $group_id ) {

                if ( in_array( $group_id, $ex_group_ids ) ) {

                    continue;
                }

                $visibility_ids[] = $group_id;
            }

            $this->quantity_visibility_ids = $visibility_ids;

            return $this->quantity_visibility_ids;
        }

        public function get_products_visibility_ids( $args ) {

            if ( $this->products_visibility_ids ) {

                return $this->products_visibility_ids;
            }

            $group_ids = $this->get_mode_type_ids( $args );

            $ex_group_ids = apply_filters( 'pricely_admin_checkout-pricing_get-excl-products-ids', array(), $args );

            $visibility_ids = array();

            foreach ( $group_ids as $group_id ) {

                if ( in_array( $group_id, $ex_group_ids ) ) {

                    continue;
                }

                $visibility_ids[] = $group_id;
            }

            $this->products_visibility_ids = $visibility_ids;

            return $this->products_visibility_ids;
        }

        private function prepare_pricing_types() {

            $currency = get_woocommerce_currency_symbol( get_woocommerce_currency() );

            return array(
                'default' => $this->get_default_pricing_types( $currency ),
                'range' => $this->get_range_pricing_types( $currency ),
            );
        }

        private function get_default_pricing_types( $currency ) {

            $pricing_types = array();

            $pricing_types[ 'price' ][ 'label' ] = esc_html__( 'Price', 'pricely-dynamic-pricing-cart-discounts' );
            $pricing_types[ 'price' ][ 'options' ] = array(
                /* translators: 1: currency symbol */
                'fixed_price_item' => sprintf( esc_html__( 'Fixed price per item (%s)', 'pricely-dynamic-pricing-cart-discounts' ), $currency ),
                'per_price' => esc_html__( 'Percentage price (%)', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return $pricing_types;
        }

        private function get_range_pricing_types( $currency ) {

            $pricing_types = array();

            $pricing_types[ 'price' ][ 'label' ] = esc_html__( 'Price', 'pricely-dynamic-pricing-cart-discounts' );
            $pricing_types[ 'price' ][ 'options' ] = array(
                /* translators: 1: currency symbol */
                'fixed_price_item' => sprintf( esc_html__( 'Fixed price per item (%s)', 'pricely-dynamic-pricing-cart-discounts' ), $currency ),
                'per_price' => esc_html__( 'Percentage price (%)', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return $pricing_types;
        }

        private function get_group_type_option_ids( $option_ids, $group_type ) {

            if ( !isset( $group_type[ 'options' ] ) ) {

                return $option_ids;
            }

            if ( !is_array( $group_type[ 'options' ] ) ) {

                return $option_ids;
            }

            $group_type_keys = array_keys( $group_type[ 'options' ] );

            foreach ( $group_type_keys as $key ) {

                if ( in_array( $key, $option_ids ) ) {

                    continue;
                }

                $option_ids[] = $key;
            }

            return $option_ids;
        }

    }

}
    