import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';

import { Observable } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';

import { HttpErrorHandler, HandleError } from '../../../src/auth/http-error-handler.service';
import { AuthService } from '../../../src/auth/auth.service';

import { AppConfig } from '../../../src/AppConfig/AppConfig';
import { BaseService } from '../../../src/baseclass/BaseService';
import { SyncResponseErrorHandleService } from '../../../src/route/SyncResponseErrorHandle';
import { NotificationClass, Notification } from '../../interface/Notification';
import { NotificationSummary, NotificationSummaryClass } from '../../interface/NotificationSummary';

@Injectable()
export class NotificationService extends BaseService<NotificationClass> {
  public handleError: HandleError;

  constructor(
    public syncResponseErrorHandleService: SyncResponseErrorHandleService,
    public authService: AuthService,
    public http: HttpClient,
    public httpErrorHandler: HttpErrorHandler) {
        super(authService, httpErrorHandler, http, AppConfig.settings.apiserviceurl,
            'NotificationClass', '', syncResponseErrorHandleService);
    this.handleError = httpErrorHandler.createHandleError('NotificationService');
  }

  apiserviceurl = AppConfig.settings.ipmsbaseurl;

  apiUrl = 'api/Notification/';

  GetByRecipient(userName: string): Observable<NotificationClass[]> {
      return this.http.get<NotificationClass[]>(this.apiserviceurl + this.apiUrl + 'GetByRecipient/' + userName, {
            headers: this.GetHeaders(),
      })
        .pipe(
            catchError(this.handleError('GetByRecipient', []))
        );
  }

  GetByRecipientAndModule(userName: string, moduleName: string): Observable<NotificationClass[]> {
    return this.http.get<NotificationClass[]>(this.apiserviceurl + this.apiUrl + 'GetByRecipientAndModule/' + userName +
    '/' + moduleName, {
          headers: this.GetHeaders(),
    })
      .pipe(
          catchError(this.handleError('GetByRecipientAndModule', []))
      );
}

  GetNotificationSummary(userName: string): Observable<NotificationSummaryClass> {
    return this.http.get<NotificationSummaryClass>(this.apiserviceurl + this.apiUrl + 'GetNotificationSummary/' + userName, {
          headers: this.GetHeaders(),
    })
      .pipe(
          catchError(this.handleError('GetNotificationSummary', null))
      );
  }

  BulkRead(notificationList: NotificationClass[]): Observable<SyncResponse> {
    return this.http.post<SyncResponse>(this.apiserviceurl + this.apiUrl + 'BulkRead', notificationList, {
          headers: this.GetHeaders(),
    })
      .pipe(
          tap(r => { this.syncResponseErrorHandleService.SyncResponseErrorHandle(r); }),
          catchError(this.handleError('BulkRead', null))
      );
  }

  BulkDelete(notificationList: NotificationClass[]): Observable<SyncResponse> {
    return this.http.post<SyncResponse>(this.apiserviceurl + this.apiUrl + 'BulkDelete', notificationList, {
          headers: this.GetHeaders(),
    })
      .pipe(
          tap(r => { this.syncResponseErrorHandleService.SyncResponseErrorHandle(r); }),
          catchError(this.handleError('BulkDelete', null))
      );
  }

}
