import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { League } from 'src/modules/game/model/league.model';
import { API_BASE_URL } from 'src/modules/common/type/injection-token.type';
import { Page } from 'src/modules/common/data/model/page';
import { map } from 'rxjs/operators';

@Injectable()
export class LeagueService {
  constructor(
    @Inject(API_BASE_URL) private apiBaseUrl: string,
    private httpClient: HttpClient
  ) {}

  public getAllLeague(): Observable<Page<League>> {
    return this.httpClient.get<Page<League>>(`${this.apiBaseUrl}/league`, {});
  }

  public regist(league: League): Observable<League> {
    const engName = league.engName;
    const korName = league.korName;
    const sevenName = league.sevenName;
    const engCountry = league.engCountry;
    const korCountry = league.korCountry;
    const sportsEntities = league.sportsEntities;

    return this.httpClient
      .post<any>(`${this.apiBaseUrl}/league`, {
        engName,
        korName,
        sevenName,
        engCountry,
        korCountry,
        sportsEntities
      })
      .pipe(
        map(res => {
          console.log('league regist response: ' + res);
          return res;
        })
      );
  }

  public updateLeague(league: League): Observable<League> {
    const id = league.id;
    const engName = league.engName;
    const korName = league.korName;
    const sevenName = league.sevenName;
    const engCountry = league.engCountry;
    const korCountry = league.korCountry;
    const sportsEntities = league.sportsEntities;

    return this.httpClient
      .put<any>(`${this.apiBaseUrl}/league/${id}`, {
        id,
        engName,
        korName,
        sevenName,
        engCountry,
        korCountry,
        sportsEntities
      })
      .pipe(
        map(res => {
          console.log('league update response: ' + res);
          return res;
        })
      );
  }
}
