import { VersionUpdate } from "../../interface/VersionUpdate";

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 { HttpErrorHandler, HandleError } from '../../../src/auth/http-error-handler.service';
import { AuthService } from '../../../src/auth/auth.service';
//import { RoleClass } from '../../interface/role';

// import { AppSettingsService } from '../../../shared/appsettings/appsettings.service';
// import { AppSettings } from '../../../shared/appsettings/appsettings';
import { AppConfig } from '../../../src/AppConfig/AppConfig';

@Injectable()
export class VersionUpdateService {
  private handleError: HandleError;
  
  constructor(
    private http: HttpClient,
    httpErrorHandler: HttpErrorHandler) {
    this.handleError = httpErrorHandler.createHandleError('RoleService');
  }
  // private settings: AppSettings;
  
  apiserviceurl = AppConfig.settings.UserManagerSettings.authority;

  apiUrl = 'api/VersionUpdates/';  // URL to web api

  // httpOptions = {
  //   headers: new HttpHeaders({
  //     'Content-Type':  'application/json',
  //     'Authorization': this.authService.getAuthorizationHeaderValue()
  //   })
  // };

  Get(authService:AuthService): Observable<VersionUpdateClass[]> { 
     
   var httpHeaders = new HttpHeaders()
    .set('Authorization', authService.getAuthorizationHeaderValue());

     return this.http.get<VersionUpdateClass[]>(this.apiserviceurl + this.apiUrl, {
            headers: httpHeaders
      })
        .pipe(
          catchError(this.handleError('getVersionUpdate', []))
        );
  }

  TriggerUpdate(authService:AuthService,updateid:string): Observable<SyncResponse> { 
     
  var  httpHeaders = new HttpHeaders()
    .set('Authorization', authService.getAuthorizationHeaderValue());

    return this.http.get<SyncResponse>(this.apiserviceurl + this.apiUrl + "TriggerUpdate?guid=" + updateid, {
           headers: httpHeaders
     })
       .pipe(
         catchError(this.handleError('getVersionUpdate',null))
       );
 }
}


export class VersionUpdateClass implements VersionUpdate
{
    id: string;
    updateObjectWrapperId: string;
    version: string;
    availableSince: Date;
    status: string;
    updateBy: string;
    triggerUpdateUrl: string;
}
