<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// Woocommerce Cart Integration Callbacks
// Woocomerce Hook and filters
/*
*  Add woocommerce Cart meta data from Shipping rate
*/
function wpcsr_add_item_data($cart_item_data, $product_id) {
    global $woocommerce;
    return $cart_item_data;
}
/*
*   Get woocommerce Cart meta data from Shipping rate from Session
*/
function wpcsr_get_cart_items_from_session($item,$values,$key) {
    if (array_key_exists( '_wpcsrRate', $values ) ) {
        $item['_wpcsrRate'] = $values['_wpcsrRate'];
    }
    return $item;
}

/*
*   Display Shipping rate meta data in Product cart
*/
function wpcsr_add_metadata_session($product_name, $values, $cart_item_key ) {
	if( !empty( $values['_wpcsrRate'] ) ){
		$product_name =  wpcsr_shipment_variation_label().':'.$values['_wpcsrRate'][wpcsr_shipment_variation_label()].'<br/>';
		$product_name .= wpcsr_service_variation_label().': '.$values['_wpcsrRate'][wpcsr_service_variation_label()].'<br/>';
		$product_name .= wpcsr_weight_variation_label().': '.$values['_wpcsrRate'][wpcsr_weight_variation_label()].'<br/>';
		$product_name .= wpcsr_zone_variation_label().': '.$values['_wpcsrRate'][wpcsr_zone_variation_label()].'<br/>';
		$product_name .= wpcsr_delivery_method_label().': '.$values['_wpcsrRate'][wpcsr_delivery_method_label()].'<br/>';
		$product_name .= wpcsr_origin_label().': '.$values['_wpcsrRate'][wpcsr_origin_label()].'<br/>';
		$product_name .= wpcsr_destination_label().': '.$values['_wpcsrRate'][wpcsr_destination_label()].'<br/>';
		$product_name .= '<br/><strong>'.wpcsr_cost_breakdown_variation_label().'</strong>'.'<br/>';
        $product_name .= wpcsr_rate_label().': '.format_wpcsr_currency( $values['_wpcsrRate'][wpcsr_rate_label()] ).'<br/>';
		if( $values['_wpcsrRate']['shipment_protection'] == 1 ){
			$product_name .= wpcsr_protection_variation_label().': '.format_wpcsr_currency( $values['_wpcsrRate'][wpcsr_protection_variation_label()] ).'<br/>';
		}
        if( $values['_wpcsrRate']['is_pickup'] ){
            $product_name .= wpcsr_pickup_rate_label().': '.format_wpcsr_currency( $values['_wpcsrRate'][wpcsr_pickup_rate_label()] ).'<br/>';
        }
	}
    return $product_name;
}
/*
*   Display Shipping rate meta data in checkout
*/
function wpcsr_add_metadata_to_order_item_meta($item_id, $values) {
    global $woocommerce,$wpdb;
    if( !empty( $values['_wpcsrRate'] ) ){
		//wc_add_order_item_meta
    	wc_add_order_item_meta( $item_id, wpcsr_shipment_variation_label(), $values['_wpcsrRate'][wpcsr_shipment_variation_label()] );
    	wc_add_order_item_meta( $item_id, wpcsr_service_variation_label(), $values['_wpcsrRate'][wpcsr_service_variation_label()] );
		wc_add_order_item_meta( $item_id, wpcsr_weight_variation_label(), $values['_wpcsrRate'][wpcsr_weight_variation_label()] );
		wc_add_order_item_meta( $item_id, wpcsr_zone_variation_label(), $values['_wpcsrRate'][wpcsr_zone_variation_label()] );
		wc_add_order_item_meta( $item_id, wpcsr_delivery_method_label(), $values['_wpcsrRate'][wpcsr_delivery_method_label()] );
		wc_add_order_item_meta( $item_id, wpcsr_origin_label(), $values['_wpcsrRate'][wpcsr_origin_label()] );
		wc_add_order_item_meta( $item_id, wpcsr_destination_label(), $values['_wpcsrRate'][wpcsr_destination_label()] );
		wc_add_order_item_meta( $item_id, wpcsr_package_qty_label(), $values['_wpcsrRate']['quantity'] );
        wc_add_order_item_meta( $item_id, wpcsr_rate_label(), format_wpcsr_currency( $values['_wpcsrRate'][wpcsr_rate_label()] ) );
		if( $values['_wpcsrRate']['shipment_protection'] == 1 ){
            wc_add_order_item_meta( $item_id, wpcsr_protection_variation_label(), format_wpcsr_currency( $values['_wpcsrRate'][wpcsr_protection_variation_label()] ) );
		}
        if( $values['_wpcsrRate']['is_pickup'] ){
            wc_add_order_item_meta( $item_id, wpcsr_pickup_rate_label(), $values['_wpcsrRate'][wpcsr_pickup_rate_label()] );
        }
    }
}
/*
*   Customize Product price from Shipping rate
*/
function wpcsr_update_product_price( $cart_object ) {
    foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {
    	if( !empty( $value['_wpcsrRate'] ) ){
			$value['data']->set_price( $value['_wpcsrRate'][wpcsr_price_variation_label()] );
    	}
    }
}
function wpcsr_woointeg_completed_order( $order ){
	$order     		= wc_get_order($order);
	$order_status 	= $order->get_status();
    $items     		= $order->get_items();
    $woointeg_product = get_option('woointeg_product');
	$woointeg_status  = get_option('woointeg_status');
    	$shipments = array();
		foreach ( $items as $item ) :
			$item_data 		= $item->get_data();
			if( $item_data['product_id'] != $woointeg_product ){
				continue;
			}
			$item_meta_data = $item->get_meta_data();
			if( !empty( $item_meta_data[0]->value ) ){
				$shipments[] = $item_meta_data[0]->value;
			}
		endforeach;
		if( !empty( $shipments ) ){
			// Change the Shipment Post Status into publish when the order is submitted
			foreach ( $shipments as $shipment ) {
				$shipment_id = get_wpcsr_shipment_id( trim($shipment), 'pending' );
				wp_update_post(
					array(
						'ID'			=> $shipment_id,
						'post_status'   => 'publish'
					)
				);
			}
		}
		if( $order_status == 'completed' ){
			//** Update shipment Status
			if( !empty( $shipments ) ){
				?><h3><?php esc_html_e( 'The following Shipment is now', 'wpcargo-shipment-rate' ); ?> <?php echo $woointeg_status; ?></h3><?php
				?><ul><?php
				foreach ( $shipments as $shipment ) {
					$shipment_id = get_wpcsr_shipment_id( trim($shipment) );
					update_post_meta( $shipment_id, 'wpcargo_status', $woointeg_status );
					?><li><?php echo $shipment; ?></li><?php
				}
				?></ul><?php
			}
		}
}
//** Woocommerce Cart Integration
add_filter( 'woocommerce_add_cart_item_data', 'wpcsr_add_item_data', 1, 10 );
add_filter( 'woocommerce_get_cart_item_from_session', 'wpcsr_get_cart_items_from_session', 1, 3 );
add_filter( 'woocommerce_cart_item_name', 'wpcsr_add_metadata_session', 1, 3 );
add_action( 'woocommerce_add_order_item_meta', 'wpcsr_add_metadata_to_order_item_meta', 1, 2 );
add_action( 'woocommerce_before_calculate_totals', 'wpcsr_update_product_price', 1, 1 );
add_action( 'woocommerce_email_before_order_table', 'wpcsr_woointeg_completed_order' );