import { Component, OnInit } from '@angular/core';
import { NbDialogRef } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { Role } from '../../../entity/Auth';
import { DropDownOptions } from '../../../entity/dropDownOption';
import { FamilyMember } from '../../../entity/Member';
import { FamilyMemberService } from '../../../services/crudServices/family-member.service';
import { EnumUtils } from '../../../utilities/enum-utils';

@Component({
  selector: 'ngx-family-member-editor',
  templateUrl: './family-member-editor.component.html',
  styleUrls: ['./family-member-editor.component.scss']
})
export class FamilyMemberEditorComponent implements OnInit {
  GenderOptions = DropDownOptions.GenderOptions;
  Role = Role;
  isAdding: boolean = false;
  processing: boolean = false;
  data: FamilyMember;
  constructor(
    private familyMemberService: FamilyMemberService,
    private dlgRef: NbDialogRef<FamilyMemberEditorComponent>
  ) {

  }
  ngOnInit(): void {
    if (this.isAdding) {
      this.data = {} as FamilyMember;
    }
  }

  close() {
    this.dlgRef.close();
  }
  update() {
    if (this.processing == false) {

      this.processing = true;

      this.familyMemberService.createOrUpdate(this.data).pipe(first()).subscribe(result => {
        this.processing = false;

        this.dlgRef.close(true);
      });
    }
  }

  verbiageHasFlag(enumValue) {
    return EnumUtils.hasFlag(this.data.role, enumValue);
  }
  verbiageAddRemoveFlag(enumValue: Role, adding: boolean) {
    if (adding) {
      this.data.role |= enumValue;
    } else {
      this.data.role &= ~enumValue;
    }
  }
}

