import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable, of, Subscription } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { PartnerCategory } from 'src/app/models/partner-category.model';
import { PartnerCategoryService } from 'src/app/services/partner-category.service';

@Component({
  selector: 'app-parnter-category-create',
  templateUrl: './parnter-category-create.component.html',
  styleUrls: ['./parnter-category-create.component.scss']
})
export class ParnterCategoryCreateComponent implements OnInit, OnDestroy {
  formPartner: FormGroup;
  partnerCategory$: Observable<PartnerCategory[]>;
  partnerCategoryData: PartnerCategory;
  subscriptionPartnerCategory: Subscription;
  isloading: boolean;
  errorObject: any;

  constructor(
    private partnerCategoryService: PartnerCategoryService,
    private activeRoute: ActivatedRoute,
    private router: Router,
  ) {
    this.formPartner = new FormGroup({
      id: new FormControl(),
      name: new FormControl('', [Validators.required, Validators.minLength(10)]),
      partnerType: new FormControl('', [Validators.required]),
      paymentType: new FormControl('', [Validators.required]),
      descriptionType: new FormControl(''),
      descriptionPayment: new FormControl('')
    });
  }


  ngOnInit(): void {

    const id = this.activeRoute.snapshot.paramMap.get('id');

    if (id != null) {

      this.subscriptionPartnerCategory = this.partnerCategoryService.getPartnerCategoryById(id)
        .subscribe(result => {
          console.log(result);
          this.partnerCategoryData = result;
          this.populatePartnerCategory(this.partnerCategoryData);
        });
    }
  }

  ngOnDestroy(): void {
    if (this.subscriptionPartnerCategory) {
      this.subscriptionPartnerCategory.unsubscribe();
    }
  }

  public submitForm(): void {
    this.formPartner.markAllAsTouched();
    console.log(this.formPartner.valid);
    console.log(this.formPartner.errors);
    //    if (this.formPartner.valid) {
    const data = JSON.stringify(this.formPartner.value);
    const partner: PartnerCategory = JSON.parse(data);
    console.log(JSON.stringify(partner));
    this.partnerCategoryService.createOrReplace(this.formPartner.value).pipe(
      tap(() => {
        this.isloading = false;
        // TODO: INCLUIR MENSAGEM
        this.router.navigate(['/partner/category/home/category']);
      }),
      catchError((error) => {
        this.isloading = false;
        // TODO: INCLUIR MENSAGEM
        console.log(error);
        return of(error);
      })
    ).subscribe();

    console.log(JSON.stringify(this.formPartner.value));
  }

  public clearForm(): void {
    this.formPartner.reset();
  }


  populatePartnerCategory(data: PartnerCategory) {
    this.formPartner.patchValue({
      id: data.id,
      name: data.name,
      partnerType: data.partnerType,
      paymentType: data.paymentType,
      descriptionType: data.descriptionType,
      descriptionPayment: data.descriptionPayment,
    });

  }


}
