import {
  Component,
  OnInit,
  Input,
  ViewChild,
  ComponentFactoryResolver
} from "@angular/core";
import { Router } from "@angular/router";
import { InvestmentProposalForm } from "@app/models/investment-proposal-form";
import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
import { Instrument } from "@app/models/instrument";
import { InvestmentProposalWorkflowService } from "@app/services/investment-proposal-workflow.service";
import { InstrumentDirective } from "../instrument/instrument.directive";
import { InstrumentComponent } from "../instrument/instrument.component";
import { InstrumentsService } from "@app/services/instruments.service";

@Component({
  selector: "app-investment-proposal-changes",
  templateUrl: "./change-history.component.html",
  styleUrls: ["./change-history.component.scss"]
})
export class ChangeHistoryComponent implements OnInit {
  title = "Histórico de la propuesta";
  @Input() ads: Instrument[];
  @Input() formData: InvestmentProposalForm;
  @ViewChild(InstrumentDirective, { static: true })
  adHost: InstrumentDirective;
  isFormValid: boolean = false;
  general: any;
  instrument: any;
  complement: any;
  final: any;
  workType: string;
  form: any;
  currentAdIndex = -1;
  interval: any;
  indexDynamicComponent: number;

  constructor(
    private router: Router,

    private formDataService: FormInvestmentProposalService,
    private componentFactoryResolver: ComponentFactoryResolver,
    private instrumentService: InvestmentProposalWorkflowService,
    private loadInstrumentsService: InstrumentsService
  ) {}

  ngOnInit() {
    this.formData = this.formDataService.getFormData();
    this.isFormValid = this.formDataService.isFormValid();
    this.ads = this.loadInstrumentsService.getInstruments();
    this.formData.instrumentos;

    this.indexDynamicComponent = this.ads.findIndex(
      x => x.component.name == this.formData.instrumentos
    );
    if (this.indexDynamicComponent >= 0) {
      this.loadComponent();
    } else {
      console.log("No existe el componente");
    }

    this.general = this.formDataService.getGeneralInfo();
    this.instrument = this.formDataService.getWork();
    this.complement = this.formDataService.getComplementInfo();

    this.final = {};

    Object.assign(this.final, this.general, this.instrument, this.complement);
    console.log("Result feature loaded!");
    console.log(this.final);
  }

  loadComponent() {
    this.currentAdIndex = this.indexDynamicComponent % this.ads.length;
    const adItem = this.ads[this.currentAdIndex];
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
      adItem.component
    );

    const viewContainerRef = this.adHost.viewContainerRef;
    viewContainerRef.clear();

    const componentRef = viewContainerRef.createComponent(componentFactory);
    (<InstrumentComponent>componentRef.instance).data = adItem.data;
    (<InstrumentComponent>componentRef.instance).summary = true;
  }

  submit() {
    console.log(this.final);

    this.formData = this.formDataService.resetFormData();
    this.isFormValid = false;
  }
}
