import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs";
import {map} from "rxjs/operators";
import * as moment from 'moment';
import * as _ from 'underscore'

@Injectable({providedIn: "root"})
export class BankService {

  constructor(private httpClient: HttpClient) {

  }

  getNextDemurrage(): Observable<DemurrageInfo> {
    return this.httpClient.get("/api/bank/demurrage/next").pipe(map((res: any) => <DemurrageInfo[]>res)).pipe(map(r => _.first(r))).pipe(map((di: any) => new DemurrageInfo(moment(di.dt).toDate())))
  }

}

export class DemurrageInfo {
  constructor(public date: Date) {
  }

  getValue(): number {
    let x = moment(this.date)
    let n = moment.now()
    let t = this.getTotal()
    let dt = x.diff(n, 'seconds')
    let ret = Math.round(100 * dt / t)
    return 100 - ret
  }

  getTotal(): number {
    return 7 * 24 * 60 * 60;
  }
}
