import { Router } from '@angular/router'
import { Component, OnInit, inject } from '@angular/core'
import { RowArgs, GridComponent, SelectionDirective, CheckboxColumnComponent, ColumnComponent } from '@progress/kendo-angular-grid'

import { AlertService } from '../components/alert'
import { BreadcrumbService } from '../breadcrumb.service'
import { UserService } from '../../providers/user/user.service.'

@Component({
  standalone: true,
  selector: 'force-logout',
  templateUrl: './forcelogout.component.html',
  imports: [GridComponent, SelectionDirective, CheckboxColumnComponent, ColumnComponent]
})
export class ForceLogoutComponent implements OnInit
{
  private readonly router = inject(Router);
  private readonly service = inject(UserService);
  private readonly alertService = inject(AlertService);

  public items: any = [];
  public canEdit: boolean = false;
  public loading: boolean = false;
  public selectedIds: string[] = [];
  public remainingWidth: number = undefined;
  public selectedKeys(context: RowArgs): number { return context.dataItem.userId };

  constructor()
  {
    const bcService = inject(BreadcrumbService);

    bcService.setItems([{ label: 'System Setup' }, { label: 'User Management' }, { label: 'Force Logout', routerLink: ['/forcelogout'] }])
    if (!this.service.currentUserValue)
    {
      this.router.navigate(['/login']);
      return;
    }

    this.canEdit = this.service.currentUserValue?.moduleIds?.includes('ELIT.1.2.4_1')
      || this.service.currentUserValue?.moduleIds?.includes('ELIT.1.2.4_2')
      || this.service.currentUserValue?.moduleIds?.includes('ELIT.1.2.4_3');
  }

  ngOnInit()
  {
    this.remainingWidth = window.innerWidth <= 720 ? 230 : undefined;
    this.service.getUsersForForceLogout().subscribe(
      {
        next: (resp: any) =>
        {
          this.loading = false;
          if (resp.returnStatus === 200)
          {
            this.items = resp.value || [];
          }
          else
          {
            this.alertService.error(resp.returnMessage);
          }
        },
        error: (errorMessage: string) =>
        {
          this.loading = false;
          this.alertService.error(errorMessage);
        }
      })
  }

  public logoutForcbely(): void
  {
    this.alertService.clear();

    if (this.selectedIds == undefined || this.selectedIds == null || this.selectedIds.length <= 0)
    {
      this.alertService.error('Select at least one User to force Logout.');
      return;
    }

    this.loading = true;
    const params: any = { userIds: this.selectedIds };

    this.service.forceLogoutNow(params).subscribe(
      {
        next: (resp: any) =>
        {
          this.loading = false;
          if (resp.returnStatus === 200)
          {
            this.loading = false;
            this.alertService.success(resp.returnMessage);
          }
          else
          {
            this.alertService.error(resp.returnMessage);
          }
        },
        error: (errorMessage: string) =>
        {
          this.loading = false
          this.alertService.error(errorMessage);
        }
      });
  }
}
