<?php

class Wiaas_Admin_Orders {

    public static function init() {

        // Orders list customization

        add_filter( 'manage_shop_order_posts_columns', array(__CLASS__, 'add_additional_columns_to_orders_list'), 11 );

        add_action( 'manage_shop_order_posts_custom_column', array(__CLASS__, 'render_orders_list_additional_columns') );

        add_filter('default_hidden_columns', array(__CLASS__, 'filter_orders_list_default_hidden_columns'), 11, 2);

        // Order preview customization

        add_filter( 'woocommerce_admin_order_preview_get_order_details', array(__CLASS__, 'add_custom_data_to_order_preview') );

        add_filter( 'woocommerce_admin_order_preview_start', array(__CLASS__, 'show_custom_data_before_order_preview') );

        add_filter( 'woocommerce_admin_order_preview_line_items', array(__CLASS__, 'remove_simple_items_from_preview'));

	    add_filter( 'woocommerce_admin_order_preview_line_item_columns', array(__CLASS__, 'order_preview_order_item_columns'));

	    add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_order_item_price', array(__CLASS__, 'render_order_item_preview_price_column'), 10, 4);

	    // Order item metadata customization

	    add_action('woocommerce_before_order_itemmeta', array(__CLASS__, 'render_order_details_order_item_custom_info'), 10, 3);

	    add_filter('woocommerce_order_item_get_formatted_meta_data', array(__CLASS__, 'filter_order_item_formatted_meta_data'), 10, 2);

	    add_filter('woocommerce_order_item_display_meta_key', array(__CLASS__, 'order_item_display_meta_key'), 10, 3);

	    add_filter('woocommerce_order_item_display_meta_value', array(__CLASS__, 'order_item_display_meta_value'), 10, 3);

    }

	/**
     * Add columns to orders list with wiaas specific informations
	 * @param array $columns
	 *
	 * @return array
	 */
    public static function add_additional_columns_to_orders_list( $columns ) {

        $new_columns = array();

        $new_columns['wiaas_reference']             = __('Location', 'wiaas');
        $new_columns['wiaas_commercial_lead']       = __('Commercial lead', 'wiaas');
        $new_columns['wiaas_customer']              = __('Customer', 'wiaas');
        $new_columns['taxonomy-shop_order_project'] = __('Project', 'wiaas');

        $columns = array_merge(
                array_slice($columns,0,3),
                $new_columns,
                array_slice($columns,3)
        );

        return $columns;

    }

	/**
     * Display data for custom wiaas columns in orders list
     *
	 * @param string $column
	 */
    public static function render_orders_list_additional_columns( $column ) {

        global $post;
        $column_content = '';

        switch ($column){
            case 'wiaas_reference':
                $column_content = Wiaas_Order::get_order_reference($post->ID);
                break;

            case 'wiaas_commercial_lead':
                $column_content = Wiaas_Order::get_order_commercial_lead_name($post->ID);
                break;

            case 'wiaas_customer':
                $column_content = Wiaas_Order::get_order_customer_full_name($post->ID);

                $customer_organization_info = Wiaas_Order::get_customer_organization_info($post->ID);

                if ( ! empty($customer_organization_info) ) {

	                $column_content .= '<br>';
	                $column_content .= '<small>' . $customer_organization_info['name'] . '</small>';
                }
                break;
        }

        echo $column_content;
    }

	/**
     * Filter default hidden columns for orders list
     *
	 * @param array $hidden
	 * @param object $screen
	 *
	 * @return array
	 */
    public static function filter_orders_list_default_hidden_columns($hidden, $screen) {

        if (isset($screen->id) && $screen->id === 'edit-shop_order') {

            $hidden = array( 'wc_actions' );
        }

        return $hidden;
    }

	/**
     * Add custom information to order preview data
     *
	 * @param array $order
	 *
	 * @return array
	 */
    public static function add_custom_data_to_order_preview ($order) {

        $order['wiaas_commercial_lead_name'] = Wiaas_Order::get_order_commercial_lead_name( $order['data']['id'] );
        $order['needs_shipping'] = true;

        return $order;
    }

	/**
	 * Display custom information in order preview
     *
	 */
    public static function show_custom_data_before_order_preview () {

        echo    '<div class="wc-order-preview-addresses">
                    <div class="wc-order-preview-address">
                        <h3>Commercial lead</h3>
                        <strong>{{data.wiaas_commercial_lead_name}}</strong>
                    </div>
                  </div>';
    }

	/**
     * Display only bundles (not simple products)
     *
     * This will also not display options and addons
     *
	 * @param array $order_items
	 *
	 * @return array
	 */
    public static function remove_simple_items_from_preview( $order_items){

        $items =  array();

        foreach ($order_items as $order_item) {

            if ( Wiaas_Order_Item::is_standard_bundle($order_item) ) {
                $items[] = $order_item;
            }
        }

        return $items;
    }

	/**
     * Columns for order preview order items table
     *
	 * @return array
	 */
    public static function order_preview_order_item_columns( ) {

        return array(
                'product' => __('Product', 'wiaas'),
                'quantity' => __( 'Quantity', 'wiaas' ),
        	    'wiaas_order_item_price' => __( 'Price', 'wiaas' )
        );

    }

	/**
	 * @param $empty
	 * @param WC_Order_Item $item
	 * @param int $item_id
	 * @param WC_Order $order
     *
     * @return string
	 */
    public static function render_order_item_preview_price_column($empty, $item, $item_id, $order) {

    	if (Wiaas_Order_Item::is_standard_bundle($item)) {

		    $total_price = wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) );

		    $monthly_price = wc_price(
			    Wiaas_Order_Item::get_monthly_recurring_total($item),
			    array( 'currency' => $order->get_currency() )
		    );

		    return sprintf('<strong>On Delivery:</strong> %s <br> <strong>Monthly:</strong> %s',
			    $total_price,
			    $monthly_price);
	    }

	    return '';
    }

	/**
     * Render order item payment info with order item metadata on order details page
     *
	 * @param $item_id
	 * @param WC_Order_Item $item
	 * @param $product
	 */
    public static function render_order_details_order_item_custom_info($item_id, $item, $product) {

    	if (Wiaas_Order_Item::is_standard_bundle($item)) {

    		$order = $item->get_order();

		    $total_price = wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) );

		    $monthly_price = wc_price(
		            Wiaas_Order_Item::get_monthly_recurring_total($item),
                    array( 'currency' => $order->get_currency() )
            );

		    ?>
                <br><br>
		        <strong>On Delivery:</strong> <?php echo $total_price ?>
		        <br>
		        <strong>Monthly:</strong> <?php echo $monthly_price ?>
		    <?php
	    }
    }

	/**
	 * @param array $formatted_meta
	 * @param WC_Order_Item $order_item
	 *
	 * @return array
	 */
	public static function filter_order_item_formatted_meta_data($formatted_meta, $order_item) {

		if (Wiaas_Order_Item::is_standard_bundle($order_item)) {

			return $formatted_meta;
		}

		return array();
	}

	/**
	 * @param string $display_key
	 * @param object $meta
	 * @param WC_Order_Item $item
	 *
	 * @return string
	 */
	public static function order_item_display_meta_key($display_key, $meta, $item) {

		switch ($meta->key) {
			case '_wiaas_payment_type':
				return 'Payment type';

			case '_wiaas_services_extra':
				return 'Services and support price';

			case '_wiaas_recurrent_extra':
				return 'Recurrent price';

			default:
				return $display_key;
		}
	}

	/**
	 * @param string $display_value
	 * @param object $meta
	 * @param WC_Order_Item $item
	 *
	 * @return string
	 */
	public static function order_item_display_meta_value($display_value, $meta, $item) {

		switch ($meta->key) {

			case '_wiaas_services_extra':
				$order = $item->get_order();

				return wc_price(
					Wiaas_Order_Item::get_services_total($item),
					array( 'currency' => $order->get_currency())
				);

			case '_wiaas_recurrent_extra':
				$order = $item->get_order();

				return wc_price(
					Wiaas_Order_Item::get_recurrent_total($item),
					array( 'currency' => $order->get_currency())
				);

			default:
				return $display_value;
		}
	}

}

Wiaas_Admin_Orders::init();