import { Component, inject, signal } from '@angular/core';
import { Group } from '../../../models/user-management.api.models';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { CommonModule } from '@angular/common';
import { MatTabsModule } from '@angular/material/tabs';
import { GroupService } from '../../../services/api/group.service';
import { RefreshService } from '../../../services/button/refresh.service';
import Swal from 'sweetalert2';
import { MatSelectModule } from '@angular/material/select';
import { env } from '../../../../environments/environment'
import { MatDividerModule } from '@angular/material/divider';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';

@Component({
  selector: 'app-group-update-form',
  standalone: true,
  imports: [MatFormFieldModule, MatInputModule, FormsModule, ReactiveFormsModule, MatIconModule, MatButtonModule, CommonModule, MatTabsModule, MatSelectModule, MatDividerModule, MatCheckboxModule, MatSlideToggleModule],
  templateUrl: './group-update-form.component.html',
  styleUrl: './group-update-form.component.scss'
})
export class GroupUpdateFormComponent {

  readonly dialogRef: MatDialogRef<GroupUpdateFormComponent> = inject(MatDialogRef<GroupUpdateFormComponent>);

  readonly group: Group = inject(MAT_DIALOG_DATA);

  get allowedDateFormats(): Array<{ value: string, name: string }> {
    return env.constants.date_formats;
  }

  get allowedLanguages(): Array<{ value: string, name: string }> {
    return env.constants.languages;
  }

  readonly name = new FormControl(this.group.name);
  readonly comment = new FormControl(this.group.comment);

  mailErrorMessage = signal('');
  errorMessage = signal('');

  constructor(private uService: GroupService, private rService: RefreshService) {
  }

  update() {

    this.group.name = this.name.value!;
    this.group.comment = this.comment.value!;

    this.uService.update(this.group).subscribe({
      next: () => {
        this.rService.executeAll();
      },
      error: err => {
        console.error(err)
        Swal.fire({
          title: "Interner Dienstfehler",
          text: "Bitte wenden Sie sich an das IT-Team, um den Fehler zu beheben.",
          icon: "error"
        });
      }
    })
  }

  delete() {
    Swal.fire({
      text: "Sind Sie sicher, dass Sie diesen Datensatz löschen wollen?",
      icon: "question",
      showDenyButton: true,
      confirmButtonText: "Ja",
      denyButtonText: `Nein`
    }).then((result) => {
      if (result.isConfirmed) {
        if (this.group.id) {
          this.uService.delete(this.group.id).subscribe({
            next: () => {
              this.rService.executeAll();
              this.dialogRef.close();
            },
            error: err => {
              Swal.fire({
                title: "Interner Dienstfehler",
                text: "Bitte wenden Sie sich an das IT-Team, um den Fehler zu beheben.",
                icon: "error"
              });
            },
          })
        }
        else
          Swal.fire({
            title: "Ein unerwarteter Fehler",
            text: "Die Benutzer-ID existiert nicht (Nullwert).",
            icon: "error"
          });
      }
    });
  }
}