import { Injectable } from '@angular/core';
import { EntitiesService, QueryParam } from '@iqser/common-ui';
import { ComponentMapping, ComponentMappingList, IComponentMapping, IComponentMappingList } from '@red/domain';
import { Observable } from 'rxjs';
import { HeadersConfiguration, List } from '@common-ui/utils';
import { map } from 'rxjs/operators';
import { HttpResponse } from '@angular/common/http';

@Injectable({
    providedIn: 'root',
})
export class ComponentMappingsService extends EntitiesService<IComponentMapping, ComponentMapping> {
    getComponentMappings(dossierTemplateId: string): Observable<IComponentMappingList> {
        return this._http
            .get<IComponentMappingList>(`/api/dossier-templates/${dossierTemplateId}/component-mappings`)
            .pipe(map(componentMappingList => this.#mapEntities(componentMappingList)));
    }

    createUpdateComponentMapping(
        dossierTemplateId: string,
        componentMapping: Partial<ComponentMapping>,
        file: Blob,
    ): Observable<IComponentMapping> {
        const formParams = new FormData();
        formParams.append('file', file, componentMapping.fileName);

        const queryParams: List<QueryParam> = [
            { key: 'name', value: componentMapping.name },
            { key: 'encoding', value: componentMapping.encoding },
            { key: 'delimiter', value: componentMapping.delimiter },
            { key: 'quoteChar', value: componentMapping.quoteChar },
        ];

        if (componentMapping.id) {
            return this._http.put<IComponentMapping>(
                `/api/dossier-templates/${dossierTemplateId}/component-mappings/${componentMapping.id}`,
                formParams,
                {
                    params: this._queryParams(queryParams),
                },
            );
        }

        return this._http.post<IComponentMapping>(`/api/dossier-templates/${dossierTemplateId}/component-mappings`, formParams, {
            params: this._queryParams(queryParams),
        });
    }

    deleteComponentMapping(dossierTemplateId: string, componentMappingId: string) {
        return this._http.delete(`/api/dossier-templates/${dossierTemplateId}/component-mappings/${componentMappingId}`);
    }

    getComponentMappingFile(dossierTemplateId: string, componentMappingId: string): Observable<HttpResponse<Blob>> {
        return this._http.get(`/api/dossier-templates/${dossierTemplateId}/component-mappings/${componentMappingId}`, {
            responseType: 'blob',
            observe: 'response',
            headers: HeadersConfiguration.getHeaders({ contentType: false }),
        });
    }

    #mapEntities(componentMappingList: IComponentMappingList) {
        const componentMappings = componentMappingList.componentMappingList.map(m => new ComponentMapping(m));
        return new ComponentMappingList({
            dossierTemplateId: componentMappingList.dossierTemplateId,
            componentMappingList: componentMappings,
        });
    }
}
