import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ToastService } from 'src/app/service/toast.service';
import { FormPageBase } from 'src/app/core/base/form-page-base';
import {
  Group,
  GroupCreateInput,
  GroupUpdateInput,
} from 'src/app/model/entities/group';
import { GroupsDataService } from 'src/app/modules/admin/groups/groups.data.service';
import { Role } from 'src/app/model/entities/role';
import { CommonDataService } from 'src/app/modules/shared/service/common-data.service';
import { FeatureFlagService } from 'src/app/service/feature-flag.service';

@Component({
  selector: 'app-group-form-page',
  templateUrl: './group-form-page.component.html',
  styleUrl: './group-form-page.component.scss',
})
export class GroupFormPageComponent extends FormPageBase<
  Group,
  GroupCreateInput,
  GroupUpdateInput,
  GroupsDataService
> {
  roles: Role[] = [];

  constructor(
    private features: FeatureFlagService,
    private toast: ToastService,
    private cds: CommonDataService
  ) {
    super();
    this.cds.getAllRoles().subscribe(roles => {
      this.roles = roles;
    });

    if (!this.nodeId) {
      this.node = this.new();
      this.setForm(this.node);

      return;
    }

    this.dataService
      .load([{ id: { equal: this.nodeId } }])
      .subscribe(apiKey => {
        this.node = apiKey.nodes[0];
        this.setForm(this.node);
      });
  }

  new(): Group {
    return {} as Group;
  }

  buildForm() {
    this.form = new FormGroup({
      id: new FormControl<number | undefined>(undefined),
      name: new FormControl<string | undefined>(undefined, Validators.required),
      roles: new FormControl<Role[]>([]),
    });
    this.form.controls['id'].disable();
  }

  setForm(node?: Group) {
    this.form.controls['id'].setValue(node?.id);
    this.form.controls['name'].setValue(node?.name);
    this.form.controls['roles'].setValue(node?.roles ?? []);
  }

  getCreateInput(): GroupCreateInput {
    return {
      name: this.form.controls['name'].pristine
        ? undefined
        : (this.form.controls['name'].value ?? undefined),
      roles: this.form.controls['roles'].pristine
        ? undefined
        : this.form.controls['roles'].value,
    };
  }

  getUpdateInput(): GroupUpdateInput {
    if (!this.node?.id) {
      throw new Error('Node id is missing');
    }

    return {
      id: this.form.controls['id'].value,
      name: this.form.controls['name'].pristine
        ? undefined
        : (this.form.controls['name'].value ?? undefined),
      roles: this.form.controls['roles'].pristine
        ? undefined
        : this.form.controls['roles'].value,
    };
  }

  create(apiKey: GroupCreateInput): void {
    this.dataService.create(apiKey).subscribe(() => {
      this.spinner.hide();
      this.toast.success('action.created');
      this.close();
    });
  }

  update(apiKey: GroupUpdateInput): void {
    this.dataService.update(apiKey).subscribe(() => {
      this.spinner.hide();
      this.toast.success('action.created');
      this.close();
    });
  }
}
