import { Injectable } from '@angular/core';
import {Auth} from "../Interfaces/auth.interface";
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {BehaviorSubject, Observable, Subject} from "rxjs";
import {Task} from "../Interfaces/task.interface";


const httpOptionAuth: {headers: HttpHeaders} = {
  headers: new HttpHeaders({
    Accept: "application/vnd.api+json",
    "Content-Type": "application/vnd.api+json"
  }),
};
const httpOptionBearer: {headers: HttpHeaders} = {
  headers: new HttpHeaders({
    Authorization: `Bearer ${localStorage.getItem("token")}`,
    Accept: "application/vnd.api+json",
    "Content-Type": "application/vnd.api+json"
  }),
};

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  hasLoggedInSub: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false)

  constructor(private http: HttpClient) { }

  adminLogin(creds: Auth): Observable<any>{
    return this.http.post("https://lucky-tasks.vercel.app/api/v1/login", creds, httpOptionAuth)
  }

  setLoggedInSubj(value: boolean): void{
    this.hasLoggedInSub.next(value)
  }

  getAllCards(): Observable<any>{
    return this.http.get<any>("https://lucky-tasks.vercel.app/api/v1/cards", {
      headers: new HttpHeaders({
        Authorization: `Bearer ${localStorage.getItem("token")}`,
        Accept: "application/vnd.api+json",
        "Content-Type": "application/vnd.api+json"
      }),
    })
  }

  storeCard(data: Task): Observable<any>{
    return this.http.post("https://lucky-tasks.vercel.app/api/v1/cards", data, httpOptionBearer)
  }

  deleteCard(index: number): Observable<any>{
    return this.http.delete(`https://lucky-tasks.vercel.app/api/v1/cards/${index}`, httpOptionBearer)
  }

  getOneCard(index: number): Observable<any>{
    return this.http.get(`https://lucky-tasks.vercel.app/api/v1/cards/${index}`, httpOptionBearer)
  }

  editCard(index: number, card: Task): Observable<any>{
    return this.http.patch(`https://lucky-tasks.vercel.app/api/v1/cards/${index}`, card, httpOptionBearer)
  }

  getTodaysTasks(): Observable<any>{
    return this.http.get(`https://lucky-tasks.vercel.app/api/v1/cards/today`, httpOptionAuth)
  }
}
