<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Pipeline_View_Mini_Cart' ) ) {

    class Pricely_Pipeline_View_Mini_Cart {

        private static $instance;
        private $product_util;
        private $item_prices;
        private $is_mini_cart_box;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->is_mini_cart_box = false;
            $this->item_prices = array();

            add_action( 'woocommerce_before_mini_cart', array( $this, 'set_content_start' ), 1 );
            add_filter( 'woocommerce_widget_cart_item_quantity', array( $this, 'get_widget_cart_item_quantity' ), 99999, 3 );
        }

        public function get_cart_item_price( $theme_price, $cart_item_prices, $item_key ) {

            $this->item_prices[ $item_key ] = $cart_item_prices;

            $view_data = array(
                'view_id' => 'mini-cart-item-prices',
                'item_key' => $item_key,
                'prices' => $cart_item_prices[ 'prices' ],
            );

            ob_start();

            $rendered = $this->render_view( $view_data );

            $rendered_content = ob_get_clean();

            if ( $rendered ) {

                return $rendered_content;
            }

            return $theme_price;
        }

        public function get_widget_cart_item_quantity( $widget_cart_item_quantity, $cart_item, $cart_item_key ) {

            $item_key = $this->get_item_key( $cart_item );

            if ( !isset( $this->item_prices[ $item_key ] ) ) {

                return $widget_cart_item_quantity;
            }

            if ( !isset( $this->item_prices[ $item_key ][ 'prices' ] ) ) {

                return $widget_cart_item_quantity;
            }

            $view_data = array(
                'view_id' => 'mini-cart-item-prices',
                'item_key' => $item_key,
                'prices' => $this->item_prices[ $item_key ][ 'prices' ]
            );


            ob_start();

            $rendered = $this->render_view( $view_data );

            $rendered_content = ob_get_clean();

            if ( !$rendered ) {

                return $widget_cart_item_quantity;
            }

            return sprintf( '<span class="quantity">%s</span>', $rendered_content );
        }

        public function set_content_start() {
            
            $this->is_mini_cart_box = true;
        }

        public function is_mini_cart() {

            return $this->is_mini_cart_box;
        }

        private function get_item_key( $cart_item ) {

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

                $this->product_util = Pricely_Product::get_instance();
            }

            if ( !isset( $cart_item[ 'product_id' ] ) ) {

                return false;
            }

            if ( !isset( $cart_item[ 'variation_id' ] ) ) {

                return false;
            }

            return $this->product_util->get_product_key( $cart_item[ 'product_id' ], $cart_item[ 'variation_id' ] );
        }

        private function render_view( $view_data ) {

            return Pricely_Views::get_instance()->render_view( $view_data );
        }

    }

}