import { inject, Injectable } from '@angular/core'
import { FeatureKey } from '@core/types'
import { isFeatureEnabled } from '@core/utils'
import { DefaultService, FeaturesSettings } from '@api-client/angular'
import { BehaviorSubject, map, Observable, skip, switchMap, tap } from 'rxjs'
import { I18nService } from '../../core'
import { Navigation } from '../types'
import { LucideCalendarDays, LucideChartBarBig, LucideCog, LucideLayoutDashboard } from '@lucide/angular'

@Injectable({
	providedIn: 'root',
})
export class NavigationService {
	private readonly _i18nService = inject(I18nService)
	private readonly _apiService = inject(DefaultService)

	private readonly _navigationSubject = new BehaviorSubject<Navigation>({
		compact: [],
		default: [],
		futuristic: [],
		horizontal: [],
	})
	readonly navigation$ = this._navigationSubject.asObservable()

	constructor() {
		// Re-create navigation after language change
		this._i18nService.language$
			.pipe(
				skip(1),
				switchMap(() => this.mapNavigation()),
			)
			.subscribe()
	}

	mapNavigation(): Observable<Navigation> {
		return this._apiService.settingsControllerFind().pipe(
			map((settings) => this.getDefaultNavigation(settings.features)),
			tap((navigation) => this._navigationSubject.next(navigation)),
		)
	}

	private getDefaultNavigation(features?: FeaturesSettings): Navigation {
		return {
			default: [
				{
					id: 'dashboard',
					title: this._i18nService.translate('title.Dashboard'),
					type: 'basic',
					icon: LucideLayoutDashboard,
					link: '/dashboard',
				},
				{
					id: 'reservations',
					title: this._i18nService.translate('title.Reservations'),
					type: 'basic',
					icon: LucideCalendarDays,
					link: '/reservations',
					hidden: () => !isFeatureEnabled(FeatureKey.Reservation, features),
				},
				{
					id: 'statistics',
					title: this._i18nService.translate('title.Statistics'),
					type: 'basic',
					icon: LucideChartBarBig,
					link: '/statistics',
					hidden: () => !isFeatureEnabled(FeatureKey.Statistics, features),
				},
				{
					id: 'settings',
					title: this._i18nService.translate('title.Settings'),
					type: 'basic',
					icon: LucideCog,
					link: '/settings',
				},
			],
			compact: [
				{
					id: 'dashboard',
					title: this._i18nService.translate('title.Dashboard'),
					type: 'basic',
					icon: LucideLayoutDashboard,
					link: '/dashboard',
				},
				{
					id: 'reservations',
					title: this._i18nService.translate('title.Reservations'),
					type: 'basic',
					icon: LucideCalendarDays,
					link: '/reservations',
					hidden: () => !isFeatureEnabled(FeatureKey.Reservation, features),
				},
				{
					id: 'statistics',
					title: this._i18nService.translate('title.Statistics'),
					type: 'basic',
					icon: LucideChartBarBig,
					link: '/statistics',
					hidden: () => !isFeatureEnabled(FeatureKey.Statistics, features),
				},
				{
					id: 'settings',
					title: this._i18nService.translate('title.Settings'),
					type: 'basic',
					icon: LucideCog,
					link: '/settings',
				},
			],
			futuristic: [],
			horizontal: [],
		}
	}
}
