import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from "@angular/common/http";

import { Injectable } from '@angular/core';
import { DIRECTION } from "@xdf/commons";
import { Observable, of, throwError } from "rxjs";
import { delay, dematerialize, materialize, mergeMap } from "rxjs/operators";

const basePath = '/service/supervisor';

const tableData: Array<any> = [
    {
        id: 361,
        name: 'John',
        lastname: 'Doe'
    },
    {
        id: 362,
        name: 'Jane',
        lastname: 'Doe'
    },
    {
        id: 363,
        name: 'Juan',
        lastname: 'Perez'
    },
    {
        id: 364,
        name: 'Pedro',
        lastname: 'Perez'
    }
];

@Injectable()
export class SupervisorFakeBackend implements HttpInterceptor {
    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        const { url, method, headers, body } = request;

        return of(null)
            .pipe(mergeMap(handleRoute))
            .pipe(materialize())
            .pipe(delay(10))
            .pipe(dematerialize());

        function handleRoute() {

            switch (true) {
                case url.endsWith(basePath + '/page') && method === 'POST':
                    return pagination(body);
                case url.indexOf(basePath + '/connection/data') !== -1 && method === 'GET ':
                    return ok({});
                /* case url.indexOf(basePath + '/countries') !== -1 && method === 'GET':
                    return ok(countries);
                case url.indexOf(basePath + '/channels') !== -1 && method === 'GET':
                    return ok(channels);
                case url.indexOf(basePath + '/file-upload') !== -1 && method === 'POST':
                    return ok(fileOK);
                case url.indexOf(basePath + '/') !== -1 && method === 'GET':
                    return ok(fakeAgentData); */
                default:
                    // pass through any requests not handled above
                    return next.handle(request);
            }
        }

        function pagination(dataRecibida: any) {
            dataRecibida.totalItems = tableData.length;

            const pageNumber = dataRecibida.currentPage;
            const pageSize = dataRecibida.itemsPerPage ? dataRecibida.itemsPerPage : 5;

            dataRecibida.totalPages = Math.ceil(dataRecibida.totalItems / pageSize);
            
            
            if (dataRecibida.sortFields && dataRecibida.sortFields.length > 0) {
                const sortField: any = dataRecibida.sortFields[0];
                if (sortField.direction === DIRECTION.asc) {
                    tableData.sort((a, b) => {
                        if (JSON.stringify(a[sortField.field]) > JSON.stringify(b[sortField.field])) {
                            return 1;
                        } else if (JSON.stringify(a[sortField.field]) < JSON.stringify(b[sortField.field])) {
                            return -1;
                        }
                        return 0;
                    });
                } else {
                    tableData.sort((a, b) => {
                        if (JSON.stringify(a[sortField.field]) < JSON.stringify(b[sortField.field])) {
                            return 1;
                        } else if (JSON.stringify(a[sortField.field]) > JSON.stringify(b[sortField.field])) {
                            return -1;
                        }
                        return 0;
                    });
                }
            }

            dataRecibida.data = tableData.slice(pageNumber * pageSize, (pageNumber + 1) * pageSize);
            console.log(dataRecibida);
            console.log(pageSize);
            return ok(dataRecibida);
        }


        function ok(bodyContent?: any) {
            return of(new HttpResponse({ status: 200, body: bodyContent }));
        }

        function error(message: string) {
            return throwError({ error: { message } });
        }
    }
}