import { Component, ChangeDetectorRef, ChangeDetectionStrategy, Input } from '@angular/core'
import {COMMA, ENTER} from '@angular/cdk/keycodes'

import { User, Role } from './models'
import { AdminManageAccessDataService } from './access-data.service'

@Component({
    selector: 'gisaf-admin-user',
    templateUrl: './user.component.html',
    styleUrls: ['./user.component.css'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AdminUserComponent {
    @Input() user: User
    readonly separatorKeysCodes: number[] = [ENTER, COMMA];

    constructor(
        private cdr: ChangeDetectorRef,
        public adminManageAccessDataService: AdminManageAccessDataService,
    ) {}

    drop(evt) {
        // Assign a role to the user
        this.adminManageAccessDataService.addUserRole(this.user, evt.item.data['role']).subscribe()
    }

    removeUserRole(user: User, role: Role) {
        this.adminManageAccessDataService.deleteUserRole(user, role).subscribe()
    }

    delete() {
        this.adminManageAccessDataService.deleteUser(this.user).subscribe(
            res => this.adminManageAccessDataService.init()
        )
    }

    edit() {
        this.adminManageAccessDataService.openUserDialog(this.user)
    }
}