<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Items_In_Cart' ) ) {

    class Pricely_Items_In_Cart {

        private $data_key;
        private $items_in_cart_engine;
        private $items_in_cart;
        private $currency;

        public function __construct() {

            $this->data_key = 'items_in_cart';
            $this->items_in_cart = false;
        }

        private function init() {

            if ( $this->items_in_cart_engine ) {

                return;
            }

            $this->items_in_cart_engine = new Pricely_Items_In_Cart_Engine;
        }

        public function clear_cache() {

            //initialise all
            $this->init();
            $this->items_in_cart = false;

            $this->items_in_cart_engine->clear_cache();
        }

        public function get_cache( $data, $context ) {

            $items_in_cart = $this->revert_item_prices( $this->get_items_in_cart() );

            if ( !is_array( $items_in_cart ) ) {

                return $data;
            }

            $data[ $this->data_key ] = $items_in_cart;

            return $data;
        }

        public function get_product_cache( $data ) {

            return $this->process_product_data( $data );
        }

        public function process_data( $data, $context ) {

            $items_in_cart = $this->revert_item_prices( $this->get_items_in_cart() );

            if ( !is_array( $items_in_cart ) ) {

                return $data;
            }

            $data[ $this->data_key ] = $items_in_cart;

            return $data;
        }

        public function process_product_data( $data ) {

            if ( !isset( $data[ 'wc' ][ 'product' ][ 'key' ] ) ) {

                return $data;
            }

            $items_in_cart = $this->revert_item_prices( $this->get_items_in_cart() );

            if ( !is_array( $items_in_cart ) ) {

                return $data;
            }

            $key = $data[ 'wc' ][ 'product' ][ 'key' ];

            if ( !isset( $items_in_cart[ $key ] ) ) {

                return $data;
            }

            $data[ $this->data_key ] = $items_in_cart[ $key ];

            return $data;
        }

        private function get_items_in_cart() {

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

                return $this->items_in_cart;
            }

            //initialise all
            $this->init();

            //process data from engine
            $items_in_cart = $this->items_in_cart_engine->get_data();

            if ( !is_array( $items_in_cart ) ) {

                return false;
            }

            $this->items_in_cart = $items_in_cart;

            return $this->items_in_cart;
        }

        private function revert_item_prices( $items_in_cart ) {

            if ( !is_array( $items_in_cart ) ) {

                return $items_in_cart;
            }

            foreach ( $items_in_cart as $key => $item_in_cart ) {

                $items_in_cart[ $key ][ 'price' ] = $this->revert_amount( $item_in_cart[ 'price' ], 'items-in-cart-module-' . $key );
            }

            return $items_in_cart;
        }

        private function revert_amount( $amount, $amount_id ) {

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

                $this->currency = Pricely_Currency::get_instance();
            }

            return $this->currency->revert_amount( $amount, $amount_id );
        }

    }

}

