import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { MatSlideToggleChange } from '@angular/material/slide-toggle';
import { Role } from '@core/domain-classes/role';
import { MatCheckboxChange } from '@angular/material/checkbox';
import { Page } from '@core/domain-classes/page';
import { Action } from '@core/domain-classes/action';
import { PageAction } from '@core/domain-classes/page-action';

@Component({
  selector: 'app-manage-role-presentation',
  templateUrl: './manage-role-presentation.component.html',
  styleUrls: ['./manage-role-presentation.component.scss']
})
export class ManageRolePresentationComponent implements OnInit {

  @Input() screens: Page[];
  @Input() operations: Action[];
  @Input() screenOperations: PageAction[];
  @Input() loading: boolean;
  @Input() loadingScreen: boolean;
  @Input() loadingOperation: boolean;
  @Input() role: Role;
  @Output() onManageRoleOperation: EventEmitter<Role> = new EventEmitter<Role>();
  constructor() { }

  ngOnInit(): void {

  }

  checkPageAction(pageId: string, actionId: string): boolean {
    const screenOperation = this.screenOperations.find(c => c.pageId === pageId && c.actionId === actionId);
    if (screenOperation) {
      return true;
    } else {
      return false;
    }
  }

  selecetAll(event: MatCheckboxChange) {
    if (event.checked) {
      this.screens.forEach(screen => {
        this.operations.forEach(operation => {
          if (this.checkPageAction(screen.id, operation.id)) {
            this.role.roleClaims.push({
              roleId: this.role.id,
              claimType: `${screen.name}_${operation.name}`,
              claimValue: '',
              pageId: screen.id,
              actionId: operation.id
            });
          }
        });
      });
    } else {
      this.role.roleClaims = [];
    }
  }

  checkPermission(screenId: string, operationId: string): boolean {
    const pageAction = this.role.roleClaims.find(c => c.pageId === screenId && c.actionId === operationId);
    if (pageAction) {
      return true;
    } else {
      return false;
    }
  }

  onPermissionChange(flag: MatSlideToggleChange, screen: Page, operation: Action) {
    if (flag.checked) {
      this.role.roleClaims.push({
        roleId: this.role.id,
        claimType: `${screen.name}_${operation.name}`,
        claimValue: '',
        pageId: screen.id,
        actionId: operation.id
      })
    } else {
      const roleClaimToRemove = this.role.roleClaims.find(c => c.actionId === operation.id && c.pageId === screen.id);
      const index = this.role.roleClaims.indexOf(roleClaimToRemove, 0);
      if (index > -1) {
        this.role.roleClaims.splice(index, 1);
      }
    }
  }

  onPageSelect(event: MatCheckboxChange, pageName: string) {
    if (event.checked) {
      this.screens.filter(c => c.name == pageName).forEach(screen => {
        this.operations.forEach(operation => {
          if (this.checkPageAction(screen.id, operation.id)) {
            this.role.roleClaims.push({
              roleId: this.role.id,
              claimType: `${screen.name}_${operation.name}`,
              claimValue: '',
              pageId: screen.id,
              actionId: operation.id
            });
          }
        });
      });
    } else {
      var screen = this.screens.find(c => c.name == pageName);
      this.role.roleClaims = this.role.roleClaims.filter(c => c.pageId != screen.id);
    }
  }

  saveRole(): void {
    this.onManageRoleOperation.emit(this.role);
  }
}
