<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Product_Prices_Engine' ) ) {

    class Pricely_Product_Prices_Engine {

        private $data_key;
        private $valid_options;
        private $cache;
        private $cart;
        private $condition_types;
        private $product_filter_types;
        private $product_prices_types;

        public function __construct() {

            $this->data_key = 'adjusted_prices';
            $this->valid_options = array();

            $this->cache = Pricely_Product_Prices_Cache::get_instance();
            $this->cart = Pricely_Cart::get_instance();
            $this->condition_types = Pricely_Condition_Types::get_instance();
            $this->product_filter_types = Pricely_Product_Filter_Types::get_instance();
            $this->product_prices_types = Pricely_Product_Prices_Types::get_instance();
        }

        public function clear_cache() {

            $this->valid_options = array();

            $this->cache->clear();
        }

        public function get_data( $data, $cart_data ) {

            $cache_key = $this->get_cache_key( $data );

            //can't process empty product
            if ( empty( $cache_key ) ) {

                return false;
            }

            //process prices
            $prices = $this->calculate_prices( $data, $cart_data );

            return $prices;
        }

        public function get_cache( $data ) {

            //check for valid variation id
            if ( !$this->is_valid_variation_id( $data ) ) {

                return false;
            }

            $cache_key = $this->get_cache_key( $data );

            //can't process empty cache
            if ( empty( $cache_key ) ) {

                return false;
            }

            //get data from cache
            return $this->cache->get( $cache_key );
        }

        public function store_cache( $prices, $data ) {

            $cache_key = $this->get_cache_key( $data );

            //can't store empty cache
            if ( empty( $cache_key ) ) {

                return;
            }

            //store prices into cache for re-use
            $this->cache->set( $prices, $cache_key );
        }

        private function calculate_prices( $in_data, $cart_data ) {

            //check for valid variation id
            if ( !$this->is_valid_variation_id( $in_data ) ) {

                return false;
            }

            $settings = $this->get_settings();

            //check if prices should apply
            if ( 'no' == $settings[ 'mode' ] ) {

                // return result
                return false;
            }

            $data = $this->set_prev_prices( $in_data );

            // go through the prices options and apply them
            foreach ( $this->get_valid_prices_options( $cart_data ) as $price_option ) {

                $option_head = array(
                    'apply_mode' => $price_option[ 'apply_mode' ],
                    'pricing_id' => $price_option[ 'pricing_id' ],
                    'settings' => $settings,
                    'module' => 'product-pricing',
                );

                $product_args = array();

                if ( isset( $price_option[ 'product_args' ] ) ) {

                    $product_args = $price_option[ 'product_args' ];
                }

                if ( !$this->can_apply_option( $option_head, $product_args, $data ) ) {

                    continue;
                }

                $prices = $this->get_prices( $price_option, $settings, $data );

                if ( has_filter( 'pricely_product-pricing_calculated-prices' ) ) {

                    $prices = apply_filters( 'pricely_product-pricing_calculated-prices', $prices, $price_option[ 'pricing_id' ], $data );
                }

                $data = $this->add_option_prices( $this->set_prev_prices( $data, $prices ), $prices, $option_head );
            }

            // return result
            if ( isset( $data[ $this->data_key ] ) ) {

                return $data[ $this->data_key ];
            }

            return false;
        }

        private function get_prices( $price_option, $settings, $data ) {

            $prices = array();

            if ( class_exists( 'Pricely_Pro_Product_Prices_Engine' ) ) {

                $prices = Pricely_Pro_Product_Prices_Engine::get_instance()->get_prices( $price_option, $settings, $data );
            } else {

                $prices_args = $this->get_args_from_options( $price_option );

                $prices = $this->product_prices_types->get_prices( $prices_args, $data );
            }

            return $prices;
        }

        private function get_args_from_options( $price_option ) {

            $ex_keys = array(
                'admin_note',
                'limit',
                'product_args',
                'condition_args',
                'apply_mode',
                'enable'
            );

            $price_args = array();

            foreach ( $price_option as $key => $price_option_arg ) {

                if ( in_array( $key, $ex_keys ) ) {

                    continue;
                }

                $price_args[ $key ] = $price_option_arg;
            }

            return $price_args;
        }

        private function can_apply_option( $option_head, $product_args, $data ) {

            //validate product

            $bool_val = $this->product_filter_types->validate_product( $data, $product_args );

            if ( !$bool_val ) {

                return false;
            }

            if ( class_exists( 'Pricely_Pro_Product_Prices_Engine' ) ) {

                $bool_val = Pricely_Pro_Product_Prices_Engine::get_instance()->can_apply_option( $option_head, $data );
            }

            // allows other plugins to check if the option should apply
            if ( has_filter( 'pricely_product-pricing_can-apply-option' ) ) {

                $bool_val = apply_filters( 'pricely_product-pricing_can-apply-option', $bool_val, $option_head, $data );
            }

            return $bool_val;
        }

        private function add_option_prices( $data, $prices, $option_head ) {

            if ( class_exists( 'Pricely_Pro_Product_Prices_Engine' ) ) {

                $data = Pricely_Pro_Product_Prices_Engine::get_instance()->add_option_prices( $data, $prices, $option_head );
            } else {

                $data[ $this->data_key ][ $option_head[ 'pricing_id' ] ] = $prices;
            }

            // allows other plugins to modify prices data once added
            if ( has_filter( 'pricely_product-pricing_added-prices-option' ) ) {

                if ( isset( $data[ $this->data_key ][ $option_head[ 'pricing_id' ] ] ) ) {

                    $data[ $this->data_key ][ $option_head[ 'pricing_id' ] ] = apply_filters( 'pricely_product-pricing_added-prices-option', $data[ $this->data_key ][ $option_head[ 'pricing_id' ] ], $option_head );
                }
            }

            return $data;
        }

        private function set_prev_prices( $data, $prices = array() ) {

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

                $reg_price = isset( $data[ 'wc' ][ 'product' ][ 'regular_price' ] ) ? $data[ 'wc' ][ 'product' ][ 'regular_price' ] : 0;
                $sale_price = isset( $data[ 'wc' ][ 'product' ][ 'sale_price' ] ) ? $data[ 'wc' ][ 'product' ][ 'sale_price' ] : 0;

                if ( !$sale_price ) {
                    $sale_price = $reg_price;
                }

                $data[ 'wc' ][ 'prev_prices' ] = array(
                    'regular_price' => is_numeric( $reg_price ) ? $reg_price : 0,
                    'sale_price' => is_numeric( $sale_price ) ? $sale_price : 0
                );
            }

            if ( isset( $prices[ 'regular_price' ] ) ) {

                $data[ 'wc' ][ 'prev_prices' ][ 'regular_price' ] = $prices[ 'regular_price' ];
            }

            if ( isset( $prices[ 'sale_price' ] ) ) {

                $data[ 'wc' ][ 'prev_prices' ][ 'sale_price' ] = $prices[ 'sale_price' ];
            }

            return $data;
        }

        private function is_valid_variation_id( $data ) {

            $product_type = '';

            $variation_id = 0;

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

                $product_type = $data[ 'wc' ][ 'product' ][ 'type' ];
            }

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

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

            if ( 'variable' == $product_type && $variation_id <= 0 ) {

                return false;
            }

            return true;
        }

        private function get_valid_prices_options( $data ) {

            // check for already validated options
            if ( count( $this->valid_options ) ) {

                return $this->valid_options;
            }

            // get data to validate

            $context = 'shop_prices';
            $data[ 'wc' ] = $this->cart->get_data( $context );

            // go through each options and validate them
            foreach ( $this->get_prices_options() as $price_option ) {

                // allows active rules only
                if ( 'no' == $price_option[ 'enable' ] ) {

                    continue;
                }

                // check for required validations
                if ( !isset( $price_option[ 'condition_args' ][ 'conditions' ] ) ) {

                    $this->valid_options[] = $price_option;

                    continue;
                }

                // get conditions to validate
                $option_conditions = $price_option[ 'condition_args' ];
                $option_conditions[ 'id' ] = $price_option[ 'pricing_id' ];
                $option_conditions[ 'module' ] = 'product-pricing';
                $option_conditions[ 'context' ] = $context;

                // validate option conditions
                $bool_val = $this->condition_types->validate( $option_conditions, $data );

                if ( !$bool_val ) {

                    continue;
                }

                $this->valid_options[] = $price_option;
            }

            return $this->valid_options;
        }

        private function get_cache_key( $data ) {

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

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

            return '';
        }

        private function get_prices_options() {

            $rules = array();

            foreach ( $this->get_options( 'product_pricings', array() ) as $rule ) {

                $rules[] = $rule;
            }

            return $rules;
        }

        private function get_settings() {

            $default = array(
                'mode' => 'no',
            );

            return $this->get_options( 'product_pricing_settings', $default );
        }

        private function get_options( $option_key, $default ) {

            $pricely = Pricely::get_instance();

            return $pricely->get_options( $option_key, $default );
        }

    }

}

