<?php

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

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

if ( !class_exists( 'Pricely_Admin_Product_Filter_Types' ) ) {

    require_once (dirname( __FILE__ ) . '/product-filter-types-product.php');

    class Pricely_Admin_Product_Filter_Types {

        private static $instance;
        private $filter_types;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function get_groups( $args ) {

            $groups = array();

            foreach ( $this->load_filter_types() as $filter_type ) {

                if ( !method_exists( $filter_type, 'get_groups' ) ) {

                    continue;
                }

                $new_groups = $filter_type->get_groups( $args );

                if ( !$new_groups ) {

                    continue;
                }

                $groups = $this->merge_list( $groups, $new_groups );
            }

            return $groups;
        }

        public function get_product_filters( $group_id, $args ) {

            $filters = array();

            foreach ( $this->load_filter_types() as $filter_type ) {

                if ( !method_exists( $filter_type, 'get_filters' ) ) {

                    continue;
                }

                $new_filters = $filter_type->get_filters( $group_id, $args );

                if ( !$new_filters ) {

                    continue;
                }

                $filters = $this->merge_list( $filters, $new_filters );
            }

            return $filters;
        }

        public function get_product_filter_fields( $filter_id, $args ) {

            foreach ( $this->load_filter_types() as $filter_type ) {

                if ( !method_exists( $filter_type, 'get_fields' ) ) {

                    continue;
                }

                $fields = $filter_type->get_fields( $filter_id, $args );

                if ( is_array( $fields ) ) {

                    return $this->process_fields( $fields );
                }
            }

            return $this->process_fields( array() );
        }

        private function process_fields( $new_fields ) {

            $fields = array(
                array(
                    'id' => 'is_req',
                    'type' => 'select2',
                    'default' => '>=',
                    'options' => array(
                        'yes' => esc_html__( 'Required', 'pricely-dynamic-pricing-cart-discounts' ),
                        'no' => esc_html__( 'Optional', 'pricely-dynamic-pricing-cart-discounts' ),
                    ),
                    'width' => '98%',
                    'box_width' => '19%',
                ),
            );

            foreach ( $new_fields as $new_field ) {

                $fields[] = $new_field;
            }

            return $fields;
        }

        private function merge_list( $prev_list, $new_list ) {

            foreach ( $new_list as $key => $value ) {

                $prev_list[ $key ] = $value;
            }

            return $prev_list;
        }

        private function load_filter_types() {

            if ( !is_null( $this->filter_types ) ) {

                return $this->filter_types;
            }

            $this->filter_types = array();

            foreach ( $this->register_filter_types() as $key => $filter_type_class ) {

                if ( !class_exists( $filter_type_class ) ) {

                    continue;
                }

                if ( method_exists( $filter_type_class, 'get_instance' ) ) {

                    $this->filter_types[ $key ] = $filter_type_class::get_instance();
                } else {

                    $this->filter_types[ $key ] = new $filter_type_class();
                }
            }

            return $this->filter_types;
        }

        private function register_filter_types() {
            
            if ( class_exists( 'Pricely_Pro_Admin_Product_Filter_Types' ) ) {

                $cond_types_obj = Pricely_Pro_Admin_Product_Filter_Types::get_instance();

                return $cond_types_obj->register_filter_types();
            }

            $filter_type_classes = array(
                'product' => 'Pricely_Admin_Product_Filter_Type_Product'
            );

            return apply_filters( 'pricely_admin_register-product-filter-types', $filter_type_classes );
        }

    }

}
