import { Maybe } from '@/core';
import { MustMatch } from '@/core/validators';
import { AbstractFormDialog } from '@/shared/abstractClasses';
import { InputComponent, SharedPasswordInputComponent } from '@/shared/components';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { onSelectFileArgs } from '@/shared/components/uploadFile/model';
import { SharedUploadFileComponent } from '@/shared/components/uploadFile/upload-file.component';
import { buildFormData } from '@/utils';
import { Component, computed, inject, Input, signal } from '@angular/core';
import { ReactiveFormsModule, Validators } from '@angular/forms';
import { Divider } from 'primeng/divider';
import { IPartnerRequest, IPartnerResponse } from '../models';
import { PartnersService } from '../services/main.service';

@Component({
  selector: 'partner-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    InputComponent,
    FormFooterActionsComponent,
    Divider,
    SharedPasswordInputComponent,
    SharedUploadFileComponent,
  ],
})
export class GuildFormComponent extends AbstractFormDialog<IPartnerRequest, IPartnerResponse> {
  @Input() partnerId?: string;
  private service = inject(PartnersService);

  private initForm() {
    const form = this.fb.group({
      name: [this.initialValues?.name, [Validators.required]],
      code: [this.initialValues?.code, [Validators.required]],
      username: [''],
      password: [''],
      confirmPassword: [''],
    });

    if (!this.editMode) {
      form.addControl(
        'username',
        this.fb.control<string>('', {
          nonNullable: true,
          validators: [Validators.required],
        }),
      );
      form.addControl(
        'password',
        this.fb.control<string>('', {
          nonNullable: true,
          validators: [Validators.required],
        }),
      );
      form.addControl(
        'confirmPassword',
        this.fb.control<string>('', {
          nonNullable: true,
          validators: [Validators.required],
        }),
      );
      form.addValidators([MustMatch('password', 'confirmPassword')]);
    } else {
      // @ts-ignore
      form.removeControl('username');
      // @ts-ignore
      form.removeControl('password');
      // @ts-ignore
      form.removeControl('confirmPassword');
      form.removeValidators([MustMatch('password', 'confirmPassword')]);
    }

    return form;
  }

  logo = signal<Maybe<File>>(null);

  form = this.initForm();

  preparedTitle = computed(() => `${this.editMode ? 'ویرایش' : 'ایجاد'} شریک تجاری`);

  changeFile(payload: onSelectFileArgs) {
    this.logo.set(payload.file);
  }

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

  override submitForm(payload: IPartnerRequest) {
    const formData = buildFormData(this.form, { logo: this.logo() }, ['confirmPassword']);

    if (this.editMode) {
      return this.service.update(this.partnerId!, formData);
    }
    return this.service.create(formData);
  }
}
