<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Cart_Totals_Util' ) ) {

    require_once (dirname( __FILE__ ) . '/cart-total-items-total-util.php');

    class Pricely_Cart_Totals_Util {

        private static $instance;
        private $settings_list;
        private $items_total_util;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->items_total_util = new Pricely_Cart_Totals_Items_Total_Util();
        }

        public function get_total( $based_on, $context, $data, $items ) {

            $calc_args = $this->get_calc_args( $based_on, $items );

            if ( !$calc_args ) {

                return 0;
            }

            $total = $this->calculate_items_total( 0, $calc_args, $data );

            if ( !is_numeric( $total ) ) {

                $total = 0;
            }

             return max( 0, apply_filters( 'pricely_get-cart-total', $total, $based_on, $context, $data, $items ) );
        }

        private function calculate_items_total( $total, $calc_args, $data ) {

            $cart_total = $this->items_total_util->calculate_total( $total, $calc_args, $data );

            if ( !is_numeric( $cart_total ) ) {

                $cart_total = 0;
            }

            return $cart_total;
        }

        private function get_calc_args( $based_on, $items ) {

            $settings = $this->get_settings( $based_on );

            if ( !$settings ) {

                return false;
            }

            $calc_args = array(
                'id' => $settings[ 'option_id' ],
            );

            $calc_args[ 'item_keys' ] = array_keys( $items );

            foreach ( $settings as $key => $value ) {


                if ( 'option_id' == $key ) {

                    continue;
                }

                $calc_args[ $key ] = $value;
            }

            return $calc_args;
        }

        private function get_settings( $based_on ) {

            $settings_list = $this->get_settings_list();

            if ( isset( $settings_list[ $based_on ] ) ) {

                return $settings_list[ $based_on ];
            }

            return false;
        }

        private function get_settings_list() {

            if ( !is_null( $this->settings_list ) ) {

                return $this->settings_list;
            }

            $pricely = Pricely::get_instance();

            $options = $pricely->get_options( 'cart_totals_calcs', $this->get_default_settings() );

            $list = array();

            foreach ( $options as $option ) {

                if ( !isset( $option[ 'option_id' ] ) ) {

                    continue;
                }

                $option_id = $option[ 'option_id' ];

                $list[ $option_id ] = $option;
            }

            $this->settings_list = $list;

            return $this->settings_list;
        }

        private function get_default_settings() {

            return array(
                array(
                    'option_id' => '2234343',
                    'calc_for' => 'cart-discounts',
                    'items_total' => array(
                        'mode' => 'add_all',
                        'based_on' => 'sale_price',
                        'tax_total' => 'no'
                    ),
                ),
                array(
                    'option_id' => '2234344',
                    'calc_for' => 'cart-discounts',
                    'items_total' => array(
                        'mode' => 'add_all',
                        'based_on' => 'sale_price',
                        'tax_total' => 'yes'
                    ),
                ),
            );
        }

    }

}