import { Injectable } from '@angular/core';
import { BaseService } from 'ekangularbase/src/baseclass/BaseService';
import { HandleError, HttpErrorHandler } from 'ekangularbase/src/auth/http-error-handler.service';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { AppConfig } from 'ekangularbase/src/AppConfig/AppConfig';
import { Observable } from 'rxjs';
import { AuthService } from 'ekangularbase/src/auth/auth.service';
import { catchError, tap } from 'rxjs/operators';
import { FieldHasAuditTrailClass } from '../../interface/field-has-audit-trial';
import { SyncResponseErrorHandleService } from 'ekangularbase/src/route/SyncResponseErrorHandle';
import { AuditTrailClass, LogHasChangeById, SpecifyLogHasChangeById } from 'ekangularbase/src/interface/audit-trail';

@Injectable()
export class AuditTrailService extends BaseService<AuditTrailClass> {
  public handleError: HandleError;

  constructor(
    public syncResponseErrorHandleService: SyncResponseErrorHandleService,
        public authService: AuthService,
        public http: HttpClient,
        public httpErrorHandler: HttpErrorHandler) {
        super(authService, httpErrorHandler, http, AppConfig.settings.apiserviceurl, 'Logs', '', syncResponseErrorHandleService);
    this.handleError = httpErrorHandler.createHandleError('AuditTrailService');
  }

  apiserviceurl = AppConfig.settings.apiserviceurl;
    apiUrl = 'api/Logs/';

    //  httpHeaders = new HttpHeaders()
    //      .set('Content-Type', 'application/json')
    //      .set('Authorization', this.authService.getAuthorizationHeaderValue());

    GetHasChanges(entityType: string, rowId: string): Observable<FieldHasAuditTrailClass[]> {
        const paramsInput: HttpParams = new HttpParams()
        .append('type', entityType)
        .append('id', rowId);

         return this.http.get<FieldHasAuditTrailClass[]>(this.apiserviceurl + this.apiUrl + 'HasChanges', {
                headers: this.GetHeaders(),
                params: paramsInput
          })
            .pipe(
              catchError(this.handleError('getAuditTrailHasChanges', []))
            );
        }

    GetChanges(entityType: string, rowId: string, columnName: string): Observable<AuditTrailClass[]> {

         const paramsInput: HttpParams = new HttpParams()
         .append('type', entityType)
         .append('id', rowId)
         .append('column', columnName);

          return this.http.get<AuditTrailClass[]>(this.apiserviceurl + this.apiUrl + 'GetChanges', {
                 headers: this.GetHeaders(),
                 params: paramsInput
           })
             .pipe(
               catchError(this.handleError('getAuditTrailGetChanges', []))
             );
       }

       HasChangesList(entityType: string, object: any[]): Observable<LogHasChangeById[]> {
        return this.http.post<LogHasChangeById[]>(this.apiserviceurl + this.apiUrl + 'HasChangesList/' + entityType,
        object, this.optionss()).pipe(
                catchError(this.handleError('HasChangesList', []))
            );
      }

      HasSpecifyChangesList(entityType: string, object: SpecifyLogHasChangeById): Observable<SpecifyLogHasChangeById> {
        return this.http.post<SpecifyLogHasChangeById>(this.apiserviceurl + this.apiUrl + 'HasSpecifyChangesList/' + entityType,
        object, this.optionss()).pipe(
                catchError(this.handleError('HasSpecifyChangesList', null))
            );
      }

      PostHasChanges(entityType: string, rowId: string, columnList: string[]): Observable<FieldHasAuditTrailClass[]> {
        return this.http.post<FieldHasAuditTrailClass[]>(this.apiserviceurl + this.apiUrl + 'HasChanges/' +
        entityType + '/' + rowId, columnList, {
          headers: this.GetHeaders(),
      })
        .pipe(
            catchError(this.handleError('PostHasChanges', null))
        );
      }

}
