import { Component } from '@angular/core';
import { PageBase } from 'src/app/core/base/page-base';
import { ToastService } from 'src/app/service/toast.service';
import { ConfirmationDialogService } from 'src/app/service/confirmation-dialog.service';
import { PermissionsEnum } from 'src/app/model/auth/permissionsEnum';
import { Domain } from 'src/app/model/entities/domain';
import { DomainsColumns } from 'src/app/modules/admin/administration/domains/domains.columns';
import { DomainsDataService } from 'src/app/modules/admin/administration/domains/domains.data.service';

@Component({
  selector: 'app-domains',
  templateUrl: './domains.page.html',
  styleUrl: './domains.page.scss',
})
export class DomainsPage extends PageBase<
  Domain,
  DomainsDataService,
  DomainsColumns
> {
  constructor(
    private toast: ToastService,
    private confirmation: ConfirmationDialogService
  ) {
    super(true, {
      read: [PermissionsEnum.domains],
      create: [PermissionsEnum.domainsCreate],
      update: [PermissionsEnum.domainsUpdate],
      delete: [PermissionsEnum.domainsDelete],
      restore: [PermissionsEnum.domainsDelete],
    });
  }

  load(silent?: boolean): void {
    if (!silent) this.loading = true;
    this.dataService
      .load(this.filter, this.sort, this.skip, this.take)
      .subscribe(result => {
        this.result = result;
        this.loading = false;
      });
  }

  delete(domain: Domain): void {
    this.confirmation.confirmDialog({
      header: 'dialog.delete.header',
      message: 'dialog.delete.message',
      accept: () => {
        this.loading = true;
        this.dataService.delete(domain).subscribe(() => {
          this.toast.success('action.deleted');
          this.load();
        });
      },
      messageParams: { entity: domain.name },
    });
  }

  restore(domain: Domain): void {
    this.confirmation.confirmDialog({
      header: 'dialog.restore.header',
      message: 'dialog.restore.message',
      accept: () => {
        this.loading = true;
        this.dataService.restore(domain).subscribe(() => {
          this.toast.success('action.restored');
          this.load();
        });
      },
      messageParams: { entity: domain.name },
    });
  }
}
