<%#
 Copyright 2013-2019 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
<%_ if (fieldsContainDate) { _%>
import * as moment from 'moment';
import { DATE_FORMAT } from 'app/shared/constants/input.constants';
import { map } from 'rxjs/operators';
<%_ } _%>

import { SERVER_API_URL } from 'app/app.constants';
import { createRequestOption } from 'app/shared';
import { I<%= entityAngularName %> } from 'app/shared/model/<%= entityModelFileName %>.model';

type EntityResponseType = HttpResponse<I<%= entityAngularName %>>;
type EntityArrayResponseType = HttpResponse<I<%= entityAngularName %>[]>;

@Injectable({providedIn: 'root'})
export class <%= entityAngularName %>Service {

    public resourceUrl = SERVER_API_URL + '<% if (applicationType === 'gateway' && locals.microserviceName) { %><%= microserviceName.toLowerCase() %>/<% } %>api/<%= entityApiUrl %>';
    <%_ if (searchEngine === 'elasticsearch') { _%>
    public resourceSearchUrl = SERVER_API_URL + '<% if (applicationType === 'gateway' && locals.microserviceName) { %><%= microserviceName.toLowerCase() %>/<% } %>api/_search/<%= entityApiUrl %>';
    <%_ } _%>

    constructor(protected http: HttpClient) { }

    create(<%= entityInstance %>: I<%= entityAngularName %>): Observable<EntityResponseType> {
        <%_ if (fieldsContainDate) { _%>
        const copy = this.convertDateFromClient(<%= entityInstance %>);
        <%_ } _%>
            return this.http.post<I<%= entityAngularName %>>(this.resourceUrl,
                    <% if (fieldsContainDate) { %> copy <% } else { %> <%= entityInstance %> <% } %>,
                    { observe: 'response' })
            <% if (fieldsContainDate) { %>.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)))<% } %>;
    }

    update(<%= entityInstance %>: I<%= entityAngularName %>): Observable<EntityResponseType> {
        <%_ if (fieldsContainDate) { _%>
        const copy = this.convertDateFromClient(<%= entityInstance %>);
        <%_ } _%>
            return this.http.put<I<%= entityAngularName %>>(this.resourceUrl,
                    <% if (fieldsContainDate) { %> copy <% } else { %> <%= entityInstance %> <% } %>,
                    { observe: 'response' })
            <% if (fieldsContainDate) { %>.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)))<% } %>;
    }

    find(id: <% if (pkType === 'String') { %>string<% } else { %>number<% } %>): Observable<EntityResponseType> {
        return this.http.get<I<%= entityAngularName %>>(`${this.resourceUrl}/${id}`, { observe: 'response'})
            <% if (fieldsContainDate) { %>.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)))<% } %>;
    }

    query(req?: any): Observable<EntityArrayResponseType> {
        const options = createRequestOption(req);
        return this.http.get<I<%= entityAngularName %>[]>(this.resourceUrl, { params: options, observe: 'response' })
            <% if (fieldsContainDate) { %>.pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)))<% } %>;
    }

    delete(id: <% if (pkType === 'String') { %>string<% } else { %>number<% } %>): Observable<HttpResponse<any>> {
        return this.http.delete<any>(`${this.resourceUrl}/${id}`, { observe: 'response'});
    }

    <%_ if (searchEngine === 'elasticsearch') { _%>
    search(req?: any): Observable<EntityArrayResponseType> {
        const options = createRequestOption(req);
        return this.http.get<I<%= entityAngularName %>[]>(this.resourceSearchUrl, { params: options, observe: 'response' })
            <% if (fieldsContainDate) { %>.pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)))<% } %>;
    }
    <%_ } _%>

<%_ if (fieldsContainDate) { _%>
    protected convertDateFromClient(<%= entityInstance %>: I<%= entityAngularName %>): I<%= entityAngularName %> {
        const copy: I<%= entityAngularName %> = Object.assign({}, <%= entityInstance %>, {
    <%_ for (idx in fields) { _%>
        <%_ if ( ['Instant', 'ZonedDateTime'].includes(fields[idx].fieldType) ) { _%>
        <%= fields[idx].fieldName %>: <%= entityInstance %>.<%= fields[idx].fieldName %> != null && <%= entityInstance %>.<%= fields[idx].fieldName %>.isValid() ? <%= entityInstance %>.<%= fields[idx].fieldName %>.toJSON() : null,
        <%_ } _%>
        <%_ if ( fields[idx].fieldType === 'LocalDate' ) { _%>
        <%= fields[idx].fieldName %>: <%= entityInstance %>.<%= fields[idx].fieldName %> != null && <%= entityInstance %>.<%= fields[idx].fieldName %>.isValid() ? <%= entityInstance %>.<%= fields[idx].fieldName %>.format(DATE_FORMAT) : null,
        <%_ } _%>
    <%_ } _%>
        });
        return copy;
    }

    protected convertDateFromServer(res: EntityResponseType): EntityResponseType {
        if (res.body) {
    <%_ for (idx in fields) { _%>
        <%_ if ( ['Instant', 'ZonedDateTime', 'LocalDate'].includes(fields[idx].fieldType) ) { _%>
            res.body.<%= fields[idx].fieldName %> = res.body.<%= fields[idx].fieldName %> != null ? moment(res.body.<%= fields[idx].fieldName %>) : null;
        <%_ } _%>
    <%_ } _%>
        }
        return res;
    }

    protected convertDateArrayFromServer(res: EntityArrayResponseType): EntityArrayResponseType {
        if (res.body) {
            res.body.forEach ((<%= entityInstance %>: I<%= entityAngularName %>) => {
    <%_ for (idx in fields) { _%>
        <%_ if ( ['Instant', 'ZonedDateTime', 'LocalDate'].includes(fields[idx].fieldType) ) { _%>
                <%= entityInstance %>.<%= fields[idx].fieldName %> = <%= entityInstance %>.<%= fields[idx].fieldName %> != null ? moment(<%= entityInstance %>.<%= fields[idx].fieldName %>) : null;
        <%_ } _%>
    <%_ } _%>
            });
        }
        return res;
    }
    <%_ } _%>
}
