import { NgModule } from "@angular/core";
import { ActivatedRouteSnapshot, RouterModule, RouterStateSnapshot, Routes, UrlTree } from "@angular/router";
import { HomeComponent } from "./home/home.component";
import { Observable, of } from "rxjs";
import {ConfigurationComponent} from "./configuration/configuration.component";
import {AdminMainComponent} from "./admin-main/admin-main.component";
import {AdminTestingComponent} from "./admin-testing/admin-testing.component";

export class AdminGuard  {

    constructor(
    ) {
    }

    canDeactivate(component: HomeComponent, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
        if(nextState?.url.indexOf('admin') !== -1){
            return of(true);
        }
        return of(false);
    }

}

const routes: Routes = [
    {
        path: '',
        component: HomeComponent,
        canDeactivate: [AdminGuard],
        children: [
            {
                path:'',
                component: AdminMainComponent,
                canDeactivate: [AdminGuard],
            },
            {
                path: 'configuration',
                component: ConfigurationComponent,
                canDeactivate: [AdminGuard],
            },
            {
                path:'testing',
                component: AdminTestingComponent,
                canDeactivate: [AdminGuard],
            }
            ]
    },

]

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
    providers: [AdminGuard],
})
export class AdminRoutingModule {}


