<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Locale_Styles_Price_Table' ) ) {

    class Pricely_Admin_Locale_Styles_Price_Table {

        private static $instance;
        private $ui_type = 'price-table';
        private $ui_options_key = 'ui_pricing_tables';

        public static function get_instance(): self {

            if ( is_null( self::$instance ) ) {

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

            return self::$instance;
        }

        public function get_strings( $strings, $options ) {

            foreach ( $this->get_options( $options, $this->ui_options_key, array() ) as $option ) {

                $strings = $this->process_strings( $strings, $option );
            }

            return $strings;
        }

        private function process_strings( $strings, $option ) {

            return $this->process_pricing_titles_text( $strings, $option );
        }

        private function process_pricing_titles_text( $strings, $option ) {

            if ( empty( $option[ 'pricing_titles_text' ] ) ) {

                return $this->process_price_text( $strings, $option );
            }

            $string_id = $this->get_string_id( $option[ 'ui_id' ], 'header-titles' );

            $strings[ $string_id ] = $option[ 'pricing_titles_text' ];

            return $this->process_price_text( $strings, $option );
        }

        private function process_price_text( $strings, $option ) {

            if ( empty( $option[ 'price_text' ] ) ) {

                return $this->process_discount_text( $strings, $option );
            }

            $string_id = $this->get_string_id( $option[ 'ui_id' ], 'header-price' );

            $strings[ $string_id ] = $option[ 'price_text' ];

            return $this->process_discount_text( $strings, $option );
        }

        private function process_discount_text( $strings, $option ) {

            if ( empty( $option[ 'discount_text' ] ) ) {

                return $this->process_discount_per_text( $strings, $option );
            }

            $string_id = $this->get_string_id( $option[ 'ui_id' ], 'header-discount' );

            $strings[ $string_id ] = $option[ 'discount_text' ];

            return $this->process_discount_per_text( $strings, $option );
        }

        private function process_discount_per_text( $strings, $option ) {

            if ( empty( $option[ 'discount_per_text' ] ) ) {

                return $strings;
            }

            $string_id = $this->get_string_id( $option[ 'ui_id' ], 'header-discount-per' );

            $strings[ $string_id ] = $option[ 'discount_per_text' ];

            return $strings;
        }

        private function get_options( $options, $option_key, $default ) {

            if ( isset( $options[ $option_key ] ) ) {

                return $options[ $option_key ];
            }

            return $default;
        }

        private function get_string_id( $ui_id, $key ) {

            return 'ui-' . $this->ui_type . '-' . $ui_id . '-' . $key;
        }

    }

}