import { Injectable } from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import {debug} from '../utility';
import {DatabasePageComponent} from '../components/editor/database/database-page.component';
import {FileBoxPageComponent} from '../components/nodes/file-box/file-box-page.component';
import {ModalController} from '@ionic/angular';
import {ApiService} from './api.service';

@Injectable({
    providedIn: 'root'
})
export class NavigationService {
    protected refreshCount = 0;
    public readonly sidebarRefresh$: BehaviorSubject<[number, boolean]> = new BehaviorSubject<[number, boolean]>([this.refreshCount, true]);
    public readonly navigationRequest$: BehaviorSubject<string> = new BehaviorSubject<string>('');
    public readonly initializationRequest$: BehaviorSubject<number> = new BehaviorSubject<number>(0);
    public readonly initialized$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

    constructor(
        protected readonly ionModalController: ModalController,
        protected readonly api: ApiService,
    ) { }

    requestSidebarRefresh({ quiet = false }) {
        this.refreshCount += 1;
        this.sidebarRefresh$.next([this.refreshCount, quiet]);
    }

    requestNavigation(pageId: string) {
        debug('Requesting page navigation:', pageId);
        debug('Page navigation trace', new Error());

        this.navigationRequest$.next(pageId);
    }

    requestInitialization() {
        debug('Requesting application initialization');
        this.initializationRequest$.next(this.initializationRequest$.getValue() + 1);
    }
}
