import { Injectable } from '@angular/core';
import {
  Resolve,
  RouterStateSnapshot,
  ActivatedRouteSnapshot,
  Params
} from '@angular/router';

import { BehaviorSubject, Observable } from 'rxjs';

import { Dashboard } from 'src/modules/dashboard/model/Dashboard';
import { DashboardService } from 'src/modules/dashboard/service/dashboard.service';

@Injectable()
export class SettingsDashboardResolver implements Resolve<Dashboard[]> {
  dashboards: Dashboard[];
  currentDashboard: Dashboard;

  routeParams: Params;

  onDashboardsChanged: BehaviorSubject<Dashboard[]>;
  onCurrentDashboardChanged: BehaviorSubject<Dashboard>;

  constructor(private dashboardService: DashboardService) {
    this.onDashboardsChanged = new BehaviorSubject([]);
    this.onCurrentDashboardChanged = new BehaviorSubject(null);
  }

  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Dashboard[] | Observable<Dashboard[]> | Promise<Dashboard[]> {
    this.routeParams = route.params;

    return this.dashboardService.getDashboards();
  }
}
