<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Cache_Params' ) ) {

    class Pricely_Cache_Params {

        private static $instance = null;
        private $hash;
        private $locale;

        public static function get_instance(): self {

            if ( null == self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->hash = '';
        }

        public function get_cart_hash( $hash ) {

            $this->hash = $hash;

            return $this->generate_hash();
        }

        public function generate_hash() {

            $parameter_types = $this->get_parameter_types();

            if ( false == $parameter_types ) {

                return md5( wp_json_encode( $this->get_default_hash_data() ) );
            }

            $hash_data = $this->get_hash_data( $parameter_types );

            return md5( wp_json_encode( $hash_data ) );
        }

        private function get_hash_data( $parameter_types ) {

            // Set cart hash
            $hash_data = array(
                'revision' => $this->get_revision(), //settings revision cache
                'lang' => $this->get_current_language(), // multilingual cache
                'is_pro' => $this->get_is_pro()
            );

            foreach ( $parameter_types as $parameter_type ) {

                if ( 'cart_items_hash' == $parameter_type ) {

                    $hash_data[ $parameter_type ] = $this->hash;
                }
            }

            return apply_filters( 'pricely_get-cart-hash-data', $hash_data, $parameter_types );
        }

        private function get_revision() {

            $default = 0;

            return $this->get_options( 'revision', $default );
        }

        private function get_current_language() {

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

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

            return $this->locale->get_current_language();
        }

        private function get_is_pro() {

            if ( !class_exists( 'Pricely_Init_Pro' ) ) {

                return false;
            }

            return true;
        }

        private function get_default_hash_data() {

            return array(
                'hash' => $this->hash,
                'time' => current_time( 'U' )
            );
        }

        private function get_parameter_types() {

            $settings = $this->get_settings();

            if ( ('yes' != $settings[ 'enable' ] ) ) {

                return false;
            }

            if ( !isset( $settings[ 'cache_params' ] ) ) {

                return false;
            }

            return $settings[ 'cache_params' ];
        }

        private function get_settings() {

            $default = array(
                'enable' => 'no'
            );

            return $this->get_options( 'cart_cache', $default );
        }

        private function get_options( $options_key, $default ) {

            return Pricely::get_instance()->get_options( $options_key, $default );
        }

    }

}