import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';

import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';

import { AuthService } from '../auth/auth.service';

import { BaseService } from '../baseclass/BaseService';
import { SystemSettingClass } from '../interface/system-setting';
import { HandleError, HttpErrorHandler } from '../auth/http-error-handler.service';
import { AppConfig } from '../AppConfig/AppConfig';

@Injectable()
export class SystemSettingService extends BaseService<SystemSettingClass> {
  SystemSettingClass: SystemSettingClass[];
  public handleError: HandleError;
  
  constructor(
    // private appSettingsService: AppSettingsService,
    public authService: AuthService,
    public http: HttpClient,
    public httpErrorHandler: HttpErrorHandler) {
      super(authService,httpErrorHandler,http,AppConfig.settings.apiserviceurl,"UserManagementApi");
    this.handleError = httpErrorHandler.createHandleError('SystemSettingService');
  }
  // private settings: AppSettings;
  
  apiserviceurl = AppConfig.settings.apiserviceurl;

  apiUrl = 'api/SystemSettingApi/';  // URL to web api

  // httpOptions = {
  //   headers: new HttpHeaders({
  //     'Content-Type':  'application/json',
  //     'Authorization': this.authService.getAuthorizationHeaderValue()
  //   })
  // };
     
  httpHeaders = new HttpHeaders()
    .set('Content-Type', 'application/json')
    .set('Authorization', this.authService.getAuthorizationHeaderValue());

   Get (): Observable<SystemSettingClass[]> { 
    // let httpParams = new HttpParams()
    //                       .set('category', "")
    //                 .set('year', "");

      //  alert("BASE_URL:"+this.apiserviceurl + this.apiUrl);
      return this.http.get<SystemSettingClass[]>(this.apiserviceurl + this.apiUrl, {
            headers: this.httpHeaders,
            // params: httpParams, 
            // responseType: 'json'
      })
        .pipe(
          catchError(this.handleError('getSystemSetting', []))
        );
  }

  /** POST: add a new hero to the database */
  post (SystemSettingClass: SystemSettingClass): Observable<SystemSettingClass> {
    return this.http.post<SystemSettingClass>(this.apiserviceurl + this.apiUrl, 
      SystemSettingClass, {
        headers: this.httpHeaders,
        // params: httpParams, 
        // responseType: 'json'
      })
      .pipe(
        catchError(this.handleError('getSystemSetting', null))
      );
  }

  edit (editActionPermissionClass: SystemSettingClass): Observable<string> {
    // let httpParams = new HttpParams()
    //                       .set('id', id);

    return this.http.put<SystemSettingClass>(this.apiserviceurl + this.apiUrl, 
      editActionPermissionClass, {
        headers: this.httpHeaders,
        // params: httpParams, 
        // responseType: 'json'
      })
      .pipe(
        catchError(this.handleError('editSystemSettingClass', null))
      );
  }


  delete(SystemSettingClass: SystemSettingClass): Observable<SystemSettingClass> {
    // let httpParams = new HttpParams()
    //                       .set('id', id);

    return this.http.delete<SystemSettingClass>(this.apiserviceurl + this.apiUrl + SystemSettingClass.id, 
       {
        headers: this.httpHeaders,
        // params: httpParams, 
        // responseType: 'json'
      })
      .pipe(
        catchError(this.handleError('deleteSystemSetting', null))
      );
  }

}
