import { HttpRequest, HttpResponse, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { delay, mergeMap, materialize, dematerialize } from 'rxjs/operators';
import { SortField, DIRECTION } from '@xdf/commons';
import { Injectable } from '@angular/core';



import * as source from '../../assets/fake-data/aeropuerto-data.json';
import { BoundDirectivePropertyAst } from '@angular/compiler';

const basePath = './service/aeropuerto';
///service/user-role
@Injectable()
export class AeropuertoFakeBackendInterceptor implements HttpInterceptor {
    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        const { url, method, headers, body } = request;
        const data = (source as any).default;
        return of(null)
            .pipe(mergeMap(handleRoute))
            .pipe(materialize())
            .pipe(delay(50))
            .pipe(dematerialize());

            function handleRoute() {
                switch (true) {                
                  case url.endsWith(basePath + '/page') && method === 'POST':
                    return pagination(body);

                    case url.match(".*" + basePath) && method === 'GET':
                      console.log(url);
                        return getOne(url)
    
                    default:
                        // pass through any requests not handled above
                        return next.handle(request);
                }
            }

          function pagination(body) {
            
            body.totalPages = 1;
            body.totalItems = 2;
            
            const page_number = body.currentPage;
            const page_size = body.itemsPerPage ? body.itemsPerPage : 5;
      
           if(body.sortFields.length > 0){
            const sortField: SortField = body.sortFields[0];
            const sign = sortField.direction === DIRECTION.asc ? 1: -1;

            data.sort(function (a, b){
              if((typeof a[sortField.field] === 'number') && (typeof b[sortField.field] === 'number')){
                if(a[sortField.field] > b[sortField.field]){
                  return 1 * sign;
                } else if (a[sortField.field] < b[sortField.field]){
                  return -1 * sign;
                }
                return 0;
              } else {
                return a[sortField.field].localeCompare(b[sortField.field])*sign;
              }
            });
           }

           body.data = data.slice(page_number * page_size, (page_number+1)*page_size);
           return ok(body);
        }
    
            function getData() {
                return ok(data);
            }
    
            // helper functions
    
            function ok(bodyContent?) {
                return of(new HttpResponse({ status: 200, body: bodyContent }));
            }
    
            function error(message: string) {
                return throwError({ error: { message } });
            }
            
            function getOne(url: string) {
                return ok(data)
            }    
    }
}