<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_After_Checkout_Prices' ) ) {

    class Pricely_After_Checkout_Prices {

        private $items_in_cart_data_key;
        private $calculated_prices_data_key;

        public function __construct() {

            $this->items_in_cart_data_key = 'items_in_cart';
            $this->calculated_prices_data_key = 'calculated_prices';
        }

        public function get_cache( $data, $context ) {

            return $this->process_data( $data, $context );
        }

        public function get_product_cache( $data ) {

            return $this->process_product_data( $data );
        }

        public function process_data( $data, $context ) {

            if ( !isset( $data[ $this->items_in_cart_data_key ] ) ) {

                return $data;
            }

            if ( !isset( $data[ $this->calculated_prices_data_key ] ) ) {

                return $data;
            }

            $items_from_data = $this->get_items_from_data( $data[ $this->calculated_prices_data_key ] );

            if ( !$items_from_data ) {

                return $data;
            }

            foreach ( $items_from_data as $item_key => $item_data ) {

                if ( !isset( $data[ $this->items_in_cart_data_key ][ $item_key ] ) ) {

                    continue;
                }

                $prev_price = $data[ $this->items_in_cart_data_key ][ $item_key ][ 'price' ];

                $calculated_price = $this->get_calculated_price( $item_data, $prev_price );

                if ( false === $calculated_price ) {

                    continue;
                }

                $data[ $this->items_in_cart_data_key ][ $item_key ][ 'price' ] = $calculated_price;
            }

            return $data;
        }

        public function process_product_data( $data ) {

            if ( !isset( $data[ $this->items_in_cart_data_key ] ) ) {

                return $data;
            }

            if ( !isset( $data[ $this->calculated_prices_data_key ] ) ) {

                return $data;
            }

            $prev_price = $data[ $this->items_in_cart_data_key ][ 'price' ];

            $item_data = $data[ $this->calculated_prices_data_key ];

            $calculated_price = $this->get_calculated_price( $item_data, $prev_price );

            if ( false === $calculated_price ) {

                return $data;
            }

            $data[ $this->items_in_cart_data_key ][ 'price' ] = $calculated_price;

            return $data;
        }

        private function get_items_from_data( $data ) {

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

                return false;
            }

            if ( !$data[ 'items' ] ) {

                return false;
            }

            return $data[ 'items' ];
        }

        private function get_calculated_price( $item_data, $prev_price ) {

            $item_qty = $item_data[ 'item_qty' ];
            $qty = $item_data[ 'qty' ];

            $original_total = 0;

            if ( $item_qty > $qty ) {

                $original_total = ($item_qty - $qty) * $prev_price;
            }

            $total = array_sum( $item_data[ 'prices' ] );

            $final_price = ($total + $original_total) / $item_qty;

            return $final_price;
        }

    }

}

