<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Items_In_Cart_Engine' ) ) {

    class Pricely_Items_In_Cart_Engine {

        private $items_in_cart_key;

        public function __construct() {

            $this->items_in_cart_key = 'items_in_cart';
        }

        public function clear_cache() {

            Pricely_Session::get_instance()->unset( $this->items_in_cart_key );
        }

        public function get_data() {

            return $this->get_items_in_cart();
        }

        private function get_items_in_cart() {

            return $this->get_cart_updated_quantity( Pricely_Session::get_instance()->get( $this->items_in_cart_key, false ) );
        }

        private function get_cart_updated_quantity( $items_in_cart ) {

            if ( !$items_in_cart ) {

                return $items_in_cart;
            }

            $posted_cart = $this->get_posted_cart();

            if ( !is_array( $posted_cart ) ) {

                return $items_in_cart;
            }

            $item_keys = array_keys( $items_in_cart );

            foreach ( $posted_cart as $cart_key => $item_qty ) {

                $items_in_cart = $this->update_item_quantity( $items_in_cart, $item_keys, $cart_key, $item_qty );
            }

            return $items_in_cart;
        }

        private function get_posted_cart() {

            if ( !isset( $_POST[ 'woocommerce-cart-nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ 'woocommerce-cart-nonce' ] ) ), 'woocommerce-cart' ) ) {

                return false;
            }

            if ( !isset( $_POST[ 'update_cart' ] ) ) {

                return false;
            }

            if ( !isset( $_POST[ 'cart' ] ) ) {

                return false;
            }
            
            if ( $this->is_auto_adding() ) {

                return false;
            }

            $posted_items = map_deep( wp_unslash( $_POST[ 'cart' ] ), 'sanitize_key' );

            return $this->get_posted_items( $posted_items );
        }

        private function get_posted_items( $posted_items ) {

            $qty_list = array();

            if ( !is_array( $posted_items ) ) {

                return $qty_list;
            }

            foreach ( $posted_items as $key => $value ) {

                if ( !isset( $value[ 'qty' ] ) ) {

                    continue;
                }

                if ( !is_numeric( $value[ 'qty' ] ) ) {

                    continue;
                }
                
                $item_key = sanitize_key( $key );

                if ( empty( $item_key ) ) {

                    continue;
                }

                $qty_list[ $item_key ] = ( float ) $value[ 'qty' ];
            }

            return $qty_list;
        }

        private function update_item_quantity( $items_in_cart, $item_keys, $cart_key, $item_qty ) {

            foreach ( $item_keys as $item_key ) {

                if ( $cart_key !== $items_in_cart[ $item_key ][ 'cart_key' ] ) {

                    continue;
                }

                $items_in_cart[ $item_key ][ 'quantity' ] = $item_qty;
            }

            return $items_in_cart;
        }
        
        private function is_auto_adding() {

            if ( !class_exists( 'Pricely_Pipeline_Cart_Items_Auto_Add' ) ) {

                return false;
            }

            return Pricely_Pipeline_Cart_Items_Auto_Add::get_instance()->is_auto_adding();
        }

    }

}
