import { Component, OnDestroy, OnInit } from '@angular/core';
import { IPluginComponent } from '@shared/plugins/interfaces/plugin-component';
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { ProductModuleSettingsHelper } from '@fleio-api/billing/products/product-module-settings-helper';
import { ConfigService } from '@shared/config/config.service';
import { Subscription } from 'rxjs';
import { DirectAdminApiService } from '@fleio-api/plugins/directadmin/directadmin-api.service';
import { IDirectAdminProductModel } from '@fleio-api/plugins/directadmin/model/direct-admin-product.model';

@Component({
  selector: 'app-product-settings',
  templateUrl: './product-settings.component.html',
  styleUrls: ['./product-settings.component.scss']
})
export class ProductSettingsComponent implements OnInit, OnDestroy, IPluginComponent {
  productSettingsForm = this.formBuilder.group({
    directadmin_product_id: ['', Validators.required],
  });
  directAdminProducts: IDirectAdminProductModel[] = null;
  public data: {
    productModuleSettingsHelper: ProductModuleSettingsHelper;
    productModuleSettings: any;
  };
  backendErrors = {};
  mainFormSubmittedSubscription: Subscription;
  fieldRequiredTxt = $localize`This field is required!`;
  loadingProducts = true;

  constructor(
    private formBuilder: UntypedFormBuilder,
    private directAdminApi: DirectAdminApiService,
    public config: ConfigService,
  ) {
  }

  displayFormErrors(formGroup: UntypedFormGroup) {
    Object.keys(formGroup.controls).map(name => {
      const control = formGroup.controls[name];
      if (control instanceof UntypedFormGroup) {
        this.displayFormErrors(control);
      } else {
        if (control.invalid) {
          control.markAsTouched();
        }
      }
    });
  }

  onMainFormSubmit() {
    if (this.productSettingsForm.invalid) {
      this.displayFormErrors(this.productSettingsForm);
    }
  }

  ngOnInit(): void {
    this.directAdminApi.getProducts().subscribe(response => {
      this.directAdminProducts = response;
    }).add(()=>{
      this.loadingProducts = false;
    });
    if (this.data) {
      this.data.productModuleSettingsHelper.form = this.productSettingsForm;
      this.mainFormSubmittedSubscription = this.data.productModuleSettingsHelper.observableMainFormSubmitted
        .subscribe(value => {
          if (value === true) {
            this.onMainFormSubmit();
          }
        });
    }
    if (this.data && this.data.productModuleSettings) {
      this.productSettingsForm.patchValue(this.data.productModuleSettings);
      this.productSettingsForm.addControl(
        'product', this.formBuilder.control(this.data.productModuleSettings.product)
      );
    }
    this.productSettingsForm.valueChanges.subscribe(value => {
      this.data.productModuleSettingsHelper.moduleSettings = value;
    });
  }

  ngOnDestroy() {
    if (this.data) {
      this.data.productModuleSettingsHelper.mainFormSubmitted.next(false);
    }
    if (this.mainFormSubmittedSubscription) {
      this.mainFormSubmittedSubscription.unsubscribe();
      this.mainFormSubmittedSubscription = null;
    }
  }

}
