// 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 { Component } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { CalendarOptions } from '@fullcalendar/core';
import dayGridPlugin from '@fullcalendar/daygrid';
import listPlugin from '@fullcalendar/list';
import interactionPlugin from '@fullcalendar/interaction';
import itLocale from '@fullcalendar/core/locales/it';

import { ConfigService } from '../../../providers/config.service';
import { LayoutService } from '../../../providers/layout.service';
import { AssembleeService } from '../../../providers/assemblee.service';
import { Assemblea } from '../../../models/assemblea.model';

@Component({
	selector: 'app-assemblee-calendar',
	templateUrl: './assemblee-calendar.page.html',
	styleUrls: ['./assemblee-calendar.page.scss'],
	standalone: false,
})
export class AssembleeCalendarPage {
	cmp_id = 0;
	prt_id = 0;

	calendarOptions: CalendarOptions = {
		plugins: [dayGridPlugin, listPlugin, interactionPlugin],
		initialView: 'dayGridMonth',
		locales: [itLocale],
		locale: 'it',
		firstDay: 1,
		height: 'auto',
		headerToolbar: { left: 'prev,next today', center: 'title', right: 'dayGridMonth,listMonth' },
		buttonText: { today: 'Oggi', month: 'Mese', list: 'Lista' },
		events: [],
		eventClick: this.onEventClick.bind(this),
	};

	constructor(
		private route: ActivatedRoute,
		private router: Router,
		private config: ConfigService,
		public layout: LayoutService,
		private service: AssembleeService,
	) {}

	ionViewWillEnter() {
		this.resolveIds();
		this.layout.setPageConfig({ title: 'Calendario assemblee', showBack: true, backButtonHref: `/portale/${this.prt_id}/company/${this.cmp_id}/service/assemblee/list` });
		this.load();
	}

	private resolveIds() {
		let r: ActivatedRoute | null = this.route;
		while (r) {
			const pm = r.snapshot.paramMap;
			if (pm.get('cmp_id')) this.cmp_id = +(pm.get('cmp_id') as string);
			if (pm.get('prt_id')) this.prt_id = +(pm.get('prt_id') as string);
			r = r.parent;
		}
	}

	load() {
		this.service.getAssemblee(this.cmp_id).subscribe((rows) => {
			const events: any[] = [];
			(rows || []).forEach((a: Assemblea) => {
				const color = this.colorFor(a);
				if (a.asm_first_call_dt) events.push({ title: a.asm_title + ' (1ª)', start: a.asm_first_call_dt, backgroundColor: color, borderColor: color, extendedProps: { asm_id: a.asm_id } });
				if (a.asm_second_call_dt) events.push({ title: a.asm_title + ' (2ª)', start: a.asm_second_call_dt, backgroundColor: color, borderColor: color, extendedProps: { asm_id: a.asm_id } });
			});
			this.calendarOptions = { ...this.calendarOptions, events };
		});
	}

	private colorFor(a: Assemblea): string {
		if (a.asm_status === 'annullata') return '#dc3545';
		if (a.asm_status === 'conclusa') return '#198754';
		return a.asm_type === 'straordinaria' ? '#408ee2' : '#0F75BB';
	}

	onEventClick(info: any) {
		const id = info?.event?.extendedProps?.asm_id;
		if (id) this.router.navigate([`/portale/${this.prt_id}/company/${this.cmp_id}/service/assemblee/detail`, id]);
	}
}
