import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable, inject } from '@angular/core'
import { RestExtractor, RestPagination, RestService } from '@app/core'
import { SortMeta } from 'primeng/api'
import { Observable } from 'rxjs'
import { catchError } from 'rxjs/operators'
import { environment } from 'src/environments/environment'

export interface LiveVideoCreate {
  title: string
  targetCamera: string
  startTime: string
  endTime: string
  status?: 'scheduled' | 'cancelled' | 'completed'
}

export interface LiveVideoUpdate {
  title?: string
  targetCamera?: string
  startTime?: string
  endTime?: string
  status?: 'scheduled' | 'cancelled' | 'completed'
}

@Injectable({
  providedIn: 'root'
})
export class LiveApiService {

  private authHttp = inject(HttpClient)
  private restExtractor = inject(RestExtractor)
  private restService = inject(RestService)

private static BASE_LIVE_VIDEO_URL =
  environment.apiUrl + '/api/v1/live-videos'

  // -------------------------------
  // List live videos
  // -------------------------------
  listLiveVideos (options?: {
    pagination?: RestPagination
    sort?: SortMeta
    targetCamera?: string
    status?: 'scheduled' | 'cancelled' | 'completed'
  }): Observable<any> {

    let params = new HttpParams()

    if (options?.pagination) {
      params = this.restService.addRestGetParams(params, options.pagination)
    }

    if (options?.sort) {
      params = this.restService.addRestGetParams(params, undefined, options.sort)
    }

    if (options?.targetCamera) {
      params = params.set('targetCamera', options.targetCamera)
    }

    if (options?.status) {
      params = params.set('status', options.status)
    }

    return this.authHttp
      .get<any>(LiveApiService.BASE_LIVE_VIDEO_URL, { params })
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  // -------------------------------
  // Get single live video
  // -------------------------------
  getLiveVideo (videoId: number): Observable<any> {

    const url = `${LiveApiService.BASE_LIVE_VIDEO_URL}/${videoId}`

    return this.authHttp
      .get<any>(url)
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  // -------------------------------
  // Create live video
  // -------------------------------
  createLiveVideo (video: LiveVideoCreate | FormData): Observable<any> {

    return this.authHttp
      .post<any>(LiveApiService.BASE_LIVE_VIDEO_URL, video)
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  // -------------------------------
  // Update live video
  // -------------------------------
  updateLiveVideo (videoId: number, video: LiveVideoUpdate | FormData): Observable<any> {

    const url = `${LiveApiService.BASE_LIVE_VIDEO_URL}/${videoId}`

    return this.authHttp
      .put<any>(url, video)
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  // -------------------------------
  // Delete live video
  // -------------------------------
  deleteLiveVideo (videoId: number): Observable<void> {

    const url = `${LiveApiService.BASE_LIVE_VIDEO_URL}/${videoId}`

    return this.authHttp
      .delete<void>(url)
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }
}
