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

import { LotInfo } from '../model/lotInfo.interface';
import { Lot } from '../model/lot.interface';

@Injectable({
    providedIn: 'root'
})
export class apiLotService {
    ServeurURL = environment.ServeurURL;
    ApiURL = this.ServeurURL+"/api";

    constructor(private http: HttpClient){}    

    // Lot
    getLotInfo(url: string): Observable<LotInfo> {
        let encodeUrl = encodeURIComponent(url);
        return this.http.get<LotInfo>(this.ApiURL+'/lot/getInfos/'+encodeUrl);
    }

    getPictures(url: string): Observable<String[]> {
        let encodeUrl = encodeURIComponent(url);
        return this.http.get<String[]>(this.ApiURL+'/lot/getPictures/'+encodeUrl);
    }

    getLotsBySale(_id: String): Observable<Lot[]> {
        return this.http.get<Lot[]>(this.ApiURL+'/lot/getLotsBySale/'+_id);
    }

    // Lot CRUD
    getLot(_id: String): Observable<Lot> {
        return this.http.get<Lot>(this.ApiURL+'/lot/lot/'+_id);
    }

    saveLot(Lot: Lot): Observable<Lot> {
        return this.http.post<Lot>(this.ApiURL+'/lot/lot', Lot);
    }

    updateLot(Lot: Lot): Observable<Lot> {
        return this.http.put<Lot>(this.ApiURL+'/lot/lot/'+Lot._id, Lot);
    }

    deleteLot(_id: String): Observable<any> {
        return this.http.delete<any>(this.ApiURL+'/lot/lot/'+_id);
    }
}