<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Locale' ) ) {

    require_once dirname( __FILE__ ) . '/checkout-prices/checkout-prices.php';
    require_once dirname( __FILE__ ) . '/cart-discounts/cart-discounts.php';
    require_once dirname( __FILE__ ) . '/product-promotions/product-promotions.php';
    require_once dirname( __FILE__ ) . '/ui-countdown/ui-countdown.php';
    require_once dirname( __FILE__ ) . '/ui-others/ui-others.php';

    class Pricely_Admin_Locale {

        private static $instance;
        private $prev_translation_ids_key = 'pricely_locale_ids';
        private $translator_obj;
        private $string_locals;

        public static function get_instance(): self {

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

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

            return self::$instance;
        }

        public function process_options( $options ) {

            $translator = $this->get_translator();

            if ( !$translator ) {

                return $options;
            }

            $strings = $this->get_strings( $options );

            if ( !$strings ) {

                return $options;
            }

            $this->unregister_prev_strings( $strings, $translator );

            $this->register_strings( $strings, $translator );

            return $options;
        }

        private function register_strings( $strings, $translator ) {

            if ( !method_exists( $translator, 'register_string' ) ) {

                return;
            }

            foreach ( $strings as $translation_id => $value ) {

                $translator->register_string( $value, $translation_id );
            }
        }

        private function unregister_prev_strings( $strings, $translator ) {

            if ( !method_exists( $translator, 'unregister_string' ) ) {

                return;
            }

            $new_translation_ids = array_keys( $strings );

            $prev_translation_ids = $this->get_prev_translation_id_ids();


            foreach ( $prev_translation_ids as $translation_id ) {

                if ( in_array( $translation_id, $new_translation_ids ) ) {

                    continue;
                }

                $translator->unregister_string( $translation_id );
            }

            $this->save_new_translation_id_ids( $new_translation_ids );
        }

        private function get_strings( $options ) {

            $strings = array();

            foreach ( $this->load_string_locals() as $string_locale ) {

                if ( !method_exists( $string_locale, 'get_strings' ) ) {

                    continue;
                }

                $strings = $string_locale->get_strings( $strings, $options );
            }

            if ( !is_array( $strings ) ) {

                return false;
            }

            if ( !count( $strings ) ) {

                return false;
            }

            return $strings;
        }

        private function get_prev_translation_id_ids() {

            return get_option( $this->prev_translation_ids_key, array() );
        }

        private function save_new_translation_id_ids( $new_translation_ids ) {

            if ( !count( $new_translation_ids ) ) {

                delete_option( $this->prev_translation_ids_key );
            } else {

                update_option( $this->prev_translation_ids_key, $new_translation_ids, false );
            }
        }

        private function get_translator() {

            if ( !is_null( $this->translator_obj ) ) {

                return $this->translator_obj;
            }

            $this->translator_obj = false;

            $translators = $this->register_translators();

            if ( !is_array( $translators ) ) {

                return $this->translator_obj;
            }

            foreach ( $translators as $translator ) {

                if ( !class_exists( $translator ) ) {

                    continue;
                }

                if ( method_exists( $translator, 'get_instance' ) ) {

                    $this->translator_obj = $translator::get_instance();
                } else {

                    $this->translator_obj = new $translator();
                }

                return $this->translator_obj;
            }

            return $this->translator_obj;
        }

        private function register_translators() {

            $translators = array();

            if ( class_exists( 'Pricely_Polylang' ) ) {

                $translators[ 'polylang' ] = 'Pricely_Polylang';
            }

            if ( class_exists( 'Pricely_WPML' ) ) {

                $translators[ 'wpml' ] = 'Pricely_WPML';
            }

            return apply_filters( 'pricely_register_locale_translators', $translators );
        }

        private function load_string_locals() {

            if ( !is_null( $this->string_locals ) ) {

                return $this->string_locals;
            }

            $this->string_locals = array();

            foreach ( $this->register_string_locals() as $key => $string_locale ) {

                if ( !class_exists( $string_locale ) ) {

                    continue;
                }

                if ( method_exists( $string_locale, 'get_instance' ) ) {

                    $this->string_locals[ $key ] = $string_locale::get_instance();
                } else {

                    $this->string_locals[ $key ] = new $string_locale();
                }
            }

            return $this->string_locals;
        }

        private function register_string_locals() {

            $string_locals = array(
                'checkout_prices' => 'Pricely_Admin_Locale_Checkout_Prices',
                'cart_discounts' => 'Pricely_Admin_Locale_Cart_Discounts',
                'promotions' => 'Pricely_Admin_Locale_Product_Promotions',
                'countdown' => 'Pricely_Admin_Locale_Styles_CountDown',
                'others' => 'Pricely_Admin_Locale_Styles_Other'
            );

            return apply_filters( 'pricely_admin_register_string_locale', $string_locals );
        }

    }

}

