<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Amount_Totals' ) ) {

    class Pricely_Amount_Totals {

        private static $instance;
        private $totals_util;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->totals_util = Pricely_Totals_Util::get_instance();
        }

        public function process_amount( $amount_data, $items ) {

            //process totals
            if ( $amount_data[ 'total' ] > 0 && !count( $amount_data[ 'totals' ] ) ) {

                $amount_data = $this->process_items( $amount_data, $items );
            }

            //process total
            if ( $amount_data[ 'total' ] == 0 && count( $amount_data[ 'totals' ] ) ) {

                $amount_data = $this->process_total( $amount_data );
            }

            return $amount_data;
        }

        private function process_items( $amount_data, $items ) {

            $total = $amount_data[ 'total' ];

            $args = array(
                'mode' => 'total',
                'items' => array()
            );

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

                $item_qty = $item[ 'items_in_cart' ][ 'quantity' ];
                $item_total = $this->get_item_price( $item );


                $item_arg = array(
                    'qty' => $item_qty,
                    'total' => $item_total * $item_qty
                );

                $args[ 'items' ][ $key ] = $item_arg;
            }

            $items_totals = $this->totals_util->split_total_for_items( $total, $args, false );

            if ( !$items_totals ) {

                return $amount_data;
            }

            $totals = array();

            foreach ( $items_totals as $key => $item_totals ) {

                if ( !isset( $item_totals[ 'totals' ] ) ) {

                    continue;
                }

                $totals[ $key ][ 'total' ] = array_sum( $item_totals[ 'totals' ] );
            }

            if ( !count( $totals ) ) {

                return $amount_data;
            }

            $amount_data[ 'totals' ] = $totals;

            return $amount_data;
        }

        private function process_total( $amount_data ) {

            $total = 0;

            foreach ( $amount_data[ 'totals' ] as $item ) {

                if ( !isset( $item[ 'total' ] ) ) {

                    continue;
                }

                if ( !is_numeric( $item[ 'total' ] ) ) {

                    continue;
                }

                $total += $item[ 'total' ];
            }

            $amount_data[ 'total' ] = $total;

            return $amount_data;
        }

        private function get_item_price( $item ) {

            $regular_price = false;

            if ( isset( $item[ 'source_prices' ][ 'regular_price' ] ) ) {

                $regular_price = $item[ 'source_prices' ][ 'regular_price' ];
            }

            if ( '' === $regular_price ) {

                $regular_price = false;
            }

            if ( false === $regular_price ) {

                return 1;
            }

            return $regular_price;
        }

    }

}
