<?php

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

if ( !class_exists( 'Pricely_Product_Prices' ) ) {

    class Pricely_Product_Prices {

        private $data_key;
        private $product_prices_engine;
        private $product_prices_data;
        private $cart_data;
        private $prices_list;

        public function __construct() {

            $this->data_key = 'adjusted_prices';
            $this->cart_data = array();
            $this->prices_list = array();
        }

        private function init() {

            if ( isset( $this->product_prices_engine ) ) {

                return;
            }

            $this->product_prices_engine = new Pricely_Product_Prices_Engine();
            $this->product_prices_data = new Pricely_Product_Prices_Data();
        }

        public function clear_cache() {

            $this->init();
            $this->prices_list = array();
            $this->product_prices_engine->clear_cache();
        }

        public function get_cache( $data, $context ) {

            $this->init();

            if ( 'shop_prices' == $context ) {

                $this->cart_data = $data;
            }

            if ( !count( $this->get_price_list() ) ) {

                return $data;
            }

            $data[ $this->data_key ] = $this->get_price_list();

            return $data;
        }

        public function get_product_cache( $data ) {

            $this->init();

            //get cache prices from engine
            $cache_prices = $this->product_prices_engine->get_cache( $data );

            if ( $cache_prices ) {

                $data[ $this->data_key ] = $cache_prices;
                $this->add_to_prices_list( $cache_prices, $data );
            }

            return $data;
        }

        public function process_product_data( $data ) {

            $this->init();

            //get cache prices from engine
            $cache_prices = $this->product_prices_engine->get_cache( $data );

            if ( $cache_prices ) {

                $data[ $this->data_key ] = $cache_prices;
                $this->add_to_prices_list( $cache_prices, $data );

                return $data;
            }

            //process data from engine
            $prices_data = $this->product_prices_engine->get_data( $data, $this->cart_data );

            if ( !$prices_data ) {

                return $data;
            }

            //process data as prices and store as cache
            $prices = $this->product_prices_data->process_data( $prices_data );

            if ( $prices ) {

                $this->product_prices_engine->store_cache( $prices, $data );
                $data[ $this->data_key ] = $prices;
                $this->add_to_prices_list( $prices, $data );
            }

            return $data;
        }

        public function process_data( $data, $context ) {

            $this->init();

            if ( 'shop_prices' == $context ) {

                $this->cart_data = $data;
            }

            if ( !count( $this->get_price_list() ) ) {

                return $data;
            }

            $data[ $this->data_key ] = $this->get_price_list();

            return $data;
        }

        private function add_to_prices_list( $prices, $data ) {

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

                return;
            }

            $key = $data[ 'wc' ][ 'product' ][ 'key' ];

            $this->prices_list[ $key ] = $prices;
        }

        private function get_price_list() {

            return $this->prices_list;
        }

    }

}
