import { Injectable } from '@angular/core';
import {HttpClient, HttpRequest, HttpEvent} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import { environment } from '../../../environments/environment';
import { User } from '../../core/entities';

const IMAGES_URL = environment.apiUrl + '/api/images';
const ACCOUNT_URL = environment.apiUrl + '/api/account';

@Injectable()
export class ProfilEditionService {
	constructor(private http: HttpClient) {}

	uploadAvatarPicture(file: File): Observable<HttpEvent<{}>> {
		const formData: FormData = new FormData();

		formData.append('file', file);

		return this.http.request(new HttpRequest(
			'POST', IMAGES_URL + '/uploadAvatar', formData, {
				reportProgress: true,
				responseType: 'text'
			}
		));
	}

	updateUser(user: User): Observable<any> {
		return this.http.put<any>(`${ACCOUNT_URL}/`, user);
	}
}
