<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
function wpcstl_get_pallet_number( $packages ){
    $packages = maybe_unserialize( $packages );
    if( empty(  $packages ) ){
        return 0;
    }
    return array_reduce($packages, function( $curr, $item ){
        $qty    = array_key_exists( 'wpc-pm-qty', $item ) ? $item['wpc-pm-qty'] : 0 ;
        return $curr + $qty;
    }, 0 );
}
function wpcstl_get_total_weight( $packages ){
    $packages = maybe_unserialize( $packages );
    if( empty(  $packages ) ){
        return false;
    }
    return array_reduce($packages, function( $curr, $item ){
        $qty        = array_key_exists( 'wpc-pm-qty', $item ) ? $item['wpc-pm-qty'] : 0 ;
        $weight     = array_key_exists( 'wpc-pm-weight', $item ) ? $item['wpc-pm-weight'] : 0 ;
        return $curr + ( $qty * $weight );
    }, 0 );
}
function wpcstl_get_order_shipments( $order_id ){
    $order      = wc_get_order($order_id);
    $items      = $order->get_items();
    $shipments  = array();
    foreach ( $items as $item ) :
        if( $item->get_product_id() != get_option('woointeg_product') ){
            continue;
        }
        $item_meta_data = $item->get_meta_data();
        $searched_data  = array_search('_wpcstl_id', array_column($item_meta_data, 'key'));
        if( $searched_data === false ){
            continue;
        }
        $shipments[] = $item_meta_data[$searched_data]->value;
    endforeach;
    return $shipments;
}
function wpcstl_map_location( $location_id ){
    $location = '';
    if( $location_id == 5 ){
        $location = 'sydney';
    }elseif( $location_id == 6 ){
        $location = 'melbourne';
    }elseif( $location_id == 7 ){
        $location = 'brisbane';
    }
    return $location;
}
function wpcstl_get_pallet_cost( $origin, $destination, $packages ){
    $pallet     = wpcstl_get_pallet_number(  $packages );
    // Origin and Destination are not the same
    if( $origin != $destination ){
        switch ( $pallet ) {
            case $pallet == 1 :
                $cost = 120; // Change this value for the price
                break;
            case  $pallet >= 2 && $pallet < 10 :
                $cost = $pallet * 90; // Change this value for the price
                break;
            case  $pallet >= 10 && $pallet < 23 :
                $cost = $pallet * 80; // Change this value for the price
                break;
            case  $pallet >= 23:
                $cost = $pallet * 70; // Change this value for the price
                break;
            default:
                $cost = 0;
        }
        return $cost;
    }
    // The same location
    switch ( $pallet ) {
        case $pallet == 1 :
            $cost = 85; // Change this value for the price
            break;
        case  $pallet >= 2 && $pallet < 8 :
            $cost = $pallet * 15; // Change this value for the price
            break;
        case  $pallet >= 8 && $pallet < 23 :
            $cost = $pallet * 13; // Change this value for the price
            break;
        case  $pallet >= 23:
            $cost = $pallet * 10; // Change this value for the price
            break;
        default:
            $cost = 0;
    }
    return $cost;
}
function wpcstl_key_metakey_label( $meta_key ){
    global $wpdb;
    return $wpdb->get_var( "SELECT `label` FROM `".$wpdb->prefix."wpcargo_custom_fields` WHERE `field_key`='".$meta_key."'" );
}
function  wpcstl_get_gst( $freight_cost ){
    $insurance  = (float)$freight_cost * .002;
    $gst        = ( $insurance + $freight_cost ) * 0.1;
    $subtotal   = ( $insurance + $freight_cost ) * 1.1;
    return array(
        'insurance' => (float)$insurance,
        'gst'       => (float)$gst,
        'subtotal' => (float)$subtotal
    );
}
function format_wpcstl_currency( $value, $html = false ){
	$formatted_value = $value ? number_format( $value, 2, ".", "") : 0;
	if( $html ){
		$formatted_value = wpcsr_currency().' '.$formatted_value;
	}
	return $formatted_value;
}