<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Amount_Type_Order_Items' ) ) {

    class Pricely_Amount_Type_Order_Items {

        private $totals_util;

        public function __construct() {

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

        public function can_calculate( $amount_type ) {

            $amount_types = array(
                'per_amount_item',
            );

            return in_array( $amount_type, $amount_types );
        }

        public function calculate_amount( $amount_data, $data, $amount_args ) {

            return $this->calculate_per_amount( $amount_data, $amount_args );
        }

        private function calculate_per_amount( $amount_data, $amount_args ) {

            $amount = 0;

            if ( is_numeric( $amount_args[ 'amount' ] ) ) {

                $amount = $amount_args[ 'amount' ];
            }

            if ( $amount <= 0 ) {

                return $amount_data;
            }

            $items_data = $this->get_items_data( $amount_args );

            if ( !$items_data ) {

                return $amount_data;
            }



            $items_total = 0;

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

            foreach ( $items_data as $key => $item_data ) {

                $items_total += $item_data[ 'total' ];

                $item_arg = array(
                    'qty' => $item_data[ 'qty' ],
                    'total' => $item_data[ 'total' ]
                );

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

            $total = $this->round( ($amount / 100) * $items_total );

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

            if ( !$split_totals ) {

                return $amount_data;
            }

            foreach ( $split_totals as $key => $split_total ) {

                if ( !isset( $amount_data[ 'totals' ][ $key ] ) ) {

                    $amount_data[ 'totals' ][ $key ][ 'total' ] = 0;
                }

                $amount_data[ 'totals' ][ $key ][ 'total' ] += array_sum( $split_total[ 'totals' ] );
            }

            return $amount_data;
        }

        private function get_items_data( $amount_args ) {

            $amount_types = Pricely_Amount_Types::get_instance();

            $args = array(
                'context' => $amount_args[ 'context' ]
            );

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

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

            $items = $amount_types->get_items( $args );

            if ( !is_array( $items ) ) {

                return false;
            }

            $based_on = $amount_args[ 'item_based_on' ];

            $items_data = array();

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

                $price = $this->get_item_price( $item, $based_on );

                $item_qty = 1;

                if ( isset( $item[ 'items_in_cart' ][ 'quantity' ] ) ) {

                    $item_qty = $item[ 'items_in_cart' ][ 'quantity' ];
                }

                $item_total = $price * $item_qty;

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

                $items_data[ $item_key ] = $item_data;
            }

            if ( count( $items_data ) > 0 ) {

                return $items_data;
            }

            return false;
        }

        private function get_item_price( $item, $based_on ) {

            $prices = $this->get_prices( $item );

            $price = $prices[ 'regular_price' ];

            if ( 'sale_price' == $based_on ) {

                $price = $prices[ 'sale_price' ];
            }

            if ( !is_numeric( $price ) ) {

                $price = $prices[ 'regular_price' ];
            }

            if ( !is_numeric( $price ) ) {

                return 1;
            }

            return $price;
        }

        private function get_prices( $item ) {

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

                return $item[ 'source_prices' ];
            }

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

                return $item[ 'source_prices' ];
            }


            $prices = array(
                'regular_price' => '',
                'sale_price' => '',
            );

            if ( $item[ 'wc' ][ 'product' ][ 'regular_price' ] ) {

                $prices[ 'regular_price' ] = $item[ 'wc' ][ 'product' ][ 'regular_price' ];
            }

            if ( $item[ 'wc' ][ 'product' ][ 'sale_price' ] ) {

                $prices[ 'sale_price' ] = $item[ 'wc' ][ 'product' ][ 'sale_price' ];
            }

            return $prices;
        }

        private function round( $total ) {

            return $this->totals_util->round( $total );
        }

    }

}