<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Data_List_Cart_Discounts_Totals' ) ) {

    class Pricely_Admin_Data_List_Cart_Discounts_Totals {
        
        private $source_type;

        public function __construct() {

            $this->source_type = 'cart_discounts_totals';
        }

        public function can_get_data( $source ) {

            return $this->source_type == $source;
        }

        public function get_data( $result, $data_args ) {

            $options = $this->get_options();

            foreach ( $options as $option ) {

                if ( !$this->calc_for_this( $option ) ) {

                    continue;
                }

                $result[ $option[ 'option_id' ] ] = $this->get_title( $option );
            }

            return $result;
        }

        private function get_title( $option ) {

            if ( !empty( $option[ 'admin_note' ] ) ) {

                return $option[ 'admin_note' ];
            }

            return esc_html__( 'Cart Totals Calculator', 'pricely-dynamic-pricing-cart-discounts' );
        }

        private function calc_for_this( $option ) {

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

                return true;
            }

            if ( 'cart-discounts' == $option[ 'calc_for' ] ) {

                return true;
            }

            if ( 'any' == $option[ 'calc_for' ] ) {

                return true;
            }

            return false;
        }

        private function get_options() {

            global $pricely_settings;

            if ( isset( $pricely_settings[ 'cart_totals_calcs' ] ) ) {

                return $pricely_settings[ 'cart_totals_calcs' ];
            }

            return $this->get_default_options();
        }

        private function get_default_options() {

            return array(
                array(
                    'admin_note' => esc_html__( 'Subtotal Excluding Tax', 'pricely-dynamic-pricing-cart-discounts' ),
                    'option_id' => '2234343',
                ),
                array(
                    'admin_note' => esc_html__( 'Subtotal Including Tax', 'pricely-dynamic-pricing-cart-discounts' ),
                    'option_id' => '2234344',
                ),
            );
        }

    }

}

