<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_View_Data_Tables_Volumetric_Price' ) ) {

    class Pricely_View_Data_Tables_Volumetric_Price {

        private $view_data;

        public function get_table_row_title( $range_data, $text_formats ) {

            //set the most likely text params
            $min_qty = $range_data[ 'min' ];
            $max_qty = 0;
            $text_format = $text_formats[ 'from_quantity_text' ];

            if ( false !== $range_data[ 'max' ] ) {

                $max_qty = $range_data[ 'max' ];
                $text_format = $text_formats[ 'range_quantity_text' ];
            }

            //set default text params
            if ( empty( $text_format ) && $max_qty > 0 ) {

                $text_format = esc_html__( '{{from_quantity}} - {{to_quantity}}', 'pricely-dynamic-pricing-cart-discounts' );
            } elseif ( empty( $text_format ) ) {

                $text_format = esc_html__( '{{from_quantity}}+', 'pricely-dynamic-pricing-cart-discounts' );
            }


            //set text replacemtents
            $text_args = array(
                'from_quantity' => $min_qty,
            );

            if ( $max_qty > 0 ) {

                $text_args[ 'to_quantity' ] = $max_qty;
            }

            //process the text
            return $this->replace_text( $text_format, $text_args );
        }

        public function get_table_row_prices( $range_data, $product ) {

            if ( 'variable' == $product[ 'type' ] ) {

                return $this->get_table_row_variable_price( $range_data, $product );
            }

            return $this->get_table_row_simple_price( $range_data, $product );
        }

        public function get_table_row_discounts( $range_data, $product ) {

            if ( 'variable' == $product[ 'type' ] ) {

                return $this->get_table_row_variable_discount( $range_data, $product );
            }

            return $this->get_table_row_simple_discount( $range_data, $product );
        }

        private function get_table_row_simple_price( $range_data, $product ) {

            $product_id = $product[ 'product_id' ];

            $simple_price = $this->get_price_to_display( $range_data[ 'price' ], $product_id, 0 );

            return $this->process_single_price( array(), $product_id, $simple_price );
        }

        private function get_table_row_variable_price( $range_data, $product ) {

            $product_id = $product[ 'product_id' ];

            $prices = $this->get_variation_prices_to_display( $range_data[ 'prices' ], $product_id );

            $min_price = $this->get_min_price( $prices );
            $max_price = $this->get_max_price( $prices );

            if ( $min_price == $max_price ) {

                return $this->process_single_price( array(), $product_id, $min_price );
            }

            return $this->process_range_price( array(), $product_id, $min_price, $max_price );
        }

        private function get_table_row_simple_discount( $range_data, $product ) {

            $product_id = $product[ 'product_id' ];

            $regular_price = $this->get_regular_price( $product_id, 0 );

            $price = $this->get_price_to_display( $range_data[ 'price' ], $product_id, 0 );

            return $this->process_single_discount( array(), $regular_price, $price );
        }

        private function get_table_row_variable_discount( $range_data, $product ) {

            $product_id = $product[ 'product_id' ];

            $variation_ids = array_keys( $range_data[ 'prices' ] );

            $regular_prices = array();
            $prices = $this->get_variation_prices_to_display( $range_data[ 'prices' ], $product_id );

            foreach ( $variation_ids as $variation_id ) {

                $regular_prices[ $variation_id ] = $this->get_regular_price( $product_id, $variation_id );
            }


            $min_discount_prices = $this->get_min_discount_prices( $regular_prices, $prices );
            $max_discount_prices = $this->get_max_discount_prices( $regular_prices, $prices );

            if ( $this->same_min_max_discounts( $min_discount_prices, $max_discount_prices ) ) {

                return $this->process_single_discount( array(), $min_discount_prices[ 'price' ], $min_discount_prices[ 'discount_price' ] );
            }

            return $this->process_range_discount( array(), $min_discount_prices, $max_discount_prices );
        }

        private function process_range_price( $table_prices, $product_id, $min_price, $max_price ) {

            $text_format = esc_html__( '{{from_price}} - {{to_price}}', 'pricely-dynamic-pricing-cart-discounts' );

            $range_args = array(
                'from_price' => wc_price( $this->convert_amount( $min_price, 'views-data-table-price' ) ),
                'to_price' => wc_price( $this->convert_amount( $max_price, 'views-data-table-price' ) ),
            );

            $price = $this->replace_text( $text_format, $range_args );

            $table_prices[ 0 ] = array(
                'price' => $price,
                'suffix' => $this->get_price_suffix( $product_id, 0, '' ),
            );

            return $table_prices;
        }

        private function process_single_price( $table_prices, $product_id, $price ) {

            $table_prices[ 0 ] = array(
                'price' => wc_price( $this->convert_amount( $price, 'views-data-table-price' ) ),
                'suffix' => $this->get_price_suffix( $product_id, 0, $price ),
            );

            return $table_prices;
        }

        private function process_range_discount( $table_discounts, $min_discount_prices, $max_discount_prices ) {

            $min_discount = $this->get_discount_text( $min_discount_prices[ 'price' ], $min_discount_prices[ 'discount_price' ], 'discount', false );
            $max_discount = $this->get_discount_text( $max_discount_prices[ 'price' ], $max_discount_prices[ 'discount_price' ], 'discount', false );

            $min_discount_per = $this->get_discount_text( $min_discount_prices[ 'price' ], $min_discount_prices[ 'discount_price' ], '', true );
            $max_discount_per = $this->get_discount_text( $max_discount_prices[ 'price' ], $max_discount_prices[ 'discount_price' ], '', true );

            $variable_args = array(
                'from_discount' => $min_discount,
                'to_discount' => $max_discount,
            );

            $variable_per_args = array(
                'from_discount' => $min_discount_per,
                'to_discount' => $max_discount_per,
            );

            $variable_text_format = esc_html__( '{{from_discount}} - {{to_discount}}', 'pricely-dynamic-pricing-cart-discounts' );

            $variable_per_text_format = esc_html__( '{{from_discount}} - {{to_discount}}', 'pricely-dynamic-pricing-cart-discounts' );

            $table_discounts[ 0 ] = array(
                'discount' => $this->replace_text( $variable_text_format, $variable_args ),
                'discount_per' => $this->replace_text( $variable_per_text_format, $variable_per_args ),
            );

            return $table_discounts;
        }

        private function process_single_discount( $table_discounts, $regular_price, $price ) {

            $table_discounts[ 0 ] = array(
                'discount' => $this->get_discount_text( $regular_price, $price, 'discount', false ),
                'discount_per' => $this->get_discount_text( $regular_price, $price, '', true ),
            );

            return $table_discounts;
        }

        private function get_regular_price( $product_id, $variation_id ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            $price = $this->view_data->get_regular_price( $product_id, $variation_id );

            if ( false === $price ) {

                return 0;
            }

            return $this->get_price_to_display( $price, $product_id, $variation_id );
        }

        private function get_price_suffix( $product_id, $variation_id, $price ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->get_price_suffix( $product_id, $variation_id, $price );
        }

        private function get_min_price( $prices ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->get_min_price( $prices );
        }

        private function get_max_price( $prices ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->get_max_price( $prices );
        }

        private function get_min_discount_prices( $regular_prices, $prices ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->get_min_discount_prices( $regular_prices, $prices );
        }

        private function get_max_discount_prices( $regular_prices, $prices ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->get_max_discount_prices( $regular_prices, $prices );
        }

        private function same_min_max_discounts( $min_prices, $max_prices ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->same_min_max_discounts( $min_prices, $max_prices );
        }

        private function get_discount_text( $regular_price, $price, $mode, $show_per ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->get_discount_text( $regular_price, $price, $mode, $show_per );
        }

        private function replace_text( $text_format, $args ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->replace_text( $text_format, $args );
        }

        private function get_variation_prices_to_display( $prices, $product_id ) {

            $prices_to_display = array();

            foreach ( $prices as $variation_id => $price ) {

                $prices_to_display[ $variation_id ] = $this->get_price_to_display( $price, $product_id, $variation_id );
            }

            return $prices_to_display;
        }

        private function get_price_to_display( $price, $product_id, $variation_id ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->get_price_to_display( $price, $product_id, $variation_id );
        }

        private function convert_amount( $amount, $amount_id ) {

            if ( is_null( $this->view_data ) ) {

                $this->view_data = Pricely_View_Data::get_instance();
            }

            return $this->view_data->convert_amount( $amount, $amount_id );
        }

    }

}

