<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Product_Promotion_Types' ) ) {

    require_once (dirname( __FILE__ ) . '/promotion-types-groups.php');
    require_once (dirname( __FILE__ ) . '/promotion-types-countdown.php');
    require_once (dirname( __FILE__ ) . '/promotion-types-volumetric-price-table.php');
    require_once (dirname( __FILE__ ) . '/promotion-types-label.php');

    class Pricely_Admin_Product_Promotion_Types {

        private $groups;
        private $require_product_ids;
        private static $instance;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->groups = array();
            $this->require_product_ids = array();
        }

        public function get_groups( $args ) {

            if ( count( $this->groups ) ) {

                return $this->groups;
            }

            $this->groups = apply_filters( 'pricely_admin_product-promotions_get-promotion-type-groups', array(), $args );

            return $this->groups;
        }

        public function get_types( $args ) {

            $options = array();

            foreach ( $this->get_group_ids( $args ) as $group_id ) {

                $options = $this->get_group_types( $options, $group_id, $args );
            }

            return $options;
        }

        public function get_fields( $option_type_id, $in_fields, $args ) {

            $in_fields = apply_filters( 'pricely_admin_product-promotions_get-promotion-type-' . $option_type_id . '-fields', $in_fields, $args );

            return $this->get_product_args_fields( $option_type_id, $in_fields, $args );
        }

        public function get_shop_loop_types( $args ) {

            return apply_filters( 'pricely_admin_product-promotions_get-shop-loop-types', array(), $args );
        }

        public function get_based_on( $args ) {

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

            return apply_filters( 'pricely_admin_product-promotions_get-based-on-types', $based_on_types, $args );
        }

        public function get_adjustment_types( $args ) {

            return apply_filters( 'pricely_admin_product-promotions_get-adjustment-types', array(), $args );
        }

        public function get_adjustment_based_on_types( $args ) {

            return apply_filters( 'pricely_admin_product-promotions_get-adjustment-based-on-types', array(), $args );
        }

        public function get_adjustment_based_on_visibility( $args ) {

            return apply_filters( 'pricely_admin_product-promotions_get-adjustment-based-on-visibility', array(), $args );
        }

        public function get_calculate_from_visibility( $args ) {

            return apply_filters( 'pricely_admin_product-promotions_get-calculate-from-visibility', array(), $args );
        }

        public function get_product_args_fields( $option_type_id, $in_fields, $args ) {

            if ( !$this->get_requires_products( $option_type_id, $args ) ) {

                return $in_fields;
            }

            $logic_types_product_filters = Pricely_Admin_Logic_Types_Product_Filters::get_instance();

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

        public function get_requires_products( $option_type_id, $args ) {

            $requires_product_ids = $this->get_requires_product_ids( $args );

            return in_array( $option_type_id, $requires_product_ids );
        }

        private function get_group_types( $in_options, $group_id, $args ) {

            $options = apply_filters( 'pricely_admin_product-promotions_get-' . $group_id . '-promotion-types', array(), $args );

            foreach ( $options as $key => $option ) {

                $in_options[ $key ] = array(
                    'title' => $option[ 'title' ],
                    'list_title' => $option[ 'title' ],
                    'group_id' => $group_id,
                );

                if ( isset( $option[ 'tooltip' ] ) ) {

                    $in_options[ $key ][ 'tooltip' ] = $option[ 'tooltip' ];
                }
            }

            return $in_options;
        }

        private function get_group_ids( $args ) {

            $groups = $this->get_groups( $args );

            return array_keys( $groups );
        }

        private function get_requires_product_ids( $args ) {

            if ( count( $this->require_product_ids ) > 0 ) {

                return $this->require_product_ids;
            }

            $options = $this->get_types( $args );

            $option_keys = array_keys( $options );

            $ex_keys = apply_filters( 'pricely_admin_product-promotions_get-type-requires-ex-products', array(), $args );

            foreach ( $option_keys as $option_key ) {

                if ( !in_array( $option_key, $ex_keys ) ) {

                    $this->require_product_ids[] = $option_key;
                }
            }

            return $this->require_product_ids;
        }

    }

}

