<?php

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

if ( !class_exists( 'Pricely_Product_Promotions' ) ) {

    class Pricely_Product_Promotions {

        private $data_key;
        private $product_promotions_engine;
        private $product_promotions_data;
        private $cart_data;
        private $promotions_list;

        public function __construct() {
            $this->data_key = 'promotions';
            $this->cart_data = array();
            $this->promotions_list = array();
        }

        private function init() {

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

                return;
            }

            $this->product_promotions_engine = new Pricely_Product_Promotions_Engine();
            $this->product_promotions_data = new Pricely_Product_Promotions_Data();
        }

        public function clear_cache() {

            $this->init();
            $this->promotions_list = array();
            $this->product_promotions_engine->clear_cache();
        }

        public function get_cache( $data, $context ) {

            $this->init();

            $this->cart_data = $data;

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

                return $data;
            }

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

            return $data;
        }

        public function get_product_cache( $data ) {

            $this->init();

            $context = $this->get_data_context( $data );

            //get cache promotions from engine
            $cache_promotions = $this->product_promotions_engine->get_cache( $data, $context );

            if ( $cache_promotions ) {

                $data[ $this->data_key ] = $cache_promotions;
                $this->add_to_promotions_list( $cache_promotions, $data );
            }

            return $data;
        }

        public function process_product_data( $data ) {

            $this->init();
            
            $context = $this->get_data_context( $data );

            //get cache promotions from engine
            $cache_promotions = $this->product_promotions_engine->get_cache( $data, $context );

            if ( $cache_promotions ) {

                $data[ $this->data_key ] = $cache_promotions;
                $this->add_to_promotions_list( $cache_promotions, $data );

                return $data;
            }

            //process data from engine
            $promotions_data = $this->product_promotions_engine->get_data( $data, $this->cart_data, $context );

            if ( !$promotions_data ) {

                return $data;
            }

            //process data as promotions and store as cache
            $promotions = $this->product_promotions_data->process_data( $promotions_data );

            if ( $promotions ) {

                $this->product_promotions_engine->store_cache( $promotions, $data );
                $data[ $this->data_key ] = $promotions;
                $this->add_to_promotions_list( $promotions, $data );
            }

            return $data;
        }

        public function process_data( $data, $context ) {

            $this->init();

            $this->cart_data = $data;

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

                return $data;
            }

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

            return $data;
        }

        private function add_to_promotions_list( $promotions, $data ) {

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

                return;
            }

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

            $this->promotions_list[ $key ] = $promotions;
        }

        private function get_promotion_list() {

            return $this->promotions_list;
        }

        private function get_data_context( $data ) {

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

                return '';
            }

            return $data[ 'wc' ][ 'context' ];
        }

    }

}

