<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Checkout_Prices_Cache' ) ) {

    class Pricely_Checkout_Prices_Cache {

        private static $instance;
        private $prices;
        private $cache_key;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->prices = false;
            $this->cache_key = 'cart_prices';
        }

        public function clear() {

            $this->prices = false;
            Pricely_Cache::get_instance()->clear_data( $this->cache_key );
        }

        public function get() {

            if ( false !== $this->prices ) {

                return $this->prices;
            }

            if ( $this->is_virtual_cart() ) {

                return false;
            }

            if ( !$this->is_valid_data() ) {

                return false;
            }

            return Pricely_Cache::get_instance()->get_data( $this->cache_key, false );
        }

        public function set( $prices ) {

            if ( is_array( $prices ) ) {

                $this->prices = $prices;
            } else {

                $this->prices = false;
            }

            if ( $this->is_virtual_cart() ) {

                return;
            }

            Pricely_Cache::get_instance()->store_data( $this->cache_key, $this->prices );
        }

        private function is_valid_data() {

            return Pricely_Cache::get_instance()->is_valid_data();
        }

        private function is_virtual_cart() {

            if ( !defined( 'PRICELY_VIRTUAL_CART' ) ) {

                return false;
            }

            if ( false === PRICELY_VIRTUAL_CART ) {

                return false;
            }

            return true;
        }

    }

}