<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Checkout_Prices_Engine' ) ) {

    class Pricely_Checkout_Prices_Engine {

        private $data_key;
        private $valid_options;
        private $cache;
        private $cart;
        private $condition_types;
        private $prices_modes;
        private $locale;
        private $locale_prefix;

        public function __construct() {

            $this->data_key = 'calculated_prices';
            $this->locale_prefix = 'checkout-prices-';
            $this->valid_options = array();

            $this->cache = Pricely_Checkout_Prices_Cache::get_instance();
            $this->cart = Pricely_Cart::get_instance();
            $this->condition_types = Pricely_Condition_Types::get_instance();
            $this->prices_modes = Pricely_Checkout_Prices_Modes::get_instance();
            $this->locale = Pricely_Locale::get_instance();
        }

        public function clear_cache() {

            $this->valid_options = array();

            $this->cache->clear();

            if ( class_exists( 'Pricely_Pro_Checkout_Prices_Engine' ) ) {

                Pricely_Pro_Checkout_Prices_Engine::get_instance()->clear_cache();
            }
        }

        public function get_data( $data, $context, $items ) {

            // can't process data if not in the context
            if ( !$this->is_valid_context( $context, false ) ) {

                return false;
            }

            //check whether there are items
            if ( !count( $items ) ) {

                return false;
            }

            //process prices
            return $this->calculate_prices( $data, $context, $items );
        }

        public function get_cache( $context ) {

            // can't process data if not in the context
            if ( !$this->is_valid_context( $context, true ) ) {

                return false;
            }

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

        public function store_cache( $prices ) {

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

        private function calculate_prices( $data, $context, $items ) {

            $settings = $this->get_settings();

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

                // return result
                return false;
            }

            // go through the prices options and apply them
            foreach ( $this->get_valid_price_options( $data, $items, $context ) as $price_option ) {

                $option_args = array(
                    'apply_mode' => $price_option[ 'apply_mode' ],
                    'pricing_id' => $price_option[ 'pricing_id' ],
                    'coupon' => $price_option[ 'coupon' ],
                    'settings' => $settings,
                );

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

                    continue;
                }

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

                //set the prices information
                $option_args[ 'tooltip' ] = $price_option[ 'tooltip' ];


                //allows other plugins to modify the calculated prices
                if ( has_filter( 'pricely_checkout-pricing_calculated-prices' ) ) {

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

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

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

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

            return false;
        }

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

            if ( class_exists( 'Pricely_Pro_Checkout_Prices_Engine' ) ) {

                return Pricely_Pro_Checkout_Prices_Engine::get_instance()->get_prices( $items, $data, $price_option, $settings );
            }

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

            return $this->prices_modes->calculate_prices( $items, $prices_args );
        }

        private function get_args_from_options( $price_option ) {

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

            $prices_args = array();

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

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

                    continue;
                }

                $prices_args[ $key ] = $price_option_arg;
            }

            return $prices_args;
        }

        private function can_apply_option( $option_args, $data ) {

            $bool_val = true;

            if ( class_exists( 'Pricely_Pro_Checkout_Prices_Engine' ) ) {

                $bool_val = Pricely_Pro_Checkout_Prices_Engine::get_instance()->can_apply_option( $option_args, $data );
            }

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

                $bool_val = apply_filters( 'pricely_checkout-pricing_can-apply-option', $bool_val, $option_args, $data );
            }

            return $bool_val;
        }

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

            if ( class_exists( 'Pricely_Pro_Checkout_Prices_Engine' ) ) {

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

                // set tooltip
                $prices[ 'tooltip' ] = $this->get_translated_tooltip( $option_args[ 'tooltip' ], $option_args[ 'pricing_id' ] );

                // set coupon data
                $prices[ 'coupon' ] = false;

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

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

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

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

            return $data;
        }

        private function get_valid_price_options( $data, $items, $context ) {

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

                return $this->valid_options;
            }

            // get data to validate
            $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' ] ) ) {

                    $price_option[ 'context' ] = $context;
                    $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' ] = 'checkout-pricing';
                $option_conditions[ 'context' ] = $context;


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

                if ( !$bool_val ) {

                    continue;
                }


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

            return $this->valid_options;
        }

        private function get_translated_tooltip( $tooltip, $pricing_id ) {

            if ( empty( $tooltip ) ) {

                return $tooltip;
            }

            return $this->get_translated_text( $tooltip, $pricing_id, 'tooltip' );
        }

        private function get_translated_text( $text, $pricing_id, $text_key ) {

            $translation_id = $this->locale_prefix . $pricing_id . '-' . $text_key;

            return $this->locale->translate_string( $text, $translation_id );
        }

        private function get_prices_options() {

            $rules = array();

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

                $rules[] = $rule;
            }

            return $rules;
        }

        private function get_settings() {

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

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

        private function get_options( $option_key, $default ) {

            $pricely = Pricely::get_instance();

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

        private function is_valid_context( $context, $is_cache ) {

            if ( 'cart_prices' == $context ) {

                return true;
            }

            if ( false === $is_cache ) {

                return false;
            }

            $cache_context_list = array(
                'cart_discounts',
                'promotions',
                'cart_fees',
                'cart_messages',
            );

            return in_array( $context, $cache_context_list );
        }

    }

}
