import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { first, map } from 'rxjs/operators';
import { HappinessBEST, HappinessTask } from '../../entity/HappinessGroup';

import { saveAs } from 'file-saver';
import { CrudService } from './crud.service';

const BEST_URL = (id: string = null) => { return `Best${(id ? `/${id}` : '')}` };
const POST_URL_UPDATE_BEST_WEEK = 'Best/UpdateBestWeek';
@Injectable({
  providedIn: 'root'
})
export class HappinessGroupService {

  constructor(
    protected http: HttpClient,
  ) {
    //super(http, HAPPINESS_GROUP_BASE_URL);
  }
  createOrUpdateBestWeek(data) {
    return this.http.post<boolean>(POST_URL_UPDATE_BEST_WEEK,
      JSON.stringify(data), {
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
    });

  }

  createBest(best: HappinessBEST) {
    return this.http.post<boolean>(BEST_URL(), JSON.stringify(best),
      {
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        }
      }
    );
  }

  getBest(id: string): Observable<HappinessBEST> {
    return this.http.get<HappinessBEST>(BEST_URL(`GetById?id=${id}`));
  }

  getBestQrCode(fileName: string, id: string) {

    return this.http.get(BEST_URL(`GetInvitationQRcode?id=${id}`), { responseType: "blob", headers: { 'Accept': 'application/png' } })
      .pipe(first()).subscribe(blob => {
        saveAs(blob, fileName);
      });
  }


}


const WEEK_TASK_BASE_URL = (action: string = null) => { return `HappinessWeekTask${(action ? `/${action}` : '')}` }
@Injectable({
  providedIn: 'root'
})
export class HappinessTaskService extends CrudService<HappinessTask> {

  constructor(protected http: HttpClient) {
    super(http, WEEK_TASK_BASE_URL);
  }
}
