import {EventEmitter, Injectable, Output} from '@angular/core';
import {AuthService} from '../auth/_services';
import {TokenStorage} from '../auth/_services/token-storage.service';
import {UsuariosService} from '../../views/pages/catalogos/usuarios/usuarios.service';
import {RolesService} from '../../views/pages/catalogos/roles/roles.service';
import {MatSnackBar} from "@angular/material/snack-bar";
import {EstadosServicioService} from '../../views/pages/catalogos/estados-servicio/estados-servicio.service';
import {FormasPagoService} from '../../views/pages/catalogos/formas-pago/formas-pago.service';
import {TiposServicioService} from '../../views/pages/catalogos/tipos-servicio/tipos-servicio.service';
import {ServiciosService} from '../../views/pages/catalogos/servicios/servicios.service';
import {TiposVehiculoService} from '../../views/pages/catalogos/tipos-vehiculo/tipos-vehiculo.service';
import {VehiculosService} from '../../views/pages/catalogos/vehiculos/vehiculos.service';
import {SucursalesService} from '../../views/pages/catalogos/sucursales/sucursales.service';
import {ClientesService} from '../../views/pages/catalogos/clientes/clientes.service';
import { SolicitudesServicioService } from '../../views/pages/servicios/solicitudes-servicio/solicitudes-servicio.service';
import { ReportesService } from '../../views/pages/reportes/reportes.service';
import {ClientesDomiciliosService} from '../../views/pages/catalogos/clientes/clientes-domicilios.service';
import {AgendaService} from "../../views/pages/agenda/agenda.service";

@Injectable({
	providedIn: 'root'
})
export class ApiService {


	@Output() error: EventEmitter<any> = new EventEmitter(true);

	constructor(
		public login: AuthService,
		public usuarios: UsuariosService,
		public roles: RolesService,
		public estadosServicio: EstadosServicioService,
		public formasPago: FormasPagoService,
		public tiposServicio: TiposServicioService,
		public servicios: ServiciosService,
		public tiposVehiculos: TiposVehiculoService,
		public vehiculos: VehiculosService,
		public sucursales: SucursalesService,
		public clientes: ClientesService,
		public clientesDomicilios: ClientesDomiciliosService,
		public solicitudesServicioService: SolicitudesServicioService,
		public reportesService: ReportesService,
		public tokenStorage: TokenStorage,
		public snackBar: MatSnackBar,
		public agenda: AgendaService
	) {
		this.login.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.usuarios.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.roles.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.estadosServicio.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.formasPago.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.tiposServicio.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.servicios.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.tiposVehiculos.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.vehiculos.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.sucursales.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.clientes.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.clientesDomicilios.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.solicitudesServicioService.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.reportesService.errorEvent.subscribe((res: any) => this.error.emit(res));
		this.agenda.errorEvent.subscribe((res: any) => this.error.emit(res));
	}

	/**
	 * Get access token
	 * @description Should return access token in Observable from e.g. localStorage
	 * @returns {Observable<string>}
	 */
	public getAccessToken(): string {
		return this.tokenStorage.getAccessToken();
	}

	public snackbar(msg: string, duration: number = 3000) {
		this.snackBar.open(msg, '', {
			duration: duration,
			verticalPosition: 'bottom',
			horizontalPosition: 'right',
		});
	}
}
