import {Injectable} from '@angular/core';
import {ApiBase} from '../../../../core/api/api-base';
import {HttpClient, HttpParams} from '@angular/common/http';
import { isNullOrUndefined } from 'util';
import { catchError } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { environment } from '../../../../../environments/environment';


@Injectable({
	providedIn: 'root'
})
export class ClientesDomiciliosService extends ApiBase {

	baseUrl = environment.API + 'administrador/clientes/';

	constructor(public http: HttpClient) {
		super();
	}

	public index(url: string, page?: string, searchText?: string, sortBy?: string, order?: string, perPage?: string, clienteDomicilioID?: number, visible?: string, deleted_at?: string): Observable<any> {

		const params = new HttpParams()
			.set('sortBy', isNullOrUndefined(sortBy) ? '' : sortBy)
			.set('order', isNullOrUndefined(order) ? '' : order)
			.set('page', isNullOrUndefined(page) ? '1' : page)
			.set('perPage', isNullOrUndefined(perPage) ? '10' : perPage)
			.set('visible', isNullOrUndefined(visible) ? '1' : visible)
			.set('deleted', isNullOrUndefined(deleted_at) ? '0' : deleted_at)
			.set('query', searchText ? searchText : '');
		const options = {params: params};

		return this.http.get(this.baseUrl + clienteDomicilioID + url, options).pipe(
			catchError(this.handleError('index'))
		);
	}


	public create(url: string, body: any, clienteDomicilioID: string): Observable<any> {
		return this.http.post(this.baseUrl + clienteDomicilioID + url, body).pipe(
			catchError(this.handleError('create'))
		);
	}

	public delete(url: string, id: number, clienteId: number): Observable<any> {
		return this.http.delete(`${this.baseUrl + clienteId + url}/${id}`).pipe(
			catchError(this.handleError('delete'))
		);
	}

	public show(url: string, id: number, clienteDomicilioID: string): Observable<any> {
		return this.http.get(`${this.baseUrl + clienteDomicilioID + url}/${id}`).pipe(
			catchError(this.handleError('show'))
		);
	}

	public update(url: string, id: number, body: any, clienteDomicilioID: string): Observable<any> {
		return this.http.put(`${this.baseUrl + clienteDomicilioID + url}/${id}`, body).pipe(
			catchError(this.handleError('update'))
		);
	}
}
