import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { Pump } from 'src/app/models/pump';
import { Sensor, SensorWithStatus } from 'src/app/models/sensor';
import { Log } from 'src/app/models/log';
import { Router } from '@angular/router';
import { PumpWithStatus } from 'src/app/models/pump';
import { PumpStatus } from 'src/app/models/pumpStatus';
import { SensorStatus } from 'src/app/models/SensorStatus';

@Injectable({
  providedIn: 'root'
})
export class StatusService {
  url = environment.url;

  constructor(private httpClient: HttpClient, private router: Router) { }

  getPumpsWithStatus(): Observable<PumpWithStatus[]> {
    return this.httpClient.get<PumpWithStatus[]>(this.url + '/Pump/getPumpsWithStatus');
  }

  getSensorsWithStatus(): Observable<SensorWithStatus[]> {
    return this.httpClient.get<SensorWithStatus[]>(this.url + '/Sensor/getSensorsWithStatus');
  }

  postLog(log: Log): Observable<Log> {
    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json; charset=utf-8');

    return this.httpClient.post<Log>(this.url + '/Log/addLog', log, {headers: headers});
  }

  postPumpStatus(pumpStatus: PumpStatus): Observable<PumpStatus> {
    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json; charset=utf-8');

    return this.httpClient.post<PumpStatus>(this.url + '/PumpStatus/addPumpStatus', pumpStatus, {headers: headers});
  }

  putPumpStatus(pumpStatus: PumpStatus, id: number): Observable<PumpStatus> {
    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json; charset=utf-8');

    return this.httpClient.put<PumpStatus>(this.url + '/PumpStatus/changePumpStatus/' + id, pumpStatus, {headers: headers});
  }

  postSensorStatus(sensorStatus: SensorStatus): Observable<Log> {
    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json; charset=utf-8');

    return this.httpClient.post<Log>(this.url + '/SensorStatus/addSensorStatus', sensorStatus, {headers: headers});
  }

  putSensorStatus(sensorStatus: SensorStatus, id: number): Observable<SensorStatus> {
    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json; charset=utf-8');

    return this.httpClient.put<SensorStatus>(this.url + '/SensorStatus/changeSensorStatus/' + id, sensorStatus, {headers: headers});
  }

}
