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

import { TunnelResp } from "../models/tunnel";

import { BaseService } from "./base.service";
import { CacheApiKey } from "../models/cache-api-key.enum";

@Injectable()
export class TunnelService extends BaseService {

  constructor(private http: HttpClient) {
    super();
  }

  public getListOfTunnels(): Observable<Array<TunnelResp>> {
    let callToApi = this.http.get<Array<TunnelResp>>(`${this.apiUrl}/Tunnel`, { headers: this.headers });
    return this.serviceCacheApi.get<Array<TunnelResp>>(CacheApiKey.Gear, callToApi);
  }

  public getById(id: number) : Observable<TunnelResp> {
    return this.serviceCacheApi.getByKey<Array<TunnelResp>>(CacheApiKey.Tunnel)
                               .pipe(map(data => { 
                                 return data.find(f => f.id === id);
                               }));
  }

  public getFromCache(): Observable<Array<TunnelResp>> {
    return this.serviceCacheApi.getByKey<Array<TunnelResp>>(CacheApiKey.Tunnel);
  }
}
