import { Injectable } from '@angular/core';
import { HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiService } from './api.service';
import { Jump, JumpByDate, JumpList, JumpListConfig, JumpListFilters } from 'src/app/core/models';

@Injectable({ providedIn: 'root' })
export class JumpsService {
    constructor(
        private apiService: ApiService
    ) { }

    query(config: JumpListConfig): Observable<JumpList> {
        // Convert any filters over to Angular's URLSearchParams
        const params: JumpListFilters = {
            date: config.filters.date,
            lieu: config.filters.lieu,
            oaci: config.filters.oaci,
            aeronef: config.filters.aeronef,
            imat: config.filters.imat,
            hauteur: config.filters.hauteur,
            voile: config.filters.voile,
            taille: config.filters.taille,
            categorie: config.filters.categorie,
            module: config.filters.module,
            participants: config.filters.participants,
            accessoires: config.filters.accessoires,
            zone: config.filters.zone,
            author: config.filters.author,
            limit: config.filters.limit,
            offset: config.filters.offset
        };

        return this.apiService
            .get(
                '/jumps' + ((config.type === 'feed') ? '/feed' : ''),
                new HttpParams({ fromObject: <any>params })
            );
    }

    get(slug: string): Observable<Jump> {
        return this.apiService.get(`/jumps/${slug}`)
            .pipe(map(data => data.jump));
    }

    getAll(): Observable<[Jump]> {
        return this.apiService.get('/jumps')
            .pipe(map(data => data.jumps));
    }

    getAllByDate(): Observable<Array<JumpByDate>> {
        return this.apiService.get('/jumps/allByDate')
            .pipe(map(data => data.jumps));
    }

    getLastJump(): Observable<Jump> {
        return this.apiService.get('/jumps/last')
            .pipe(map(data => data.jump));
    }

    create(jump: Jump): Observable<Jump> {
        return this.apiService.post('/jumps/', { jump: jump })
            .pipe(map(data => data.jump));
    }

    update(jump: Jump): Observable<Jump> {
        return this.apiService.put(`/jumps/${jump.slug}`, { jump: jump })
            .pipe(map(data => data.jump));
    }

    destroy(slug: string) {
        return this.apiService.delete(`/jumps/${slug}`);
    }

    save(jump: Jump): Observable<Jump> {
        // If we're updating an existing jump
        if (jump.slug) {
            return this.apiService.put('/jumps/' + jump.slug, { jump: jump })
                .pipe(map(data => data.jump));

            // Otherwise, create a new jump
        } else {
            return this.apiService.post('/jumps/', { jump: jump })
                .pipe(map(data => data.jump));
        }
    }

    getAeronefs() {
        /*
        [
            {
                '$match': {
                'aeronef': 'Pilatus'
                }
            }, {
                '$group': {
                '_id': {
                    'Aeronef': '$aeronef', 
                    'Imat': '$imat'
                }, 
                'count': {
                    '$sum': 1
                }
                }
            }
        ]
        */
    }

}
