<?php

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

if ( !class_exists( 'Pricely_Product_Promotion_CountDown_Inline' ) ) {

    class Pricely_Product_Promotion_CountDown_Inline {

        private static $instance;
        private $ui_type = 'countdown-inline';
        private $locale;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function get_shop_view( $countdown_options, $option_args ) {

            $countdown_options[ 'shop' ][ 'view_id' ] = 'shop-countdown-inline';

            return $this->process_ui_options( $countdown_options, $option_args, 'shop', 'shop' );
        }

        public function get_single_product_view( $countdown_options, $option_args ) {

            $countdown_options[ 'single-product' ][ 'view_id' ] = 'single-countdown-inline';

            return $this->process_ui_options( $countdown_options, $option_args, 'single-product', 'single_product' );
        }

        private function process_ui_options( $countdown_options, $promotion_args, $page_id, $fields_group ) {

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

            $ui_options = $this->get_ui_options( $promotion_ui_id );

            $countdown_options[ $page_id ][ 'content' ][ 'title_text' ] = $ui_options[ 'title_text' ];
            $countdown_options[ $page_id ][ 'content' ][ 'title_tag' ] = $ui_options[ 'title_tag' ];

            $countdown_options[ $page_id ][ 'content' ][ 'days_text' ] = $ui_options[ 'label_days_text' ];
            $countdown_options[ $page_id ][ 'content' ][ 'hours_text' ] = $ui_options[ 'label_hours_text' ];
            $countdown_options[ $page_id ][ 'content' ][ 'minutes_text' ] = $ui_options[ 'label_minutes_text' ];
            $countdown_options[ $page_id ][ 'content' ][ 'seconds_text' ] = $ui_options[ 'label_seconds_text' ];

            $countdown_options[ $page_id ][ 'countdown_args' ] = $promotion_args[ 'countdown_args' ];
            $countdown_options[ $page_id ][ 'position' ] = $promotion_args[ $fields_group ][ 'position' ];

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

            return $countdown_options;
        }

        private function get_ui_id( $promotion_ui_id ) {

            $ui_id = str_replace( 'inline_', '', $promotion_ui_id );

            if ( empty( $ui_id ) ) {

                return '2234343';
            }

            return $ui_id;
        }

        private function get_ui_options( $promotion_ui_id ) {

            $options = $this->get_options( 'ui_countdown_inlines', $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_css_class( $ui_id ) {

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

        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[ 'title_text' ] = $this->get_translated_text( $option[ 'title_text' ], $ui_id, 'title' );
            $option[ 'label_days_text' ] = $this->get_translated_text( $option[ 'label_days_text' ], $ui_id, 'label-days' );
            $option[ 'label_hours_text' ] = $this->get_translated_text( $option[ 'label_hours_text' ], $ui_id, 'label-hours' );
            $option[ 'label_minutes_text' ] = $this->get_translated_text( $option[ 'label_minutes_text' ], $ui_id, 'label-minutes' );
            $option[ 'label_seconds_text' ] = $this->get_translated_text( $option[ 'label_seconds_text' ], $ui_id, 'label-seconds' );

            return $option;
        }

        private function get_translated_text( $text, $text_id, $text_key ) {

            if ( empty( $text ) ) {

                return $text;
            }

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

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

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

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

        private function get_default_ui_options() {

            $defaults = array();

            $defaults[] = array(
                'ui_id' => '2234343',
                'ui_is_default' => true,
                'title_text' => esc_html__( 'SALES ENDS IN', 'pricely-dynamic-pricing-cart-discounts' ),
                'title_tag' => 'h4',
                'label_days_text' => esc_html__( 'days', 'pricely-dynamic-pricing-cart-discounts' ),
                'label_hours_text' => esc_html__( 'hours', 'pricely-dynamic-pricing-cart-discounts' ),
                'label_minutes_text' => esc_html__( 'mins', 'pricely-dynamic-pricing-cart-discounts' ),
                'label_seconds_text' => esc_html__( 'secs', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return $defaults;
        }

        private function get_options( $option_key, $default ) {

            $pricely = Pricely::get_instance();

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

    }

}