<?php

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

if ( !class_exists( 'Pricely_Admin_Condition_Types' ) ) {

    require_once (dirname( __FILE__ ) . '/condition-types-calendar.php');
    require_once (dirname( __FILE__ ) . '/condition-types-cart.php');
    require_once (dirname( __FILE__ ) . '/condition-types-cart-total.php');
    require_once (dirname( __FILE__ ) . '/condition-types-customer.php');

    class Pricely_Admin_Condition_Types {

        private static $instance;
        private $condition_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_contidion_types() as $contidion_type ) {

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

                    continue;
                }

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

                if ( !$new_groups ) {

                    continue;
                }

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

            return $groups;
        }

        public function get_conditions( $group_id, $args ) {

            $conditions = array();

            foreach ( $this->load_contidion_types() as $contidion_type ) {

                if ( !method_exists( $contidion_type, 'get_conditions' ) ) {

                    continue;
                }

                $new_conditions = $contidion_type->get_conditions( $group_id, $args );

                if ( !$new_conditions ) {

                    continue;
                }

                $conditions = $this->merge_list( $conditions, $new_conditions );
            }

            return $conditions;
        }

        public function get_condition_fields( $condition_id, $args ) {

            foreach ( $this->load_contidion_types() as $contidion_type ) {

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

                    continue;
                }

                $fields = $contidion_type->get_fields( $condition_id, $args );

                if ( is_array( $fields ) ) {

                    return $fields;
                }
            }

            return array();
        }

        private function merge_list( $prev_list, $new_list ) {

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

                $prev_list[ $key ] = $value;
            }

            return $prev_list;
        }

        private function load_contidion_types() {

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

                return $this->condition_types;
            }

            $this->condition_types = array();

            foreach ( $this->register_contidion_types() as $key => $contidion_type_class ) {

                if ( !class_exists( $contidion_type_class ) ) {

                    continue;
                }

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

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

                    $this->condition_types[ $key ] = new $contidion_type_class();
                }
            }

            return $this->condition_types;
        }

        private function register_contidion_types() {
            
            if ( class_exists( 'Pricely_Pro_Admin_Condition_Types' ) ) {

                $cond_types_obj = Pricely_Pro_Admin_Condition_Types::get_instance();

                return $cond_types_obj->register_contidion_types();
            }

            $condition_type_classes = array(
                'calendar' => 'Pricely_Admin_Condition_Type_Calendar',
                'cart' => 'Pricely_Admin_Condition_Type_Cart',
                'cart_total' => 'Pricely_Admin_Condition_Type_Cart_Total',
                'customer' => 'Pricely_Admin_Condition_Type_Customer',
            );

            return apply_filters( 'pricely_admin_register_condition_types', $condition_type_classes );
        }

    }

}

