import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {AuthService} from './auth.service';
import {Observable} from 'rxjs';
import {PeopleModel} from '../models/people.model';
import {BrokerModel} from '../models/broker.model';
import {LoanModel} from '../models/loan.model';
import {ChangePassword} from '../models/change-password.model';
import {UserExtraModel} from '../models/user-extra.model';

@Injectable({providedIn: 'root'})
export class PeopleService {
  constructor(private http: HttpClient, private auth: AuthService ){ }

  public getPeopleById(id: string): Observable<PeopleModel> {
    return this.http.get<PeopleModel>(this.auth.getUrl('people/' + id));
  }
  public getPeopleExtraById(id: string): Observable<UserExtraModel> {
    return this.http.get<UserExtraModel>(this.auth.getUrl('people-extra/' + id));
  }

  public getPeopleList(filter: string): Observable<{Count: number, List: PeopleModel[]}> {
    const params = new HttpParams().set('filter', filter);
    return this.http.get<{Count: number, List: PeopleModel[]}>(this.auth.getUrl( 'people-list/'), { params});
  }

  public getBrokerList(filter: string): Observable<{Count: number, List: BrokerModel[]}> {
    const params = new HttpParams().set('filter', filter);
    return this.http.get<{Count: number, List: BrokerModel[]}>(this.auth.getUrl( 'broker-list/'), { params});
  }

  public syncPeople(loan: LoanModel): Observable<boolean> {
    return this.http.post<boolean>(this.auth.getUrl('sync-people/'), loan);
  }

  public updateAvatar(avatar: string, id: string ): Observable<boolean> {
    return this.http.post<boolean>(this.auth.getUrl('avatar/' + id), avatar);
  }

  public savePassword(id: string, change: ChangePassword): Observable<boolean>{
    return this.http.post<boolean>(this.auth.getUrl('change-pass/' + id), change);
  }

  public saveBroker(broker: BrokerModel): Observable<BrokerModel>{
    return this.http.post<BrokerModel>(this.auth.getUrl('broker/' + broker.Id), broker);
  }

  public savePeople(people: PeopleModel): Observable<BrokerModel>{
    return this.http.post<BrokerModel>(this.auth.getUrl('people/' + people.Id), people);
  }

  public createPeople(people: PeopleModel): Observable<PeopleModel> {
    return this.http.put<PeopleModel>(this.auth.getUrl('people/' + people.Id), people);
  }

  public deletePeople(id: string): Observable<string> {
    return this.http.delete<string>(this.auth.getUrl('people/' + id));
  }

  public upgradePeopleToUser(id: string ): Observable<UserExtraModel> {
    return this.http.put<UserExtraModel>(this.auth.getUrl('user/' + id), {});
  }

  public downgradeUserToPeople(id: string ): Observable<UserExtraModel> {
    return this.http.delete<UserExtraModel>(this.auth.getUrl('user/' + id), {});
  }

  public upgradeUserToBroker(id: string ): Observable<UserExtraModel> {
    return this.http.put<UserExtraModel>(this.auth.getUrl('broker/' + id), {});
  }
  public downgradeBrokerToUser(id: string ): Observable<UserExtraModel> {
    return this.http.delete<UserExtraModel>(this.auth.getUrl('broker/' + id), {});
  }

  public enableUser(enable: boolean, id: string): Observable<boolean> {
    return this.http.post<boolean>(this.auth.getUrl('user-enable/' + id), enable);
  }

}
