<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Checkout_Prices_Modes_Range' ) ) {

    require_once (dirname( __FILE__ ) . '/bulk.php');

    class Pricely_Checkout_Prices_Modes_Range {

        private $prices_types;
        private $prices_types_bulk;
        private $grouped_items;

        public function __construct() {
            
            $this->grouped_items = array();

            $this->prices_types = Pricely_Checkout_Prices_Types::get_instance();
            $this->prices_types_bulk = Pricely_Checkout_Prices_Modes_Bulk::get_instance();
        }

        public function can_calculate( $pricing_mode ) {

            if ( 'bulk' == $pricing_mode ) {

                return true;
            }

            return false;
        }

        public function calculate_prices( $prices, $prices_args ) {

            return $this->calculate_bulk_prices( $prices, $prices_args );
        }

        private function calculate_bulk_prices( $prices, $prices_args ) {

            $ranges = $this->get_ranges( $prices_args );

            $range_args = array(
                'pricing_id' => $prices_args[ 'pricing_id' ],
                'ranges' => $ranges,
            );

            foreach ( $this->get_items( $prices_args ) as $group ) {

                $range_args[ 'qty' ] = $group[ 'qty' ];
                $range_args[ 'items' ] = $group[ 'items' ];

                $range_items = $this->prices_types_bulk->get_range_items( $range_args );

                if ( !isset( $range_items[ 'pricing_args' ] ) || false == $range_items[ 'pricing_args' ] ) {

                    continue;
                }

                if ( !isset( $range_items[ 'items' ] ) ) {

                    continue;
                }

                $prices = $this->prices_types->calculate_prices( $prices, $range_items[ 'pricing_args' ], $range_items[ 'items' ] );
            }

            return $prices;
        }

        private function get_ranges( $prices_args ) {

            if ( !isset( $prices_args[ 'ranges' ] ) ) {

                return array();
            }

            if ( !is_array( $prices_args[ 'ranges' ] ) ) {

                return array();
            }

            return $prices_args[ 'ranges' ];
        }

        private function get_items( $prices_args ) {

            if ( isset( $this->grouped_items[ $prices_args[ 'pricing_id' ] ] ) ) {

                return $this->grouped_items[ $prices_args[ 'pricing_id' ] ];
            }

            $args = array(
                'context' => $prices_args[ 'context' ],
                'product_args' => array(),
                'group_by' => $prices_args[ 'group_by' ],
                'sort_by' => $prices_args[ 'sort_by' ],
                'exclude_keys' => false
            );

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

                $args[ 'product_args' ] = $prices_args[ 'product_args' ];
            }

            $this->grouped_items[ $prices_args[ 'pricing_id' ] ] = $this->prices_types->get_items( $args );

            return $this->grouped_items[ $prices_args[ 'pricing_id' ] ];
        }

    }

}

