import {HttpClient} from '@angular/common/http';
import {AuthService} from './auth.service';
import {Observable} from 'rxjs';
import {LoanModel} from '../models/loan.model';
import {Injectable} from '@angular/core';
import {RewardModel} from '../models/reward.model';
import {ClonerService} from './clone.service';
import {PayInModel} from '../models/pay-in.model';
import {PayInModelEx} from '../models/pay-in-ex.model';

@Injectable()
export class LoanSingleService {

  constructor(private http: HttpClient, private auth: AuthService, private dcs: ClonerService ){ }

  getLoan(loanId: string): Observable<LoanModel> {
    return this.http.get<LoanModel>(this.auth.getUrl('loan/' + loanId));
  }

  public updateBasicInfo(loan: LoanModel): Observable<any>{
    return this.http.post(this.auth.getUrl('loan/basic/' + loan.Id), loan.cloneForJson());
  }

  public photoUrl(peopleId: string): string{
    const url = this.auth.getUrl('avatar/') + peopleId;
    return 'url("' + url + '")';
  }

  public saveReward(reward: RewardModel, isNew: boolean ): Observable<RewardModel> {
    return this.http.post<RewardModel>(this.auth.getUrl('reward/'), reward);
  }

  public removeReward(reward: RewardModel): void {
    this.http.delete<RewardModel>(this.auth.getUrl('reward/' + reward.Id)).subscribe(
      resp => {
        console.log(resp);
      }
    );
  }

  public savePayIn(payIn: PayInModel, isNew: boolean): Observable<PayInModelEx> {
    return this.http.post<PayInModelEx>(this.auth.getUrl('payIn/'), payIn);
  }

  public removePayIn(id: number): void {
    this.http.delete<PayInModel>(this.auth.getUrl('payIn/' + id)).subscribe(
      resp => {
        console.log(resp);
      }
    );
  }

  // delete means more than remove, it removes relevant data too
  public deleteLoan(id: string): Observable<string> {
    return this.http.delete<string>(this.auth.getUrl('loan/' + id));
  }


  public getLoanByClient(id: string): Observable<LoanModel[]> {
    return this.http.get<LoanModel[]>(this.auth.getUrl('loan-by-client/' + id));
  }

}
