<?php

defined( 'ABSPATH' ) || exit;

/**
 * Class Wiaas_Customer
 */
class Wiaas_Customer {

    public function __construct() {
		include_once dirname( __FILE__ ) . '/../class-wiaas-countries.php';
    }
    
    public static function get_customer_info($customer_id){
        $user = get_userdata($customer_id);

        $organization_id = wiaas_get_user_organization_id($customer_id);

        $result = array(
			'id' => $customer_id,
			'company_id' => $organization_id,
			'is_company_admin' => self::get_customer_company_admin_status($customer_id),
			'mail' => $user->user_email,
			'name' => $user->first_name . ' ' . $user->last_name,
			'phone' => self::get_customer_phone_number($customer_id),
			'company_name' => wiaas_get_organization_name($organization_id),
			'vat_code' => self::get_customer_vat_code($customer_id),
			'billing_addresses' => self::get_customer_billing_addresses($customer_id),
			'profile_addresses' => self::get_customer_profile_addresses($customer_id),
			'user_type' => $user->roles
		);

		return $result;
    }

	/**
	 * Retrieve available shops for customer
	 *
	 * @return array of available shops for customer
	 */
    public static function get_customer_shops() {

    	$customer_id = wiaas_get_current_user_organization_id();

    	return Wiaas_Shop_DB::get_customer_shops($customer_id);
    }

    public static function get_customer_profile_addresses($customer_id){
        return get_user_meta($customer_id, 'profile_addresses', true) ?: [];
    }

	/**
	 * Get customer profile address by id
	 *
	 * @param int $customer_id
	 * @param int $address_id
	 *
	 * @return array|null
	 */
    public static function get_customer_profile_address($customer_id, $address_id) {
	    $profile_addresses = self::get_customer_profile_addresses($customer_id);
	    $index = array_search($address_id, array_column($profile_addresses, 'id'));
	    if (is_numeric($index)) {
		    return $profile_addresses[$index];
	    }
	    return null;
    }

    public static function get_customer_billing_addresses($customer_id){
        return get_user_meta($customer_id, 'billing_addresses', true) ?: [];
    }

	/**
	 * Get customer billing address by id
	 *
	 * @param int $customer_id
	 * @param int $address_id
	 *
	 * @return array|null
	 */
    public static function get_customer_billing_address($customer_id, $address_id) {
    	$billing_addresses = self::get_customer_billing_addresses($customer_id);
    	$index = array_search($address_id, array_column($billing_addresses, 'id'));
    	if (is_numeric($index)) {
    		return $billing_addresses[$index];
	    }
	    return null;
    }

    public static function get_customer_vat_code($customer_id){
        return get_user_meta($customer_id, 'vat_code', true) ?: '';
    }

    public static function get_customer_company_name($customer_id){
        return get_user_meta($customer_id, 'company_name', true) ?: '';
    }

    public static function get_customer_company_admin_status($customer_id){
        return 1; //TODO: don't hardcode this
    }

    public static function get_customer_phone_number($customer_id){
        return get_user_meta($customer_id, 'phone', true) ?: '';
    }

    public static function update_customer_profile_info($customer_id, $first_name, $last_name, $phone){
        $user = array(
            'ID' => $customer_id,
            'first_name' => $first_name,
            'last_name' => $last_name
        );

        if (is_wp_error(wp_update_user($user))){
            return false;
        }
        update_user_meta( $customer_id, 'phone', $phone);

        return true;
    }

    public static function update_customer_company_info($customer_id, $company_name, $vat_code){
        $result1 = update_user_meta( $customer_id, 'company_name', $company_name);
        $result2 = update_user_meta( $customer_id, 'vat_code', $vat_code );
        return $result1 || $result2;
    }
    
    public static function update_customer_billing_addresses($customer_id, $new_address){
			if (!self::validate_address($new_address)){
				return false;
			}
			$billing_addresses = self::get_customer_billing_addresses($customer_id);
			
			if ($new_address->id){
				$updated = array(
					'id' => $new_address->id,
					'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected),
					'delivery_mail' => $new_address->delivery_mail,
					'id_country_selected' => $new_address->id_country_selected,
					'city' => $new_address->city,
					'detailed_address' => $new_address->detailed_address,
					'zip_code' => $new_address->zip_code,
					'first_name' => $new_address->first_name,
					'last_name' => $new_address->last_name,
					'invoice_mail' => $new_address->invoice_mail
				);
				foreach($billing_addresses as $key => $address){
					if ($address['id'] === $new_address->id){
						$billing_addresses[$key] = $updated;
						break;
					}
				}
			}else{
				$new_billing_address = array(
					'id' => time(),
					'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected),
					'delivery_mail' => $new_address->delivery_mail,
					'id_country_selected' => $new_address->id_country_selected,
					'city' => $new_address->city,
					'detailed_address' => $new_address->detailed_address,
					'zip_code' => $new_address->zip_code,
					'first_name' => $new_address->first_name,
					'last_name' => $new_address->last_name,
					'invoice_mail' => $new_address->invoice_mail
				);
	
				array_push($billing_addresses, $new_billing_address);
			}
			
			return update_user_meta( $customer_id, 'billing_addresses', $billing_addresses);
    }

    public static function update_customer_profile_addresses($customer_id, $new_address){
        if (!self::validate_address($new_address)){
            return false;
        }
        $profile_addresses = self::get_customer_profile_addresses($customer_id);
        
        if ($new_address->id){
            $updated = array(
                'id' => $new_address->id,
                'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected),
                'delivery_mail' => $new_address->delivery_mail,
                'id_country_selected' => $new_address->id_country_selected,
                'city' => $new_address->city,
                'detailed_address' => $new_address->detailed_address,
                'zip_code' => $new_address->zip_code,
                'first_name' => $new_address->first_name,
                'last_name' => $new_address->last_name,
                'invoice_mail' => $new_address->invoice_mail
            );
            foreach($profile_addresses as $key => $address){
                if ($address['id'] === $new_address->id){
                    $profile_addresses[$key] = $updated;
                    break;
                }
            }
        }else{
            $new_delivery_address = array(
                'id' => time(),
                'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected),
                'delivery_mail' => $new_address->delivery_mail,
                'id_country_selected' => $new_address->id_country_selected,
                'city' => $new_address->city,
                'detailed_address' => $new_address->detailed_address,
                'zip_code' => $new_address->zip_code,
                'first_name' => $new_address->first_name,
                'last_name' => $new_address->last_name,
                'invoice_mail' => $new_address->invoice_mail
            );

            array_push($profile_addresses, $new_delivery_address);
        }
        
        return update_user_meta( $customer_id, 'profile_addresses', $profile_addresses);
    }

    public static function delete_customer_profile_address($customer_id, $address_id){
        $profile_addresses = self::get_customer_profile_addresses($customer_id);
        $counter = 0;
        foreach($profile_addresses as $key => $address){
            if ($address['id'] == $address_id){
                array_splice($profile_addresses, $counter, 1);
                break;
            }
            $counter++;
        }
        return update_user_meta( $customer_id, 'profile_addresses', $profile_addresses);
    }

    public static function delete_customer_billing_address($customer_id, $address_id){
        $billing_addresses = self::get_customer_billing_addresses($customer_id);
        $counter = 0;
        foreach($billing_addresses as $key => $address){
            if ($address['id'] == $address_id){
                array_splice($billing_addresses, $counter, 1);
                break;
            }
            $counter++;
        }
        return update_user_meta( $customer_id, 'billing_addresses', $billing_addresses);
    }

    /**
	 * Check if address is valid
	 *
	 * @param $address
	 *
	 * @return bool
	 */
	private static function validate_address($address){
		if (empty($address->city)){
			return false;
		}

		if (empty($address->detailed_address)){
			return false;
		}

		return is_numeric($address->zip_code);
	}


}