import { Injectable } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

import dayjs from 'dayjs/esm';
import { DATE_TIME_FORMAT } from 'app/config/input.constants';
import { SearchEmissionFactor } from '../emission-factor.model';
import { SearchFormService } from 'app/entities/shared/shared-search-form.service';

/**
 * Type for createFormGroup and resetForm argument.
 * It accepts SearchUnit, for search. in the list
 */
type EmissionFactorSearchFormGroupInput = SearchEmissionFactor;

type EmissionFactorSearchFormDefaults = SearchEmissionFactor;

type EmissionFactorSearchFormGroupContent = {
  id: FormControl<SearchEmissionFactor['id']>;
  year: FormControl<SearchEmissionFactor['year']>;
  emissionFactorNameOrCode: FormControl<SearchEmissionFactor['emissionFactorNameOrCode']>;
  code: FormControl<SearchEmissionFactor['code']>;
  name: FormControl<SearchEmissionFactor['name']>;
  unit: FormControl<SearchEmissionFactor['unit']>;
  source: FormControl<SearchEmissionFactor['source']>;
  comments: FormControl<SearchEmissionFactor['comments']>;
  version: FormControl<SearchEmissionFactor['version']>;
  variableScopeId: FormControl<SearchEmissionFactor['variableScopeId']>;
  variableCategoryId: FormControl<SearchEmissionFactor['variableCategoryId']>;
};

export type EmissionFactorSearchFormGroup = FormGroup<EmissionFactorSearchFormGroupContent>;

@Injectable({ providedIn: 'root' })
export class EmissionFactorSearchFormService extends SearchFormService {
  createEmissionFactorSearchFormGroup(emissionFactor: EmissionFactorSearchFormGroupInput = { id: null }): EmissionFactorSearchFormGroup {
    const emissionFactorRawValue = {
      ...this.getSearchFormDefaults(),
      ...emissionFactor,
    };
    return new FormGroup<EmissionFactorSearchFormGroupContent>({
      id: new FormControl(emissionFactorRawValue.id),
      year: new FormControl(emissionFactorRawValue.year, {
        validators: [Validators.required, Validators.pattern('^[0-9]*$')],
      }),
      emissionFactorNameOrCode: new FormControl(emissionFactorRawValue.emissionFactorNameOrCode),
      code: new FormControl(emissionFactorRawValue.code),
      name: new FormControl(emissionFactorRawValue.name),
      unit: new FormControl(emissionFactorRawValue.unit),
      source: new FormControl(emissionFactorRawValue.source),
      comments: new FormControl(emissionFactorRawValue.comments),
      version: new FormControl(emissionFactorRawValue.version),
      variableScopeId: new FormControl(emissionFactorRawValue.variableScopeId, {
        validators: [Validators.required],
      }),
      variableCategoryId: new FormControl(emissionFactorRawValue.variableCategoryId),
    });
  }

  getEmissionFactor(form: EmissionFactorSearchFormGroup): SearchEmissionFactor {
    return {
      ...form.getRawValue(),
      year: form.getRawValue().year ? Number(form.getRawValue().year) : null,
    };
    //return form.getRawValue() as SearchEmissionFactor;
  }

  resetForm(form: EmissionFactorSearchFormGroup, emissionFactor: EmissionFactorSearchFormGroupInput): void {
    const emissionFactorRawValue = { ...this.getSearchFormDefaults(), ...emissionFactor };
    form.reset(
      {
        ...emissionFactorRawValue,
        id: { value: emissionFactorRawValue.id, disabled: true },
      } as any /* cast to workaround https://github.com/angular/angular/issues/46458 */,
    );
  }

  private getSearchFormDefaults(): EmissionFactorSearchFormDefaults {
    const currentTime = dayjs();

    return {
      id: null,
    };
  }
}
