import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Observable } from "rxjs";

const BASE_URL = 'https://kapi.absys.ninja/hemat';

@Injectable({
  providedIn: "root",
})
export class DeviceService {
  constructor(private http: HttpClient) {}

  getDeviceData(id, category, status): Observable<any> {
    const endpoint = `/devices`;
    const params = new URLSearchParams({
      page: "1",
      limit: "100",
      building_id: id ? id : "0",
      category_id: category ? category : "0",
      status_id: status ? status : "0",
      room_id: "0"
    });
    // if (category) {
    //   params.append("category_id", category);
    // }
    // if (status) {
    //   params.append("status_id", status);
    // }
    const url = `${BASE_URL}${endpoint}?${params.toString()}`;
    const headers = new HttpHeaders({
      "Content-Type": "application/json",
      "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
    });
    return this.http.get<any>(url, { headers });
  }

  getDeviceDataById(id): Observable<any> {
    const endpoint = `/devices`;
    const params = new URLSearchParams({
      page: "1",
      limit: "100",
      building_id: id
    });
    const url = `${BASE_URL}${endpoint}?${params.toString()}`;
    const headers = new HttpHeaders({
      "Content-Type": "application/json",
      "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
    });
    return this.http.get<any>(url, { headers });
  }

  getDeviceLIst(): Observable<any> {
    const endpoint = `/devices`;
    const url = `${BASE_URL}${endpoint}/list`;
    const headers = new HttpHeaders({
      "Content-Type": "application/json",
      "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
    });
    return this.http.get<any>(url, { headers });
  }

  getSyncDeviceData(): Observable<any> {
    const endpoint = `/devices`;
    const url = `${BASE_URL}${endpoint}/Synchronization`;
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      'x-api-key': 'j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT'
    });
    return this.http.get<any>(url, { headers });
  }

  deviceSwitch(data): Observable<any> {
    const endpoint = `/devices`;
    const url = `${BASE_URL}${endpoint}/device-switch`;
    const headers = new HttpHeaders({
      "Content-Type": "application/json",
      "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
    });
    return this.http.post<any>(url, data, { headers });
  }

  getStreamUrl(id): Observable<any> {
    const endpoint = `/devices`;
    const url = `${BASE_URL}${endpoint}/live-stream?device_id=${id}`;
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      'x-api-key': 'j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT'
    });
    return this.http.get<any>(url, { headers });
  }
}
