<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Util' ) ) {

    require_once dirname( __FILE__ ) . '/pricely-taxonomy-util.php';
    require_once dirname( __FILE__ ) . '/pricely-validation-util.php';
    require_once dirname( __FILE__ ) . '/pricely-terms-validation-util.php';
    require_once dirname( __FILE__ ) . '/pricely-datetime-util.php';
    require_once dirname( __FILE__ ) . '/pricely-product-util.php';
    require_once dirname( __FILE__ ) . '/pricely-customer-util.php';

    require_once dirname( __FILE__ ) . '/quantity-util/quantity-util.php';
    require_once dirname( __FILE__ ) . '/pricely-totals-util.php';
    require_once dirname( __FILE__ ) . '/cart-total-utils/cart-total-util.php';
    require_once dirname( __FILE__ ) . '/pricely-cart-discounts-coupons-util.php';

    class Pricely_Util {

        private static $instance;
        private $locale;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function get_price_to_display( $price, $product, $variation = null, $quantity = 1 ) {

            $args = array(
                'qty' => $quantity,
                'price' => $price
            );

            if ( $variation ) {

                return wc_get_price_to_display( $variation, $args );
            }

            return wc_get_price_to_display( $product, $args );
        }

        public function get_price_suffix( $product, $variation = null, $price = '', $qty = 1 ) {

            if ( $variation ) {

                return $variation->get_price_suffix( $price, $qty );
            }

            return $product->get_price_suffix( $price, $qty );
        }

        public function get_product_ids_by_slugs( $slugs, $use_locale = true ) {

            $product_ids = array();

            $sql_hash = 'product_ids_' . md5( wp_json_encode( $slugs ) );

            $t_product_ids = get_transient( 'pricely_' . $sql_hash );

            if ( $t_product_ids ) {

                return $this->get_translated_product_ids( $t_product_ids, $use_locale, 'util-product-ids' );
            }

            try {
                global $wpdb;

                $sql = "SELECT id FROM " . $wpdb->posts
                        . " WHERE (post_type='product')"
                        . " AND post_name IN('" . implode( "','", array_map( 'esc_sql', $slugs ) ) . "')";


                $results = $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter

                foreach ( $results as $row ) {
                    $product_ids[] = $row[ 'id' ];
                }

                set_transient( 'pricely_' . $sql_hash, $product_ids, MINUTE_IN_SECONDS * 30 );
            }
            catch ( Exception $ex ) {

                return $this->get_translated_product_ids( $product_ids, $use_locale, 'util-product-ids' );
            }

            return $this->get_translated_product_ids( $product_ids, $use_locale, 'util-product-ids' );
        }

        public function get_product_term_ids_by_slugs( $slugs, $taxonomy, $use_locale = true ) {

            $term_ids = array();

            $sql_hash = $taxonomy . '_terms_ids_' . md5( wp_json_encode( $slugs ) );

            $t_term_ids = get_transient( 'pricely_' . $sql_hash );

            if ( $t_term_ids ) {

                return $this->get_translated_term_ids( $t_term_ids, $taxonomy, $use_locale, 'util-term-ids-' . $taxonomy );
            }

            try {

                global $wpdb;

                $sql = "SELECT terms.term_id FROM {$wpdb->prefix}terms AS terms"
                        . " LEFT JOIN {$wpdb->prefix}term_taxonomy AS term_taxonomy ON terms.term_id=term_taxonomy.term_id"
                        . " WHERE (term_taxonomy.taxonomy=%s)"
                        . " AND slug IN('" . implode( "','", array_map( 'esc_sql', $slugs ) ) . "')";

                $results = $wpdb->get_results( $wpdb->prepare( $sql, $taxonomy ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter

                foreach ( $results as $row ) {

                    $term_ids[] = $row[ 'term_id' ];
                }

                set_transient( 'pricely_' . $sql_hash, $term_ids, MINUTE_IN_SECONDS * 30 );
            }
            catch ( Exception $ex ) {

                return $this->get_translated_term_ids( $term_ids, $taxonomy, $use_locale, 'util-term-ids-' . $taxonomy );
            }

            return $this->get_translated_term_ids( $term_ids, $taxonomy, $use_locale, 'util-term-ids-' . $taxonomy );
        }

        private function get_translated_product_ids( $product_ids, $use_locale, $translation_id ) {

            if ( !$use_locale ) {

                return $product_ids;
            }
            
            if ( !count( $product_ids ) ) {

                return $product_ids;
            }

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

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

            return $this->locale->translate_product_ids( $product_ids, $translation_id );
        }

        private function get_translated_term_ids( $term_ids, $taxonomy, $use_locale, $translation_id ) {

            if ( !$use_locale ) {

                return $term_ids;
            }
            
            if ( !count( $term_ids ) ) {

                return $term_ids;
            }

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

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

            return $this->locale->translate_term_ids( $term_ids, $translation_id, $taxonomy );
        }

    }

}

