<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Checkout_Prices' ) ) {

    class Pricely_Checkout_Prices {

        private $data_key;
        private $checkout_prices_engine;
        private $checkout_prices_data;
        private $items_list;
        private $prices_list;

        public function __construct() {

            $this->data_key = 'calculated_prices';
            $this->items_list = array();
            $this->prices_list = false;
        }

        private function init() {

            if ( $this->checkout_prices_engine ) {

                return;
            }

            $this->checkout_prices_engine = new Pricely_Checkout_Prices_Engine();
            $this->checkout_prices_data = new Pricely_Checkout_Prices_Data();
        }

        public function clear_cache() {

            //initialise all
            $this->init();
            $this->items_list = array();
            $this->prices_list = false;

            $this->checkout_prices_engine->clear_cache();
        }

        public function get_cache( $data, $context ) {

            //initialise all
            $this->init();
            $this->prices_list = false;

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

            if ( $cache_prices ) {

                $data[ $this->data_key ] = $cache_prices;

                $this->prices_list = $cache_prices;
            }

            return $data;
        }

        public function get_product_cache( $data ) {

            //initialise all
            $this->init();

            //check whether prices was previously calculated 
            if ( !$this->prices_list ) {

                return $data;
            }

            if ( !$this->prices_list[ 'items' ] ) {

                return $data;
            }


            //get prices key
            $key = '';

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

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

            if ( empty( $key ) ) {

                return $data;
            }

            //set the prices
            $items = $this->prices_list[ 'items' ];

            if ( isset( $items[ $key ] ) ) {

                $data[ $this->data_key ] = $items[ $key ];
            }

            return $data;
        }

        public function process_product_data( $data ) {

            //initialise all
            $this->init();
            $this->set_items_list( $data );

            //check whether prices were previously calculated 
            if ( !$this->prices_list ) {

                return $data;
            }

            if ( !$this->prices_list[ 'items' ] ) {

                return $data;
            }


            //get prices key
            $key = '';

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

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

            if ( empty( $key ) ) {

                return $data;
            }

            //set the prices
            $items = $this->prices_list[ 'items' ];

            if ( isset( $items[ $key ] ) ) {

                $data[ $this->data_key ] = $items[ $key ];
            }

            return $data;
        }

        public function process_data( $data, $context ) {

            //initialise all
            $this->init();
            $this->prices_list = false;

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

            if ( $cache_prices ) {

                $data[ $this->data_key ] = $cache_prices;

                $this->prices_list = $cache_prices;

                return $data;
            }


            //process data from engine
            $prices_data = $this->checkout_prices_engine->get_data( $data, $context, $this->items_list );

            if ( !is_array( $prices_data ) ) {

                $this->checkout_prices_engine->store_cache( false );

                return $data;
            }

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

            if ( $prices ) {

                $data[ $this->data_key ] = $prices;

                $this->prices_list = $prices;

                $this->checkout_prices_engine->store_cache( $prices );
            }

            return $data;
        }

        private function set_items_list( $data ) {

            if ( !isset( $data[ 'items_in_cart' ][ 'quantity' ] ) ) {

                return;
            }

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

                return;
            }

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

            if ( isset( $this->items_list[ $item_key ] ) ) {

                return;
            }

            $this->items_list[ $item_key ] = $data;
        }

    }

}

