import { AbstractFormDialog } from '@/shared/abstractClasses';
import { NameComponent } from '@/shared/components';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { fieldControl } from '@/shared/constants';
import { Component, computed, inject, Input } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { IDeviceBrandRequest, IDeviceBrandResponse } from '../models';
import { DeviceBrandsService } from '../services/main.service';

@Component({
  selector: 'deviceBrand-form',
  templateUrl: './form.component.html',
  imports: [ReactiveFormsModule, SharedDialogComponent, FormFooterActionsComponent, NameComponent],
})
export class DeviceBrandFormComponent extends AbstractFormDialog<
  IDeviceBrandRequest,
  IDeviceBrandResponse
> {
  @Input() brandId?: string;
  private service = inject(DeviceBrandsService);

  form = this.fb.group({
    name: fieldControl.name(this.initialValues?.name),
  });

  preparedTitle = computed(() => `${this.editMode ? 'ویرایش' : 'افزودن'} برند دستگاه`);

  override ngOnChanges() {
    this.form.patchValue(this.initialValues ?? {});
    if (this.editMode && !this.brandId) {
      throw 'missing some arguments';
    }
  }

  override submitForm(payload: IDeviceBrandRequest) {
    if (this.editMode) {
      return this.service.update(this.brandId!, payload);
    }
    return this.service.create(payload);
  }
}
