<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}


/**
 * Class Wiaas_Order
 *
 * Integrates Woocommerce order with Wiaas order
 */

class Wiaas_Order {

	private static $object_order_type = 'shop_order';

	public static function init() {

		require_once dirname( __FILE__ ) . '/order/class-wiaas-order-project.php';
		require_once dirname( __FILE__ ) . '/order/wiaas-order-functions.php';
		require_once dirname( __FILE__ ) . '/order/class-wiaas-order-item.php';

		add_filter('woocommerce_register_post_type_shop_order', array(__CLASS__, 'manage_order_settings'));

		add_filter( 'woocommerce_register_shop_order_post_statuses', array(__CLASS__, 'register_custom_order_statuses'), 10, 1);

		add_filter( 'wc_order_statuses', array(__CLASS__, 'add_custom_statuses_to_list' ), 10, 1);

		add_filter( 'bulk_actions-edit-shop_order', array(__CLASS__, 'add_custom_statuses_to_bulk_edit' ), 10, 1);

		add_filter('woocommerce_rest_check_permissions', array( __CLASS__, 'check_order_access'), 10, 4);

		add_filter('woocommerce_rest_prepare_shop_order_object', array(__CLASS__, 'transform_rest_order'), 999, 3);

		add_filter('woocommerce_rest_orders_prepare_object_query', array( __CLASS__, 'wiaas_prepare_rest_orders_query'), 10, 2);

        add_filter('woocommerce_new_order_note_data',  array( __CLASS__, 'update_new_order_comment_date'), 10, 3);

		add_filter('woocommerce_order_number', array( __CLASS__, 'format_order_number'));
	}


	/**
	 * Prefix order number
	 *
	 * @param int $number
	 *
	 * @return string
	 */
	public static function format_order_number($number) {
		return "1000000$number";
	}

	public static function get_additional_days_prior_installation($order_id){
		$order = wc_get_order($order_id);
		return $order->get_meta('_wiaas_order_additional_days_prior_installation');
	}

	public static function add_additional_tracking_info($order_id, $supplier_id){
		$suppliers = self::get_suppliers($order_id);
		foreach($suppliers as $key => $supplier){
			if ($supplier['id'] === $supplier_id){
				$suppliers[$key]['tracking_info'][] = array(
					'number' => '',
					'url' => ''
				);

				$order = wc_get_order($order_id);
				$order->update_meta_data('_wiaas_delivery_suppliers', $suppliers);
				$order->save_meta_data();
				return true;
			}
		}

		return false;
	}

	public static function save_tracking_info($order_id, $supplier_id, $tracking_index, $tracking_num, $tracking_url){
		$suppliers = self::get_suppliers($order_id);
		foreach($suppliers as $key => $supplier){
			if ($supplier['id'] === $supplier_id){
				$suppliers[$key]['tracking_info'][$tracking_index]['number'] = $tracking_num;
				$suppliers[$key]['tracking_info'][$tracking_index]['url'] = $tracking_url;
				
				
				$order = wc_get_order($order_id);
				$order->update_meta_data('_wiaas_delivery_suppliers', $suppliers);
				$order->save_meta_data();
				return true;
			}
		}

		return false;
	}

	public static function delete_tracking_info($order_id, $supplier_id, $tracking_index){
		$suppliers = self::get_suppliers($order_id);
		foreach($suppliers as $key => $supplier){
			if ($supplier['id'] === $supplier_id){
				unset($suppliers[$key]['tracking_info'][$tracking_index]);
				
				$order = wc_get_order($order_id);
				$order->update_meta_data('_wiaas_delivery_suppliers', $suppliers);
				$order->save_meta_data();
				return true;
			}
		}

		return false;
	}

	public static function save_estimated_date($order_id, $supplier_id, $date){
		$suppliers = self::get_suppliers($order_id);
		$updated = false;
		foreach($suppliers as $key => $supplier){
			if ($supplier['id'] === $supplier_id){
				$suppliers[$key]['estimated_date'] = $date;
				$updated = true;
			}
		}

		if (!$updated){
			return false;
		}

		$order = wc_get_order($order_id);
		
		$order->update_meta_data('_wiaas_delivery_suppliers', $suppliers);
		self::_update_max_and_earliest_dates($order, $suppliers);
		
		$order->save_meta_data();
		return true;
	}

	public static function save_confirmed_date($order_id, $supplier_id, $date){

		$suppliers = self::get_suppliers($order_id);
		$updated = false;

		foreach($suppliers as $key => $supplier){
			if ($supplier['id'] === $supplier_id){
				$suppliers[$key]['confirmed_date'] = $date;
				$updated = true;
				if (!$suppliers[$key]['estimated_date']){
					$suppliers[$key]['estimated_date'] = $date;
				}
			}
		}

		if (!$updated){
			return false;
		}

		$order = wc_get_order($order_id);
		
		$order->update_meta_data('_wiaas_delivery_suppliers', $suppliers);
		self::_update_max_and_earliest_dates($order, $suppliers);
		
		$order->save_meta_data();
		return true;
	}

	public static function get_order_estimated_date($order_id){
		$order = wc_get_order($order_id);
		if (!$order){
			return NULL;
		}

		return $order->get_meta('_wiaas_order_estimated_delivery_date') ?: NULL;
	}

	public static function save_order_estimated_date($order_id, $date){
		return update_post_meta($order_id, '_wiaas_order_estimated_delivery_date', $date);
	}

	public static function get_final_estimated_date($order_id){
		$order = wc_get_order($order_id);
		if (!$order){
			return 0;
		}

		return $order->get_meta('_wiaas_final_estimated_delivery_date') ?: 0;
	}

	public static function get_final_confirmed_date($order_id){
		$order = wc_get_order($order_id);
		if (!$order){
			return 0;
		}

		return $order->get_meta('_wiaas_final_confirmed_delivery_date') ?: 0;
	}

	public static function get_earliest_installation_date($order_id){
		$order = wc_get_order($order_id);
		if (!$order){
			return 0;
		}

		return $order->get_meta('_wiaas_earliest_installation_date') ?: 0;
	}


	/**
	 * Get suppliers related to order
	 */
	public static function get_suppliers($order_id){
		$order = wc_get_order($order_id);
		return $order->get_meta('_wiaas_delivery_suppliers');
	}

	/**
	 * Register additional order statuses 
	 * 
	 * @param array $order_statuses
	 * 
	 * @return array
	 */
	public static function register_custom_order_statuses($order_statuses){
		// Status must start with "wc-"
		$order_statuses['wc-open'] = array(                                            
			'label'                     => _x( 'Open', 'Order status', 'woocommerce' ),
			'public'                    => false,                                            
			'exclude_from_search'       => false,                                            
			'show_in_admin_all_list'    => true,                                         
			'show_in_admin_status_list' => true,                                         
			'label_count'               => _n_noop( 'Open <span class="count">(%s)</span>', 'Open <span class="count">(%s)</span>', 'woocommerce' ),                                       
			);      
			return $order_statuses;
	}

	/**
	 * display custom wiaas statuses in order status dropdown
	 * 
	 * @param array $order_statuses
	 * 
	 * @return array
	 */
	public static function add_custom_statuses_to_list($order_statuses){
		$order_statuses['wc-open'] = _x( 'Open', 'Order status', 'woocommerce' );       
		return $order_statuses;
	}

	/**
	 * display custom wiaas statuses in bulk actions
	 * 
	 * @param array $bulk_actions
	 * 
	 * @return array
	 */
	public static function add_custom_statuses_to_bulk_edit($bulk_actions){
		// Note: "mark_" must be there instead of "wc"
		$bulk_actions['mark_open'] = 'Change status to open';
		return $bulk_actions;
	}

	/**
	 * Update `shop_order` post type settings before creation to enable better order management for wiaas
	 *
	 * @param array $args
	 *
	 * @return array
	 */
	public static function manage_order_settings($args) {
		// show orders in backend menu
		$args['show_in_menu'] = true;

		//set icon
		$args['menu_icon'] = 'dashicons-cart';

		// set capabilities
		$args['capabilities'] = array(
			'edit_post'          => 'edit_shop_order',
			'read_post'          => 'read_shop_order',
			'delete_post'        => 'delete_shop_order',
			'edit_posts'         => 'edit_shop_orders',
			'edit_others_posts'  => 'edit_others_shop_orders',
			'publish_posts'      => 'publish_shop_orders',
			'read_private_posts' => 'read_private_shop_orders',
			'create_posts'       => 'create_shop_orders', // use `create_shop_orders` instead of `edit_shop_orders`
		);

		return $args;
	}

	public static function update_new_order_comment_date($comment_data, $order_data) {
	    $user = wp_get_current_user();

        $comment_data['comment_author'] = $user->display_name;
        $comment_data['comment_author_email'] = $user->user_email;

        return $comment_data;
    }

	/**
	 * Checks if current user has access to requested order/{orderId} via woocommerce REST API.
	 * Endpoint `/orders` is filtered correctly by groups, but endpoint `/orders/{orderId}` will return order even if
	 * user does not have access to it.
	 * Groups has general support for this using rest_prepare_{$post_type} but woocommerce api does not
	 * use this filter anymore. So we will just call the same function just with woocommerce filter.
	 *
	 * @param $permission
	 * @param $context
	 * @param $object_id
	 * @param $post_type
	 * @return bool
	 */
	public static function check_order_access($permission, $context, $object_id, $post_type) {
		if ($post_type === self::$object_order_type && $object_id !== 0) {
			return Groups_Post_Access::user_can_read_post($object_id);
		}
		return $permission;
	}

	/**
	 * Handles custom wiaas arguments to woocommerce orders api endpoint `wc/v2/orders`
	 * @param $args
	 * @param $request
	 *
	 * @return mixed
	 */
	public static function wiaas_prepare_rest_orders_query($args, $request) {

		# Handle wiaas_is_active flag
		if (isset($request['wiaas_is_active'])) {
			if ($request['wiaas_is_active'] === '1') {
				$args['post_status'] = array('wc-open', 'wc-processing');
			}
			if ($request['wiaas_is_active'] === '0') {
				$args['post_status'] = array('wc-completed', 'wc-cancelled');
			}
		}

		return $args;
	}

	/**
	 * Apply wiaas custom transformation on retrieved JSON order object
	 *
	 * @param $response
	 * @param $order
	 * @param $request
	 *
	 * @return mixed
	 */
	public static function transform_rest_order($response, $order, $request) {
		$data = $response->get_data();

		# apply overrides

		$data = self::_append_customer_info($data, $order, $request);

		$data = self::_append_commercial_lead_info($data, $order, $request);

		$data = self::_append_wiaas_order_details($data, $order, $request);

		$data = self::_append_packages($data, $order, $request);

		if (isset($request['id'])) {

			$data = self::_append_order_process($data, $order, $request);

			$data = self::_append_order_comments($data, $order, $request);

			$data = self::_append_documents($data, $order, $request);
		}

		$response->set_data($data);

		return $response;
	}

	public static function get_order_customer_full_name($order_id){
        $order = wc_get_order($order_id);

        $customer_user_id = $order->get_customer_id();

        $customer = get_userdata($customer_user_id);

        return $customer->last_name . ' ' . $customer->first_name;
    }

    public static function get_order_commercial_lead_name($order_id){
        $order = wc_get_order($order_id);

        $commercial_lead_org_id = $order->get_meta('_wiaas_commercial_lead_id', true);
        $commercial_lead_organization_info = wiaas_get_organization_info($commercial_lead_org_id);

        return $commercial_lead_organization_info['name'];
    }

	public static function set_order_vat($order_id, $vat_code) {
		add_post_meta($order_id, '_wiaas_vat_code', $vat_code);
	}

	public static function get_order_vat($order_id) {
		return get_post_meta($order_id, '_wiaas_vat_code', true);
	}

	public static function set_order_company($order_id, $company_name) {
		add_post_meta($order_id, '_wiaas_company_name', $company_name);
	}

	public static function get_order_company($order_id) {
		return get_post_meta($order_id, '_wiaas_company_name', true);
	}

	public static function set_order_reference($order_id, $reference) {
		add_post_meta($order_id, '_wiaas_reference', $reference);
	}

	public static function get_order_reference($order_id) {
		return get_post_meta($order_id, '_wiaas_reference', true);
	}

	public static function set_order_tender($order_id, $tender) {
		add_post_meta($order_id, '_wiaas_tender', $tender);
	}

	public static function get_order_tender($order_id) {
		return get_post_meta($order_id, '_wiaas_tender', true);
	}

	public static function get_order_country_code($order_id) {

		$order = wc_get_order($order_id);

		$code  = $order->get_meta('_wiaas_country_code');

		if (empty($code)) {
			$code = Wiaas_Countries::get_available_country_code_by_currency($order->get_currency());
			$order->add_meta_data('_wiaas_country_code', $code);
			$order->save_meta_data();
		}

		return $code;
	}

	/**
	 * Retrieve customer organization id for order
	 *
	 * @param int $order_id
	 *
	 * @return int|null
	 */
	public static function get_order_customer_organization_id($order_id) {
		$order = wc_get_order($order_id);

		$customer_organization_id = $order->get_meta('_wiaas_customer_id', true);

		if (empty($customer_organization_id)) {

			$customer_organization_id = wiaas_get_user_organization_id($order->get_customer_id());
		}

		return $customer_organization_id;
	}

	/**
	 * Retrieve customer organization info from order
	 *
	 * @param int $order_id
	 *
	 * @return array|null
	 */
	public static function get_customer_organization_info($order_id) {
		$order = wc_get_order($order_id);

		$customer_organization_info = $order->get_meta('_wiaas_customer_info', true);

		$customer_organization_id = self::get_order_customer_organization_id($order_id);

		if ( empty($customer_organization_info) && ! empty( $customer_organization_id) ) {

			$customer_organization_info = wiaas_get_organization_info($customer_organization_id);
		}

		if ( ! empty($customer_organization_info) ) {

			$customer_organization_info['id'] = $customer_organization_id;
		}

		return ! empty($customer_organization_info) ? $customer_organization_info : null;
	}

	/**
	 * PRIVATE
	 */

	private static function _update_max_and_earliest_dates($order, $suppliers){
		$max_estimated_date = 0;
		$max_confirmed_date = 0;
		$earliest_installation_date = 0;
		$missing_estimated = false;

		foreach($suppliers as $supplier) {

			if (! empty($supplier['confirmed_date']) ) {

				$max_confirmed_date = max($max_confirmed_date, $supplier['confirmed_date']);
			} else {

				$missing_confirmed = true;
			}

			if (! empty($supplier['estimated_date']) ) {

				$max_estimated_date = max($max_estimated_date, $supplier['estimated_date']);

			} else {

				$missing_estimated = true;
				$earliest_installation_date = 0;
			}
		}

		if (! $missing_estimated) {

			$earliest_installation_date = max($max_estimated_date, $max_confirmed_date);
			$earliest_installation_date = strtotime('+' . self::get_additional_days_prior_installation($order->id) . ' weekdays', $earliest_installation_date);
		}

		$order->update_meta_data('_wiaas_final_confirmed_delivery_date', $max_confirmed_date);
		$order->update_meta_data('_wiaas_final_estimated_delivery_date', $max_estimated_date);
		$order->update_meta_data('_wiaas_earliest_installation_date', $earliest_installation_date);
	}

	/**
	 * Append specific wiaas order details, like reference
	 * @param $data
	 * @param $order
	 * @param $request
	 */
	private static function _append_wiaas_order_details($data, $order, $request) {
		$data['reference'] = self::get_order_reference($order->get_id());
		$data['tender'] = self::get_order_tender($order->get_id());
		$data['vat'] = self::get_order_vat($order->get_id());
		$data['company_name'] = self::get_order_company($order->get_id());

		$data['project_name'] = Wiaas_Order_Project::get_project_name_for_order($order->get_id());

		return $data;
	}
	/**
	 * Appends additional wiaas customer lead info to order json response
	 * @param $data
	 * @param WC_Order $order
	 * @param $request
	 *
	 * @return mixed
	 */
	private static function _append_commercial_lead_info($data, $order, $request) {

		$commercial_lead_org_id = $order->get_meta('_wiaas_commercial_lead_id', true);

		$commercial_lead_organization_info = $order->get_meta('_wiaas_commercial_lead_info', true);

		if (! empty($commercial_lead_org_id) && empty($commercial_lead_organization_info)) {
			$commercial_lead_organization_info = wiaas_get_organization_info($commercial_lead_org_id);

			$data['commercial_lead'] = array(
				'id' => $commercial_lead_org_id,
				'name' => $commercial_lead_organization_info['name'],
				'phone' => $commercial_lead_organization_info['phone'],
				'email' => $commercial_lead_organization_info['email']
			);
		}

		if (!empty($commercial_lead_organization_info)) {
			$commercial_lead_organization_info['id'] = $commercial_lead_org_id;

			$data['commercial_lead'] = $commercial_lead_organization_info;
		}

		return $data;
	}

	/**
	 * Appends additional wiaas customer info to order json response
	 * @param $data
	 * @param WC_Order $order
	 * @param $request
	 *
	 * @return mixed
	 */
	private static function _append_customer_info($data, $order, $request) {

		$customer_organization_id = $order->get_meta('_wiaas_customer_id', true);

		$customer_user_id = $order->get_customer_id();

		if (empty($customer_organization_id)) {

			$customer_organization_id = wiaas_get_user_organization_id($customer_user_id);
		}

		$customer_organization_info = $order->get_meta('_wiaas_customer_info', true);

		if ( !empty($customer_organization_id) && empty($customer_organization_info) ) {

			$customer_organization_info = wiaas_get_organization_info($customer_organization_id);

			$customer_organization_info['id'] = $customer_organization_id;
		}

		$data['customer'] = $customer_organization_info;

		$data['is_my_order'] = $customer_user_id === get_current_user_id();

		return $data;
	}

	/**
	 * Filters only package product lines and appends additional wiaas products info to order json response
	 * @param $data
	 * @param $order
	 * @param $request
	 *
	 * @return mixed
	 */
	private static function _append_packages($data, $order, $request) {
		$line_items = array();

		$order_items = $order->get_items( 'line_item' );

		foreach ($data['line_items'] as $index => $product_line) {

			$item = $order->get_item($product_line['id']);

			// add only product lines that represent product bundles
			if (isset($item['wiaas_standard_package'])) {
				# get payment type info
				$product_line['payment_type'] = $item['wiaas_payment_type'];
				$product_line['service_price'] = floatval($item['wiaas_services_extra']);
				$product_line['service_contract_period'] = floatval($item['wiaas_service_contract_period']);
				$product_line['max_contract_period'] = floatval($item['wiaas_max_contract_period']);
				$product_line['period_unit'] = $item['wiaas_period_unit'];
				$product_line['recurring_price'] = floatval($item['wiaas_recurrent_extra']);
				$product_line['pay_period'] = floatval($item['wiaas_pay_period']);

				# collect status from  order
				if ($data['status'] === 'completed') {
					$product_line['status'] = 'production';
				} else if ($data['status'] === 'cancelled') {
					$product_line['status'] = 'cancelled';
				} else {
					$product_line['status'] = 'processing';
				}

				$product_line['short_desc'] = $product_line['status'];

				# collect completion data from order
				$product_line['date_completed'] = $data['date_completed'];

				if (isset($request['id'])) {
					// collect package addons
					$product_line['additional_packages'] = array();
					$addon_items = wiaas_get_order_item_addons($order_items, $item);
					foreach ($addon_items as $addon_item) {
						$product_line['additional_packages'][] = array(
							'id' => $addon_item->get_id(),
							'name' => $addon_item->get_name(),
						);
					}

					// collect package options
					$product_line['options'] = array();
					$option_items = wiaas_get_order_item_options($order_items, $item);
					foreach ($option_items as $option_item) {
						$product_line['options'][] = array(
							'id' => $option_item->get_id(),
							'name' => $option_item->get_name(),
							'group_name' => $option_item['wiaas_option_group_name']
						);
					}
				}

				// installation date
				if (! empty($item['wiaas_installation_date'])) {

					$product_line['installation_date'] = $item['wiaas_installation_date'];
				}

				$line_items[] = $product_line;
			}
		}
		$data['line_items'] = $line_items;

		$total_recurring_price = 0;

		foreach ($order_items as $order_item) {
			if (isset($order_item['wiaas_standard_package'])) {
				$total_recurring_price += floatval($order_item['quantity']) * floatval($order_item['wiaas_services_extra']) +
				                          floatval($order_item['quantity']) * floatval($order_item['wiaas_recurrent_extra']);
			}
		}

		$data['recurring_price'] = $total_recurring_price;

		return $data;
	}

	/**
	 * Append order delivery process info if single order is requested
	 * @param $data
	 * @param $order
	 * @param $request
	 *
	 * @return mixed
	 */
	private static function _append_order_process($data, $order, $request) {

		# if this is response to `/order/[id]`
		if (isset($request['id'])) {
			$data['delivery-process'] = Wiaas_Delivery_Process::get_order_delivery_process($order->get_id());
		}

		return $data;
	}

    /** Append order comments if single order is requested
     * @param $data
     * @param $order
     * @param $request
     */
	private static function _append_order_comments($data, $order, $request) {

        if (isset($request['id'])) {
            $current_user = wp_get_current_user();

            $comments = $order->get_customer_order_notes();

            $data['comments'] = array();

            foreach ($comments as $comment) {
                $data['comments'][] = array(
                    'id' => $comment->comment_ID,
                    'content' => $comment->comment_content,
                    'username' => $comment->comment_author,
                    'date' => $comment->comment_date,
                    'is_owner' => $comment->comment_author === $current_user->display_name,
                );
            }
        }

	    return $data;
	}
	
	/** Append downloadable documents from order products if single order is requested
     * @param $data
     * @param $order
     * @param $request
     */
	private static function _append_documents($data, $order, $request) {

		// Go trough available line items and append documents for wiaas standard packages line items
		foreach ($data['line_items'] as $index => $product_line) {
			$order_item = $order->get_item($product_line['id']);

			if (wiaas_is_order_item__standard_package($order_item)) {
				$documents = wiaas_get_standard_package_order_item_documents($order, $product_line['id']);

				$data['line_items'][$index] ['documents'] = $documents;
			}

		}

		$data['documents'] = wiaas_get_order_other_documents($order->get_id());

	    return $data;
    }
}

Wiaas_Order::init();
