
import { Injectable } from '@angular/core';
import { HttpHandler, HttpRequest, HttpInterceptor, HttpEvent, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { EMPTY } from 'rxjs';
import { environment } from '../../../environments/environment';
import { DateUtils } from '../../utilities/date-utils';
@Injectable()
export class ApiPrefixInterceptor implements HttpInterceptor {
  constructor(
  ) { }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    //
    // this.logger.level = LogLevel[this.appConfig.getLogLevel()];
    // this.logger.debug('at ApiPrefixInterceptor...');
    if (!/^(http|https):/i.test(request.url) && !request.url.toLowerCase().startsWith("/assets") &&
      !request.url.toLowerCase().startsWith("assets/") &&
      !request.url.toLowerCase().startsWith("/evo/") &&
      request.url.toLowerCase().indexOf("googleapis.com") == -1) {
      // if (request.url.toLowerCase().startsWith(this.appConfig.getLogUrl().toLowerCase())) {
      //   return EMPTY;

      // } else 
      // if (request.url.toLowerCase().startsWith("token\/create") ||
      //   request.url.toLowerCase().startsWith("token\/validate") ||
      //   request.url.toLowerCase().startsWith("account\/forgotpassword")) {
      //   request = request.clone({ url: this.appConfig.getAuthUrl() + request.url });

      // }
      // else if (request.url.toLowerCase().startsWith("log\/create")) {
      //   request = request.clone({ url: this.appConfig.getLogUrl() + request.url });

      // }
      // else {

      //if (!!sessionStorage.getItem('allAccess')) {

      request = request.clone({ url: `${environment.apiUrl}\\${request.url}` });
      //request = request.clone({ url: userAccess.endPoint + request.url });

      //}


    }

    return next.handle(request)
      .do((event: HttpEvent<any>) => {
        if (event instanceof HttpResponse) {
          this.convertDates(event.body);
        }
      });
  }
  private dateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)$/;

  private utcDateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/;

  private convertDates(object: Object) {
    if (!object || !(object instanceof Object)) {
      return;
    }

    if (object instanceof Array) {
      for (const item of object) {
        this.convertDates(item);
      }
    }

    for (const key of Object.keys(object)) {
      const value = object[key];

      if (value instanceof Array) {
        for (const item of value) {
          this.convertDates(item);
        }
      }

      if (value instanceof Object) {
        this.convertDates(value);
      }
      if (
        (typeof value === 'string' || typeof value === 'undefined')
        &&
        (key.toLowerCase().indexOf('date') > -1 || key.toLowerCase().indexOf('time') > -1 || key.toLowerCase().indexOf('birthday') > -1)
      ) {
        //object[key] = new Date(value.replace('+00:00', ' UTC'));
        object[key] = new Date(value);
      }
      // if (typeof value === 'string' && this.utcDateRegex.test(value)) {
      //   object[key] = new Date(value);
      // }
    }
  }
}