<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Condition_Type_Cart_Total' ) ) {

    class Pricely_Condition_Type_Cart_Total {

        private $options;
        private $validation_util;
        private $calc_prefix;

        public function __construct() {

            $this->calc_prefix = 'cart_totals_';

            $this->validation_util = Pricely_Validation_Util::get_instance();
        }

        public function can_validate( $condition_type, $args ) {

            $condition_types = array_keys( $this->get_cart_total_types( $args[ 'module' ] ) );

            return in_array( $condition_type, $condition_types );
        }

        public function validate( $condition, $data, $items ) {

            $based_on = $this->get_based_on( $condition );

            if ( false === $based_on ) {

                return false;
            }

            return $this->validate_totals( $condition, $based_on, $data, $items );
        }

        private function validate_totals( $condition, $based_on, $data, $items ) {

            $totals = $this->get_totals( $based_on, $data, $items );

            $rule_compare = $condition[ 'compare' ];

            if ( '--' == $rule_compare ) {

                $rule_from_totals = 0;

                if ( is_numeric( $condition[ 'from_totals' ] ) ) {

                    $rule_from_totals = $condition[ 'from_totals' ];
                }

                $rule_to_totals = 0;

                if ( is_numeric( $condition[ 'to_totals' ] ) ) {

                    $rule_to_totals = $condition[ 'to_totals' ];
                }

                $from_is_valid = $this->validation_util->validate_value( '>=', $totals, $rule_from_totals );

                $to_is_valid = $this->validation_util->validate_value( '<=', $totals, $rule_to_totals );

                return (true == $from_is_valid && true == $to_is_valid);
            }

            $rule_totals = 0;

            if ( is_numeric( $condition[ 'totals' ] ) ) {

                $rule_totals = $condition[ 'totals' ];
            }

            return $this->validation_util->validate_value( $rule_compare, $totals, $rule_totals );
        }

        private function get_totals( $based_on, $data, $items ) {

            if ( !isset( $data[ 'wc' ][ 'context' ] ) ) {

                return 0;
            }

            $context = $data[ 'wc' ][ 'context' ];

            return Pricely_Cart_Totals_Util::get_instance()->get_total( $based_on, $context, $data, $items );
        }

        private function get_based_on( $condition ) {

            if ( !isset( $condition[ 'module_args' ][ 'module' ] ) ) {

                return false;
            }

            $module_id = $condition[ 'module_args' ][ 'module' ];

            $cart_total_types = $this->get_cart_total_types( $module_id );

            $condition_type = $condition[ 'condition_type' ];

            if ( !isset( $cart_total_types[ $condition_type ] ) ) {

                return false;
            }

            return $cart_total_types[ $condition_type ];
        }

        private function get_cart_total_types( $module_id ) {

            $cart_total_types = array();

            foreach ( $this->get_options() as $option ) {

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

                    continue;
                }

                $total_type_id = $this->calc_prefix . $option[ 'option_id' ];

                $cart_total_types[ $total_type_id ] = $option[ 'option_id' ];
            }

            return $cart_total_types;
        }

        private function get_options() {

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

                return $this->options;
            }

            $this->options = Pricely::get_instance()->get_options( 'cart_totals_calcs', $this->get_default_options() );

            return $this->options;
        }

        private function get_default_options() {

            return array(
                array(
                    'calc_for' => 'any',
                    'option_id' => '2234343',
                ),
                array(
                    'calc_for' => 'any',
                    'option_id' => '2234344',
                ),
            );
        }

    }

}
