// 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 { ChangeDetectorRef, Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from '@angular/core';
//import { TipoDoc } from 'src/app/models/TipoDoc';

@Component({
	selector: 'app-filtri-form',
	templateUrl: 'filtri_form.component.html',
	styleUrls: ['filtri_form.component.scss'],
	standalone: false,
})
export class FiltriFormComponent implements OnInit, OnChanges {
	@Input() form: any;
	@Input() view = false;
	@Input() view_filter = false;
	@Input() mode_search = false;
	@Input() tipo_visualizzazione!: string;
	@Input() search_sottocartelle = false;
	@Input() file_worked = false;
	@Output() emitAll = new EventEmitter();

	campi: any = {};
	campi_0: any = {};
	campi_1: any = {};
	campi_2: any = {};
	start_search = false;
	start_reset = false;
	start_salva = false;
	attend_campi_1 = true;
	attend_campi_2 = true;
	attend_reset_campi_1 = false;
	attend_reset_campi_2 = false;

	constructor(private cdr: ChangeDetectorRef) {}

	ngOnInit() {}

	restituisciDati() {
		
		this.emitAll.emit({ campi: this.campi, search_sottocartelle: this.search_sottocartelle });
		this.attend_campi_1 = false;
		this.attend_campi_2 = false;
		this.start_search = false;
		/*if (this.tipo_visualizzazione === 'richieste_pratiche') {
			const loadReset = async () => {
				await this.reset();
			};
			loadReset();
		}*/
	}

	public resetDati() {
		this.attend_reset_campi_1 = false;
		this.attend_reset_campi_2 = false;

		this.start_reset = true;
		this.cdr.detectChanges();
		this.start_reset = false;
	}

	ngOnChanges(changes: SimpleChanges) {
		
		this.campi_0 = [];
		this.campi_1 = [];
		this.campi_2 = [];
		if (changes['form']) {
			//&& !changes['form'].firstChange
			this.form = changes['form'].currentValue;
			this.loadCampi();
		}

		if (changes['tipo_visualizzazione']) {
			this.tipo_visualizzazione = changes['tipo_visualizzazione'].currentValue;
			/*if (this.tipo_visualizzazione === 'richieste_pratiche' || this.tipo_visualizzazione === 'richieste_pratiche_search') {
				this.view_filter = true;
			}*/
		}
	}

	loadCampi() {
		
		if (this.form) {
			this.campi = JSON.parse(this.form);
			if (Array.isArray(this.campi)) {
				this.campi_0 = this.campi.filter((x: any) => x?.visible);
			} else {
				this.campi_1 = this.campi.pratica
					.filter((x: any) => x?.visible)
					.sort((a: any, b: any) => {
						if (a.row !== b.row) {
							return a.row - b.row;
						}
						return a.column - b.column;
					});
				if (this.campi_1) {
					this.attend_campi_1 = false;
				}
				this.campi_2 = this.campi.fields
					.filter((x: any) => x?.visible)
					.sort((a: any, b: any) => {
						if (a.row !== b.row) {
							return a.row - b.row;
						}
						return a.column - b.column;
					});
				if (this.campi_2) {
					this.attend_campi_2 = false;
				}
			}
		}
	}

	reset() {
		this.start_reset = true;
	}

	cerca() {
		this.start_search = true;
	}

	startCerca(event: any, type: number) {
		
		switch (type) {
			case 0:
				this.campi = event.campi;
				this.attend_campi_1 = true;
				this.attend_campi_2 = true;
				break;
			case 1:
				this.campi.pratica = event.campi;
				this.attend_campi_1 = true;
				if (!this.campi?.fields[0]) {
					this.attend_campi_2 = true;
				}
				break;
			case 2:
				this.campi.fields = event.campi;
				this.attend_campi_2 = true;
				break;
		}
		if (this.attend_campi_1 && this.attend_campi_2) {
			this.restituisciDati();
		}
	}

	salva() {
		
		this.start_salva = true;
	}

	doReset(event: any, type: number) {
		switch (type) {
			case 0:
				this.campi = event.campi;
				this.attend_reset_campi_1 = true;
				this.attend_reset_campi_2 = true;
				break;
			case 1:
				this.campi.pratica = event.campi;
				this.attend_reset_campi_1 = true;
				if (!this.campi?.fields[0]) {
					this.attend_reset_campi_2 = true;
				}
				break;
			case 2:
				this.campi.fields = event.campi;
				this.attend_reset_campi_2 = true;
				break;
		}
		if (this.attend_reset_campi_1 && this.attend_reset_campi_2) {
			this.resetDati();
		}
	}

	/*@HostListener('window:keydown', ['$event'])
	handleKeyboardEvent(event: KeyboardEvent) {
		if (this.tipo_visualizzazione == 'form_file') {
			if (event.ctrlKey && (event.key === 'ArrowLeft' || event.key === 'ArrowRight')) {
				event.preventDefault();
				this.tipo_segment_sel = this.tipo_segment_sel === 'fields' ? 'pratica' : 'fields';
			}
		}
	}*/
}
