<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Checkout_Prices_Modes_Bulk' ) ) {

    class Pricely_Checkout_Prices_Modes_Bulk {

        private static $instance;
        private $quantity_util;
        private $prices_types;

        public static function get_instance(): self {

            if ( !self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function __construct() {

            $this->quantity_util = Pricely_Quantity_Util::get_instance();
            $this->prices_types = Pricely_Checkout_Prices_Types::get_instance();
        }

        public function get_range_items( $args ) {

            $range_items = array();

            if ( !is_array( $args[ 'items' ] ) ) {

                return $range_items;
            }

            $ranges = $this->quantity_util->process_ranges( $this->get_range_args( $args[ 'ranges' ] ), false );

            $item_args = $this->get_item_args( $args[ 'items' ] );

            $bulk_items = $this->quantity_util->get_bulk_items( $args[ 'qty' ], $item_args, $ranges );

            foreach ( $bulk_items as $item_key => $bulk_item ) {

                if ( !isset( $args[ 'items' ][ $item_key ] ) ) {

                    continue;
                }

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

                    $range_items[ 'pricing_args' ] = $this->get_pricing_args( $bulk_item[ 'range_id' ], $args[ 'ranges' ], $args[ 'pricing_id' ] );
                }

                $item = $args[ 'items' ][ $item_key ];

                $qty = $bulk_item[ 'qty' ];

                $range_items[ 'items' ][ $item_key ] = $this->prepare_item_quantity( $item, $qty );
            }

            return $range_items;
        }

        private function get_pricing_args( $range_id, $ranges, $pricing_id ) {

            if ( !isset( $ranges[ $range_id ] ) ) {

                return false;
            }

            $range = $ranges[ $range_id ];

            if ( !$range ) {

                return false;
            }

            $pricing_args = array(
                'pricing_id' => $pricing_id,
            );

            foreach ( $range as $arg_key => $arg_value ) {

                if ( 'min' == $arg_key || 'max' == $arg_key ) {

                    continue;
                }

                $pricing_args[ 'args' ][ $arg_key ] = $arg_value;
            }

            return $pricing_args;
        }

        private function get_range_args( $ranges ) {

            $range_args = array();

            foreach ( $ranges as $range_id => $range ) {

                $range_arg = array(
                    'id' => $range_id
                );

                $min = 1;

                if ( is_numeric( $range[ 'min' ] ) ) {

                    $min = $range[ 'min' ];
                }

                $range_arg[ 'min' ] = $min;

                if ( is_numeric( $range[ 'max' ] ) ) {

                    $range_arg[ 'max' ] = $range[ 'max' ];
                }

                $range_args[] = $range_arg;
            }

            return $range_args;
        }

        private function get_item_args( $items ) {

            $item_args = array();

            foreach ( $items as $item_key => $item ) {

                $item_args[ $item_key ] = $item[ 'items_in_cart' ][ 'quantity' ];
            }

            return $item_args;
        }

        private function prepare_item_quantity( $item, $qty ) {

            $item[ 'pricing_quantity' ] = $qty;

            return $item;
        }

    }

}

