import { Injectable, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { delay, Observable } from 'rxjs';
import { TokenStorageService } from './token.service';
const API_URL = 'https://gruppe1.testsites.info/api/';
@Injectable({
  providedIn: 'root',
})
export class DataService {
  /**
   * @param  {HttpClient} privatehttp
   * @param  {TokenStorageService} privatetokenStorage
   */
  constructor(
    private http: HttpClient,
    private tokenStorage: TokenStorageService
  ) {}

  /**
   * Function to get all portfolio data
   * @returns Observable
   */
  public getStockData(): Observable<any> {
    return this.http.get(API_URL + 'portfolio', {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        Authorization: 'Bearer ' + this.tokenStorage.getToken(),
      }),
      responseType: 'text',
    });
  }

  /**
   * Function to get all transaction data
   * @returns Observable
   */
  public getTransactionData(): Observable<any> {
    return this.http.get(API_URL + 'transactions', {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        Authorization: 'Bearer ' + this.tokenStorage.getToken(),
      }),
      responseType: 'text',
    });
  }

  /**
   * Function to create a transaction
   * @param  {string} symbol
   * @param  {Date} time
   * @param  {number} count
   * @param  {number} price
   * @returns Observable
   */
  public createTransaction(
    comment: string,
    isin: string,
    time: string,
    count: number,
    price: number
  ): Observable<any> {
    time = time + 'T12:00:00.000Z';
    price.toFixed(2);
    return this.http.post(
      API_URL + 'transaction',
      {
        comment,
        count,
        isin,
        price,
        time,
      },
      {
        headers: new HttpHeaders({
          'Content-Type': 'application/json',
          Authorization: 'Bearer ' + this.tokenStorage.getToken(),
        }),
      }
    );
  }

  /**
   * Function to get all keywords
   * @returns Observable
   */
  public getKeywords(): Observable<any> {
    return this.http.get(API_URL + 'keywords', {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        Authorization: 'Bearer ' + this.tokenStorage.getToken(),
      }),
      responseType: 'text',
    });
  }
}
