import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { DropZoneResp } from '../models/dropzone';

import { BaseService } from './base.service';


@Injectable()
export class DropzoneService extends BaseService {
  constructor(private http: HttpClient) {
    super();
  }

  public getListOfDropZones(): Observable<Array<DropZoneResp>> {
    return this.http
      .get<Array<DropZoneResp>>(`${this.apiUrl}/DropZone`, {
        headers: this.headers
      })
      .pipe(
        map(response => {
          const details = response.map(data => new DropZoneResp(data));
          return details;
        })
      );
  }

  public SetFavoriteDropZone(selectedDz: DropZoneResp): boolean {
    selectedDz.isFavorite = true;
    this.http
      .put(`${this.apiUrl}/DropZone/${selectedDz.id}`, selectedDz, {
        headers: this.headers
      })
      .subscribe(data => console.log(data));

    return true;
  }

  public RemoveFavoriteDropZone(selectedDz: DropZoneResp): boolean {
    selectedDz.isFavorite = false;
    this.http
      .put(`${this.apiUrl}/DropZone/${selectedDz.id}`, selectedDz, {
        headers: this.headers
      })
      .subscribe(data => console.log(data));

    return true;
  }
}
