// Questo file fa parte di MyTown.
// MyTown è software libero: puoi ridistribuirlo e/o modificarlo secondo
// i termini della GNU Affero General Public License come pubblicata dalla
// Free Software Foundation, versione 3.
// Distribuito senza ALCUNA GARANZIA; vedi la GNU AGPL v3 per i dettagli.
// Copyright (C) Kondor Consulting & Services S.r.l.
// SPDX-License-Identifier: AGPL-3.0-or-later

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, switchMap } from 'rxjs/operators';
import { Observable, Subject, from } from 'rxjs';
import { ConfigService } from './config.service';
import { Scan } from 'src/models/scan.model';
import { Worksite } from 'src/models/worksite.model';
import { WorksiteReport } from 'src/models/worksite-record';
import { ServiceCard } from 'src/app/models/serviceCard.model';

@Injectable({
	providedIn: 'root',
})
export class CompaniesService {
	authorizated = false;
	public token: string = '';
	reload = new Subject<boolean>();

	/** Service card attiva (impostata al click sulla card) */
	activeServiceCard: ServiceCard | null = null;

	constructor(private http: HttpClient, private configService: ConfigService) {}

	set_reload(r: any) {
		this.reload.next(r);
	}

	loadCompaniesService(): Observable<any> {
		return this.http.get(this.configService.serviceUrl_Companies + 'company_service/get_companies_service_lst', { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	saveWorksiteRecord(scan: Scan): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Companies + 'worksite/saverecord', scan, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	deleteWorksiteRecord(id: number): Observable<any> {
		return this.http.delete(this.configService.serviceUrl_Companies + 'worksite/delrecord/' + id, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	GetWorksiteRecord(txtsearch: string, order_by: string, limit: number, offset: number, cmp_id: number): Observable<any> {
		return this.http.get(this.configService.serviceUrl_Companies + 'worksite/recordlist?txtsearch=' + txtsearch + '&order_by=' + order_by + '&limit=' + limit + '&offset=' + offset + '&cmp_id=' + cmp_id, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	updateWorkSite(cmp_id: number, WorkSiteObj: Worksite): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Companies + 'worksite/add/' + cmp_id, WorkSiteObj, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	get_lstAppointmentState(): Observable<any> {
		return this.http.get(this.configService.serviceUrl_Companies + 'companies/lista_stati_appuntamenti', { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getSettingsCompaniesAppointment(cmp_id: number): Observable<any> {
		return this.http.get(this.configService.serviceUrl_Companies + 'companies/get_settings_cmp_appointment/' + cmp_id, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getAppointmentsList(filter: any): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Companies + 'companies/lista_appuntamenti', filter, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	addCompanyAppointment(cmp_id: number, data: any): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Companies + 'companies/add_appuntamento/' + cmp_id, data, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	updCompanyAppointment(cmp_id: number, data: any): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Companies + 'companies/upd_appuntamento/' + cmp_id, data, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getCantieriinProssimita(cmp_id: number, current_position: any): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Companies + 'worksite/lst_cantieri_in_prossimita/' + cmp_id, current_position, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getTypeRecord(scan: Scan): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Companies + 'worksite/get_type_record', scan, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getWorksiteList(objSearchDB: any, cmp_id: number): Observable<any> {
		return this.http.get(`${this.configService.serviceUrl_Companies}worksite/listall?cmp_id=${cmp_id}`, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getWorksiteListAll(cmp_id: number): Observable<any> {
		return this.http.get(`${this.configService.serviceUrl_Companies}worksite/listall?cmp_id=${cmp_id}`, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getMyWorksites(cmp_id: number): Observable<any> {
		return this.http.get(`${this.configService.serviceUrl_Companies}worksite/my_worksites/${cmp_id}`, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getWorksiteRecordDetail(wsre_id: number): Observable<any> {
		return this.http.get(`${this.configService.serviceUrl_Companies}worksite/record/${wsre_id}`, { observe: 'response' }).pipe(map((response) => response.body));
	}

	getWorksiteByScan(wsre_id: number): Observable<any> {
		return this.http.get(`${this.configService.serviceUrl_Companies}worksite/wsbyscan/${wsre_id}`, { observe: 'response' }).pipe(map((response) => response.body));
	}

	getRapportinobyWorksite(ws_id: number, cmp_id: number): Observable<any> {
		return this.http.get(this.configService.serviceUrl_Companies + 'report/get_report_by_worksite/' + ws_id + '/' + cmp_id, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	CompilaRapportino(id_out: number, wsrp: WorksiteReport): Observable<any> {
		return this.http.post(`${this.configService.serviceUrl_Companies}report/compila_report/${id_out}`, wsrp, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	getNaturaImpresa(): Observable<any> {
		return this.http.get(this.configService.serviceUrl_Companies + 'companies/get_natura_impresa', { observe: 'response' }).pipe(map((response) => response.body));
	}

	GetCompany(cmp_id: number): Observable<any> {
		return this.http
			.get(`${this.configService.serviceUrl_Companies}companies/get?cmp_id=${cmp_id}`, {
				observe: 'response',
			})
			.pipe(
				map((response) => {
					return response.body;
				}),
			);
	}

	getChatMode(cmp_id: number): Observable<string> {
		return this.http.get(`${this.configService.serviceUrl_Companies}companies/chatmode?cmp_id=${cmp_id}`, { observe: 'response' }).pipe(map((response: any) => response.body?.cmp_chat_mode || 'COMPANY'));
	}

	// Logo azienda on-demand: le liste (cmp_companies_services_lst) non trasportano piu' il base64
	getCompanyLogo(cmp_id: number): Observable<string> {
		return this.http.get(`${this.configService.serviceUrl_Companies}companies/logo/${cmp_id}`, { observe: 'response' }).pipe(map((response: any) => response.body?.cmp_logo_b64 || ''));
	}

	GetCompanyGallery(cmp_id: number): Observable<any> {
		return this.http
			.get(`${this.configService.serviceUrl_Companies}company-gallery/list/${cmp_id}`, {
				observe: 'response',
			})
			.pipe(map((response) => response.body));
	}

	getAssignedCompanyRoles(user_id: number): Observable<any> {
		return this.http
			.get(`${this.configService.serviceUrl_Companies}roles/assignedlistbyuser/${user_id}`, {
				observe: 'response',
			})
			.pipe(map((response) => response.body));
	}
}
