import { inject } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { of, EMPTY, Observable } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { TranslateService } from '@ngx-translate/core';
import { AlertService } from 'app/core/util/alert.service';

import { IDashboardComponent } from '../dashboard-component.model';
import { DashboardComponentService } from '../service/dashboard-component.service';
import { SecurityAction} from 'app/security/security-action.model';
import { Resources }   from 'app/security/resources.model';

const dashboardComponentResolve = (route: ActivatedRouteSnapshot): Observable<null | IDashboardComponent> => {
  const id = route.params['id'];
  const dashboardComponentService = inject(DashboardComponentService);
  const router = inject(Router);
  const translate = inject(TranslateService);
  const alertService = inject(AlertService);

  if (id) {
    return inject(DashboardComponentService)
      .find(id)
      .pipe(
        mergeMap((dashboardComponent: HttpResponse<IDashboardComponent>) => {
          if (dashboardComponent.body) {
            let canAccess: boolean = false;
            let alertMessage: string = "";
            const entityDomain = dashboardComponent.body;

            const actionToken: SecurityAction | null = route.data['action'];
            const resourceToken: Resources | null = route.data['resource'];
            if (actionToken && resourceToken) {
              switch (actionToken) {
                case SecurityAction.CREATE:
                  canAccess = dashboardComponentService.canCreate();
                  alertMessage = translate.instant('error.createNotAllowedForState');
                  break;
                case SecurityAction.READ:
                  canAccess = dashboardComponentService.canRead(entityDomain);
                  alertMessage = translate.instant('error.readNotAllowedForState');
                  break;
                case SecurityAction.UPDATE:
                  canAccess = dashboardComponentService.canUpdate(entityDomain);
                  alertMessage = translate.instant('error.updateNotAllowedForState');
                  break;
                case SecurityAction.DELETE:
                  canAccess = dashboardComponentService.canDelete(entityDomain);
                  alertMessage = translate.instant('error.deleteNotAllowedForState');
                  break;
              }
            }

            if (canAccess) {
              const ut = of(dashboardComponent.body);
              return of(dashboardComponent.body);
            } else {
              // Entity state doesn't allow the action
              // Show alert message to user
              alertService.addAlert({
                type: 'danger' ,      // 'success' | 'danger' | 'warning' | 'info'
                message: alertMessage,
                timeout: 5000,        // Optional: auto-dismiss after 5 seconds
              });

              // Redirect to list page
              router.navigate(['/dashboard-component'], {
                state: { message: alertMessage }
              });

              return EMPTY;
            }

			      
          } else {
            inject(Router).navigate(['404']);
            return EMPTY;
          }
        }),
      );
  }
  return of(null);
};

export default dashboardComponentResolve;
