<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Data_List' ) ) {

    require_once (dirname( __FILE__ ) . '/admin-data-list-cart-discounts-totals.php');
    require_once (dirname( __FILE__ ) . '/admin-data-list-countdown-designs/countdown-designs.php');
    require_once (dirname( __FILE__ ) . '/admin-data-list-price-table-designs.php');
    require_once (dirname( __FILE__ ) . '/admin-data-list-label-designs.php');
    require_once (dirname( __FILE__ ) . '/admin-data-list-view-positions.php');

    class Pricely_Admin_Data_List {

        private static $instance;
        private $prefix;
        private $data_sources;

        public static function get_instance(): self {

            if ( is_null( self::$instance ) ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->prefix = 'pricely:';
        }

        public function can_get_data( $source ) {

            return $this->starts_with( $this->prefix, $source );
        }

        public function get_data( $result, $in_data_args ) {
            
            if ( class_exists( 'Pricely_Pro_Admin_Data_List' ) ) {

                return Pricely_Pro_Admin_Data_List::get_instance()->get_data( $result, $in_data_args );
            }

            $data_args = $this->process_args( $in_data_args );

            if ( !$data_args ) {

                return $result;
            }

            return $this->process_data( $result, $data_args, $data_args[ 'source' ] );
        }

        private function process_data( $result, $data_args, $source ) {

            foreach ( $this->load_data_sources() as $data_source ) {

                if ( !method_exists( $data_source, 'can_get_data' ) ) {

                    continue;
                }

                if ( !method_exists( $data_source, 'get_data' ) ) {

                    continue;
                }

                if ( !$data_source->can_get_data( $source ) ) {

                    continue;
                }

                $result = $data_source->get_data( $result, $data_args );
            }

            return $result;
        }

        private function process_args( $data_args ) {

            if ( !isset( $data_args[ 'source' ] ) ) {

                return false;
            }

            if ( !$this->starts_with( $this->prefix, $data_args[ 'source' ] ) ) {

                return false;
            }

            $new_source = str_replace( $this->prefix, '', $data_args[ 'source' ] );

            if ( empty( $new_source ) ) {

                return false;
            }

            $data_args[ 'source' ] = $new_source;

            return $data_args;
        }

        private function load_data_sources() {

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

                return $this->data_sources;
            }

            $this->data_sources = array();

            foreach ( $this->register_data_sources() as $key => $data_source ) {

                if ( !class_exists( $data_source ) ) {

                    continue;
                }

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

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

                    $this->data_sources[ $key ] = new $data_source();
                }
            }

            return $this->data_sources;
        }

        private function register_data_sources() {

            $data_sources = array(
                'cart_discounts_totals' => 'Pricely_Admin_Data_List_Cart_Discounts_Totals',
                'countdown_designs' => 'Pricely_Admin_Data_List_CountDown_Designs',
                'price_table_designs' => 'Pricely_Admin_Data_List_Price_Table_Designs',
                'label_designs' => 'Pricely_Admin_Data_List_Label_Designs',
                'view_positions' => 'Pricely_Admin_Data_List_View_Positions'
            );

            return apply_filters( 'pricely_admin_register-data-sources', $data_sources );
        }

        private function starts_with( $needle, $haystack ) {

            return str_starts_with( $haystack, $needle );
        }

    }

    Pricely_Admin_Data_List::get_instance();
}
