<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Locale_Styles_CountDown_Inline' ) ) {

    class Pricely_Admin_Locale_Styles_CountDown_Inline {

        private static $instance;
        private $ui_type = 'countdown-inline';
        private $ui_options_key = 'ui_countdown_inlines';

        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_title( $strings, $option );
        }

        private function process_title( $strings, $option ) {

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

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

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

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

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

        private function process_days( $strings, $option ) {

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

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

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

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

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

        private function process_hours( $strings, $option ) {

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

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

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

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

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

        private function process_minutes( $strings, $option ) {

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

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

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

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

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

        private function process_seconds( $strings, $option ) {

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

                return $strings;
            }

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

            $strings[ $string_id ] = $option[ 'label_seconds_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;
        }

    }

}

