// import { CatalogRolesComponent } from '@/shared/catalog/roles';
import { AbstractFormDialog } from '@/shared/abstractClasses';
import {
  DeviceIdComponent,
  NameComponent,
  PosTypeComponent,
  ProviderIdComponent,
  SerialNumberComponent,
} 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/fields';
import { Component, inject, Input } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { IPosRequest, IPosResponse } from '../../models';
import { AdminPosesService } from '../../services/poses.service';

@Component({
  selector: 'superAdmin-consumer-pos-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    NameComponent,
    FormFooterActionsComponent,
    DeviceIdComponent,
    ProviderIdComponent,
    PosTypeComponent,
    SerialNumberComponent,
  ],
})
export class ConsumerPosFormComponent extends AbstractFormDialog<IPosRequest, IPosResponse> {
  private readonly service = inject(AdminPosesService);

  @Input({ required: true }) consumerId!: string;
  @Input({ required: true }) businessActivityId!: string;
  @Input({ required: true }) complexId!: string;
  @Input() posId!: string;

  initForm = () => {
    const form = this.fb.group({
      name: fieldControl.name(this.initialValues?.name || ''),
      serial_number: fieldControl.serial_number(this.initialValues?.serial_number || '', true),
      // model: [this.initialValues?.model || '', [Validators.required]],
      pos_type: fieldControl.pos_type(this.initialValues?.pos_type || ''),
      device_id: fieldControl.device_id(this.initialValues?.device?.id || ''),
      provider_id: fieldControl.provider_id(this.initialValues?.provider?.id || ''),
    });
    form.controls.pos_type.valueChanges.subscribe((value) => {
      if (value === 'PSP') {
        // @ts-ignore
        form.addControl(
          'device_id',
          this.fb.control<string>(this.initialValues?.device?.id ?? '', {
            nonNullable: true,
            validators: fieldControl.device_id('', true)[1],
          }),
        );
        // @ts-ignore
        form.addControl(
          'provider_id',
          this.fb.control<string>(this.initialValues?.provider?.id ?? '', {
            nonNullable: true,
            validators: fieldControl.provider_id('', true)[1],
          }),
        );
      } else {
        // @ts-ignore
        form.removeControl('device_id');
        // @ts-ignore
        form.removeControl('provider_id');
      }
    });
    return form;
  };

  form = this.initForm();

  get preparedTitle() {
    return `${this.editMode ? 'ویرایش' : 'افزودن'} پایانه فروش`;
  }

  override ngOnChanges(): void {
    this.form = this.initForm();
  }

  submitForm() {
    const formValue = this.form.value as IPosRequest;
    if (this.editMode) {
      return this.service.update(this.businessActivityId, this.complexId, this.posId, formValue);
    }
    return this.service.create(this.businessActivityId, this.complexId, formValue);
  }
}
