<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}


if ( !class_exists( 'Pricely' ) ) {

    require_once dirname( __FILE__ ) . '/pricely-locale.php';
    require_once dirname( __FILE__ ) . '/pricely-currency.php';
    require_once dirname( __FILE__ ) . '/utils/pricely-util.php';
    require_once dirname( __FILE__ ) . '/pricely-session.php';
    require_once dirname( __FILE__ ) . '/pricely-cache-params.php';
    require_once dirname( __FILE__ ) . '/pricely-cache.php';
    require_once dirname( __FILE__ ) . '/pricely-product.php';
    require_once dirname( __FILE__ ) . '/pricely-cart.php';

    require_once dirname( __FILE__ ) . '/modules/items-in-cart/items-in-cart-engine.php';
    require_once dirname( __FILE__ ) . '/modules/items-in-cart/items-in-cart.php';

    require_once dirname( __FILE__ ) . '/modules/source-prices/source-prices.php';

    require_once dirname( __FILE__ ) . '/modules/product-prices/product-prices-cache.php';
    require_once dirname( __FILE__ ) . '/modules/product-prices/product-prices-engine.php';
    require_once dirname( __FILE__ ) . '/modules/product-prices/product-prices-data.php';
    require_once dirname( __FILE__ ) . '/modules/product-prices/product-prices.php';

    require_once dirname( __FILE__ ) . '/modules/after-product-prices/after-product-prices.php';

    require_once dirname( __FILE__ ) . '/modules/checkout-prices/checkout-prices-cache.php';
    require_once dirname( __FILE__ ) . '/modules/checkout-prices/checkout-prices-engine.php';
    require_once dirname( __FILE__ ) . '/modules/checkout-prices/checkout-prices-data.php';
    require_once dirname( __FILE__ ) . '/modules/checkout-prices/checkout-prices.php';

    require_once dirname( __FILE__ ) . '/modules/after-checkout-prices/after-checkout-prices.php';

    require_once dirname( __FILE__ ) . '/modules/cart-discounts/cart-discounts-cache.php';
    require_once dirname( __FILE__ ) . '/modules/cart-discounts/cart-discounts-engine.php';
    require_once dirname( __FILE__ ) . '/modules/cart-discounts/cart-discounts-data.php';
    require_once dirname( __FILE__ ) . '/modules/cart-discounts/cart-discounts.php';

    require_once dirname( __FILE__ ) . '/modules/product-promotions/product-promotions-cache.php';
    require_once dirname( __FILE__ ) . '/modules/product-promotions/product-promotions-engine.php';
    require_once dirname( __FILE__ ) . '/modules/product-promotions/product-promotions-data.php';
    require_once dirname( __FILE__ ) . '/modules/product-promotions/product-promotions.php';

    require_once dirname( __FILE__ ) . '/product-price-types/product-price-types.php';
    require_once dirname( __FILE__ ) . '/checkout-price-types/checkout-price-types.php';
    require_once dirname( __FILE__ ) . '/checkout-price-modes/checkout-price-modes.php';
    require_once dirname( __FILE__ ) . '/amount-types/amount-types.php';
    require_once dirname( __FILE__ ) . '/product-promotion-types/promotion-types.php';

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

    class Pricely {

        private static $instance;
        private $pricely_settings;
        private $modules;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->pricely_settings = false;
        }

        public function clear_cache() {

            foreach ( $this->load_modules() as $module ) {

                if ( !method_exists( $module, 'clear_cache' ) ) {

                    continue;
                }

                $module->clear_cache();
            }
        }

        public function get_product_cache( $product_id, $variation_id, $context ) {

            $data = array();

            $pricely_product = Pricely_Product::get_instance();

            $data[ 'wc' ][ 'context' ] = $context;
            $data[ 'wc' ][ 'product' ] = $pricely_product->get_cache_data( $product_id, $variation_id );

            foreach ( $this->load_modules() as $module ) {

                if ( !method_exists( $module, 'get_product_cache' ) ) {

                    continue;
                }

                $data = $module->get_product_cache( $data );
            }

            return $data;
        }

        public function get_cache( $context ) {

            // Get me something
            $data = array();

            foreach ( $this->load_modules() as $module ) {

                if ( !method_exists( $module, 'get_cache' ) ) {

                    continue;
                }

                $data = $module->get_cache( $data, $context );
            }

            return $data;
        }

        public function get_product_data( $product, $variation, $context ) {

            $data = array();

            $pricely_product = Pricely_Product::get_instance();

            // Get me something
            $data[ 'wc' ][ 'context' ] = $context;
            $data[ 'wc' ][ 'product' ] = $pricely_product->get_data( $product, $variation );


            if ( !count( $data[ 'wc' ][ 'product' ] ) ) {

                // That's it, i will take it from here
                return $this->clean_data( $data );
            }

            foreach ( $this->load_modules() as $module ) {

                if ( !method_exists( $module, 'process_product_data' ) ) {

                    continue;
                }

                $data = $module->process_product_data( $data );
            }


            // That's it, i will take it from here
            return $this->clean_data( $data );
        }

        public function get_data( $context ) {

            // Get me something
            $data[ 'wc' ] = array();

            foreach ( $this->load_modules() as $module ) {

                if ( !method_exists( $module, 'process_data' ) ) {

                    continue;
                }

                $data = $module->process_data( $data, $context );
            }

            // That's it, i will take it from here
            return $this->clean_data( $data );
        }

        public function get_options( $option_key, $default, $options = false ) {

            if ( false === $options ) {

                $options = $this->load_options( array() );
            }

            if ( isset( $options[ $option_key ] ) ) {

                return $options[ $option_key ];
            }

            return $default;
        }

        public function load_options( $default = array() ) {

            if ( false !== $this->pricely_settings ) {

                return $this->pricely_settings;
            }


            if ( !defined( 'PRICELY_OPTION_NAME' ) ) {

                return $default;
            }

            $this->pricely_settings = get_option( PRICELY_OPTION_NAME, $default );

            return $this->pricely_settings;
        }

        private function load_modules() {

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

                return $this->modules;
            }

            $module_classes = $this->register_module_classes();

            foreach ( $module_classes as $key => $module_class ) {

                if ( !class_exists( $module_class ) ) {

                    continue;
                }

                $this->modules[ $key ] = new $module_class();
            }

            return $this->modules;
        }

        private function register_module_classes() {

            $module_classes = array(
                'source_prices' => 'Pricely_Source_Prices',
                'items_in_cart' => 'Pricely_Items_In_Cart',
                'product_prices' => 'Pricely_Product_Prices',
                'after_product_prices' => 'Pricely_After_Product_Prices',
                'checkout_prices' => 'Pricely_Checkout_Prices',
                'after-checkout-prices' => 'Pricely_After_Checkout_Prices',
                'cart_discounts' => 'Pricely_Cart_Discounts',
                'product_promotions' => 'Pricely_Product_Promotions',
            );

            return apply_filters( 'pricely_register-module-classes', $module_classes );
        }

        private function clean_data( $data ) {

            //removes unwanted data
            if ( isset( $data[ 'wc' ] ) ) {

                unset( $data[ 'wc' ] );
            }

            return $data;
        }

    }

}

