<?php
namespace Jet_Engine\Modules\Maps_Listings\Source;

class Users extends Base {

	/**
	 * Returns source ID
	 *
	 * @return string
	 */
	public function get_id() {
		return 'users';
	}

	public function get_obj_by_id( $id ) {
		return get_user_by( 'ID', $id );
	}

	public function get_field_value( $obj, $field ) {
		return get_user_meta( $obj->ID, $field, true );
	}

	public function delete_field_value( $obj, $field ) {
		delete_user_meta( $obj->ID, $field );
	}

	public function update_field_value( $obj, $field, $value ) {
		
		$hash = md5( $field );

		update_user_meta( $obj->ID, $hash . '_hash', $value['key'] );
		update_user_meta( $obj->ID, $hash . '_lat', $value['coord']['lat'] );
		update_user_meta( $obj->ID, $hash . '_lng', $value['coord']['lng'] );

	}

	public function get_failure_key( $obj ) {
		return 'User #' . $obj->ID;
	}

	public function add_preload_hooks( $preload_fields ) {

		foreach ( $preload_fields as $field ) {

			$field  = str_replace( '_custom::users::', '', $field );
			$fields = explode( '+', $field );

			if ( 1 === count( $fields ) ) {
				add_filter( 'update_user_metadata', function( $return, $user_id, $meta_key, $meta_value ) use ( $field ) {

					if ( $field === $meta_key ) {
						$this->preload( $user_id, $meta_value, $meta_key );
					}

					return $return;
				}, 10, 4 );

				add_action( 'add_user_metadata', function( $return, $user_id, $meta_key, $meta_value ) use ( $field ) {

					if ( $field === $meta_key ) {
						$this->preload( $user_id, $meta_value, $meta_key );
					}

					return $return;
				}, 10, 4 );

			} else {
				$this->field_groups[] = $fields;
			}
		}

		if ( ! empty( $this->field_groups ) ) {
			add_action( 'save_post', array( $this, 'preload_groups' ), 9999 );
		}
	}

	public function filtered_preload_fields( $field ) {
		return false !== strpos( $field, 'user::' );
	}

}
