<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Product_Promotion_Type_Volumetric_Price_Table' ) ) {

    require_once 'volumetric-price-table-range.php';

    class Pricely_Product_Promotion_Type_Volumetric_Price_Table {

        private static $instance;
        private $ui_type = 'price-table';
        private $ui_option_key = 'ui_pricing_tables';
        private $volumetric_price_table_range;
        private $locale;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function can_process_data( $promotion_type ) {

            return ('volumetric_price_table' == $promotion_type);
        }

        public function process_data( $promotion_data, $promotion_args, $data ) {

            $processed_data = $this->get_table_options( $this->get_translated_args( $promotion_args, $data ), $data );

            if ( !$processed_data ) {

                return $promotion_data;
            }

            $promotion_data[ 'views' ] = $processed_data;

            return $promotion_data;
        }

        private function get_table_options( $promotion_args, $data ) {

            if ( !$this->can_process_view( $data ) ) {

                return false;
            }

            $views = $this->get_single_product_view( $this->get_shop_view( array(), $promotion_args, $data ), $promotion_args, $data );

            if ( !count( $views ) ) {

                return false;
            }

            return $views;
        }

        private function get_shop_view( $table_options, $promotion_args, $data ) {

            $show_in_shop = ('no' != $promotion_args[ 'shop' ][ 'enable' ]);

            if ( !$show_in_shop ) {

                return $table_options;
            }

            return $this->process_ui_options( $table_options, $promotion_args, 'shop', 'shop', $data );
        }

        private function get_single_product_view( $table_options, $promotion_args, $data ) {

            $show_in_single_product = ('no' != $promotion_args[ 'single_product' ][ 'enable' ]);

            if ( !$show_in_single_product ) {

                return $table_options;
            }

            return $this->process_ui_options( $table_options, $promotion_args, 'single-product', 'single_product', $data );
        }

        private function process_ui_options( $table_options, $promotion_args, $page_id, $fields_group, $data ) {

            $promotion_ui_id = $this->get_ui_id( $promotion_args[ $fields_group ][ 'ui_id' ] );
            $ui_options = $this->get_ui_options( $promotion_ui_id );

            $table_options[ $page_id ][ 'view_id' ] = $this->get_view_id( $ui_options[ 'layout' ], $page_id );

            $table_options[ $page_id ][ 'position' ] = $promotion_args[ $fields_group ][ 'position' ];

            $table_options[ $page_id ][ 'ui_id' ] = $promotion_ui_id;

            $table_options[ $page_id ][ 'ui_css_class' ] = $this->get_css_class( $promotion_ui_id );

            $table_options[ $page_id ][ 'options' ] = $this->process_options( $promotion_args );

            $table_options[ $page_id ][ 'head' ] = $this->process_header_options( $ui_options );

            $table_options[ $page_id ][ 'text_formats' ] = $this->process_text_formats( $promotion_args );

            $table_options[ $page_id ][ 'price_options' ] = $this->get_price_options( $promotion_args, $data );

            return $table_options;
        }

        private function process_options( $promotion_args ) {

            return array(
                'prices' => ('yes' == $promotion_args[ 'show_prices' ]),
                'discounts' => ('yes' == $promotion_args[ 'show_discounts' ]),
                'discount_pers' => ('yes' == $promotion_args[ 'show_discounts_per' ]),
            );
        }

        private function process_header_options( $ui_options ) {

            $header = array(
                'titles' => $ui_options[ 'pricing_titles_text' ],
                'prices' => $ui_options[ 'price_text' ],
                'discounts' => $ui_options[ 'discount_text' ],
                'discount_pers' => $ui_options[ 'discount_per_text' ],
            );

            return $header;
        }

        private function process_text_formats( $promotion_args ) {

            $text_formats = array(
                'range_quantity_text' => $promotion_args[ 'range_quantity_text' ],
                'from_quantity_text' => $promotion_args[ 'from_quantity_text' ],
            );

            return $text_formats;
        }

        private function get_price_options( $promotion_args, $data ) {

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

                $this->volumetric_price_table_range = Pricely_Product_Promotion_Type_Volumetric_Price_Table_Range::get_instance();
            }

            $pricing_args = array(
                'promotion_id' => $promotion_args[ 'id' ],
            );


            return $this->volumetric_price_table_range->get_data( $pricing_args, $data );
        }

        private function get_view_id( $layout, $page_id ) {

            if ( 'vertical' == $layout && 'shop' == $page_id ) {

                return 'shop-volumetric-price-table-vertical';
            }

            if ( 'horizontal' == $layout && 'shop' == $page_id ) {

                return 'shop-volumetric-price-table-horizontal';
            }

            if ( 'vertical' == $layout && 'single-product' == $page_id ) {

                return 'single-volumetric-price-table-vertical';
            }

            return 'single-volumetric-price-table-horizontal';
        }

        private function get_css_class( $promotion_ui_id ) {

            return Pricely_Views_CSS::get_instance()->get_css_class( $promotion_ui_id, $this->ui_type );
        }

        private function get_ui_options( $promotion_ui_id ) {

            $options = $this->get_options( $this->ui_option_key, $this->get_default_ui_options() );

            foreach ( $options as $option ) {

                if ( $promotion_ui_id == $option[ 'ui_id' ] ) {

                    return $this->get_translated_ui_option( $option );
                }
            }

            if ( count( $options ) ) {

                return $this->get_translated_ui_option( $options[ 0 ] );
            }
        }

        private function get_ui_id( $option_ui_id ) {

            if ( empty( $option_ui_id ) ) {

                return '2234343';
            }

            return $option_ui_id;
        }

        private function get_translated_args( $promotion_args, $data ) {

            if ( !$this->can_process_view( $data ) ) {

                return $promotion_args;
            }

            $show_in_single_product = ('no' != $promotion_args[ 'single_product' ][ 'enable' ]);
            $show_in_shop = ('no' != $promotion_args[ 'shop' ][ 'enable' ]);

            if ( !$show_in_single_product && !$show_in_shop ) {

                return $promotion_args;
            }

            $settings_id = $promotion_args[ 'setting_id' ];
            $promotion_id = $promotion_args[ 'id' ];

            $promotion_args[ 'range_quantity_text' ] = $this->get_translated_text( $promotion_args[ 'range_quantity_text' ], $settings_id, $promotion_id, 'range-quantity' );
            $promotion_args[ 'from_quantity_text' ] = $this->get_translated_text( $promotion_args[ 'from_quantity_text' ], $settings_id, $promotion_id, 'from-quantity' );

            return $promotion_args;
        }

        private function get_translated_ui_option( $option ) {

            if ( isset( $option[ 'ui_is_default' ] ) && true == $option[ 'ui_is_default' ] ) {

                return $option;
            }

            $ui_id = $option[ 'ui_id' ];

            $option[ 'pricing_titles_text' ] = $this->get_translated_ui_text( $option[ 'pricing_titles_text' ], $ui_id, 'header-titles' );
            $option[ 'price_text' ] = $this->get_translated_ui_text( $option[ 'price_text' ], $ui_id, 'header-price' );
            $option[ 'discount_text' ] = $this->get_translated_ui_text( $option[ 'discount_text' ], $ui_id, 'header-discount' );
            $option[ 'discount_per_text' ] = $this->get_translated_ui_text( $option[ 'discount_per_text' ], $ui_id, 'header-discount-per' );

            return $option;
        }

        private function get_translated_text( $text, $settings_id, $promotion_id, $text_key ) {

            $translation_id = 'product-promotion-' . $this->ui_type . '-' . $settings_id . '-' . $promotion_id . '-' . $text_key;

            return $this->get_translate_string( $text, $translation_id );
        }

        private function get_translated_ui_text( $text, $ui_id, $text_key ) {

            $translation_id = 'ui-' . $this->ui_type . '-' . $ui_id . '-' . $text_key;

            return $this->get_translate_string( $text, $translation_id );
        }

        private function get_translate_string( $value, $translation_id ) {

            if ( empty( $value ) ) {

                return $value;
            }

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

                $this->locale = Pricely_Locale::get_instance();
            }

            return $this->locale->translate_string( $value, $translation_id );
        }

        private function get_default_ui_options() {

            $defaults = array();

            $defaults[] = array(
                'ui_id' => '2234343',
                'ui_is_default' => true,
                'layout' => 'vertical',
                'pricing_titles_text' => esc_html__( 'Header Text', 'pricely-dynamic-pricing-cart-discounts' ),
                'price_text' => esc_html__( 'Price', 'pricely-dynamic-pricing-cart-discounts' ),
                'discount_text' => esc_html__( 'Discount', 'pricely-dynamic-pricing-cart-discounts' ),
                'discount_per_text' => esc_html__( 'Discount %', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return $defaults;
        }

        private function can_process_view( $data ) {

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

                return false;
            }

            $product_data = $data[ 'wc' ][ 'product' ];

            if ( 'variable' == $product_data[ 'type' ] && $product_data[ 'variation_id' ] > 0 ) {

                return false;
            }

            return true;
        }

        private function get_options( $option_key, $default ) {

            $pricely = Pricely::get_instance();

            return $pricely->get_options( $option_key, $default );
        }

    }

}