import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { BaseComponent } from 'src/app/base.component';
import { ActivatedRoute, Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { User } from '@core/domain-classes/user';
import { UserService } from '../user.service';
import { TranslationService } from '@core/services/translation.service';
import { PageAction } from '@core/domain-classes/page-action';
import { Action } from '@core/domain-classes/action';
import { PageService } from '@core/services/page.service';
import { PageActionService } from '@core/services/page-action.service';
import { ActionService } from '@core/services/action.service';
import { Page } from '@core/domain-classes/page';

@Component({
  selector: 'app-user-permission',
  templateUrl: './user-permission.component.html',
  styleUrls: ['./user-permission.component.scss']
})
export class UserPermissionComponent extends BaseComponent implements OnInit {

  screenOperations$: Observable<PageAction[]>
  screens$: Observable<Page[]>;
  operations$: Observable<Action[]>;
  loading$: Observable<boolean>;
  loadingScreen$: Observable<boolean>;
  loadingOperation$: Observable<boolean>;
  user: User;

  constructor(
    private activeRoute: ActivatedRoute,
    private router: Router,
    private toastrService: ToastrService,
    private screenService: PageService,
    private operationService: ActionService,
    private screenOperationService: PageActionService,
    private userService: UserService,
    private translationService: TranslationService) {
    super();
  }

  ngOnInit(): void {
    this.sub$.sink = this.activeRoute.data.subscribe(
      (data: { user: User }) => {
        this.user = data.user;
      });

    this.loadingOperation$ = this.operationService.loaded$
      .pipe(
        tap(loaded => {
          if (!loaded) {
            this.getOperations();
          }
        })
      )
    this.operations$ = this.operationService.entities$

    this.loadingScreen$ = this.screenService.loaded$
      .pipe(
        tap(loaded => {
          if (!loaded) {
            this.getScreens();
          }
        })
      )
    this.screens$ = this.screenService.entities$
    this.loading$ = this.screenOperationService.loaded$
    this.screenOperations$ = this.screenOperationService.entities$
    this.getScreenOperations();

  }

  getOperations(): void {
    this.operationService.getAll();
  }

  getScreens(): void {
    this.screenService.getAll();
  }

  getScreenOperations(): void {
    this.screenOperationService.getAll();
  }

  manageUserClaimOperation(user: User): void {
    this.sub$.sink = this.userService.updateUserClaim(user.userClaims, user.id).subscribe(() => {
      this.toastrService.success(this.translationService.getValue('USER_PERMISSION_UPDATED_SUCCESSFULLY'));
      this.router.navigate(['/users']);
    })
  }
}
