import { Injectable } from "@angular/core";

import {
  InvestmentProposalForm,
  GeneralInfo,
  ComplementInfo
} from "@app/models/investment-proposal-form";
import { InvestmentProposalWorkflowService } from "@app/services/investment-proposal-workflow.service";
import { STEPS } from "@app/models/investment-proposal-workflow";

@Injectable({
  providedIn: "root"
})
export class FormInvestmentProposalService {
  private formData: InvestmentProposalForm = new InvestmentProposalForm();
  private instrumentData: any;
  private isGeneralInfoFormValid: boolean = false;
  private isWorkFormValid: boolean = false;
  private isComplementInfoFormValid: boolean = false;

  constructor(private workflowService: InvestmentProposalWorkflowService) {}

  getGeneralInfo(): GeneralInfo {
    // Return the GeneralInfo data
    var general_info: GeneralInfo = {
      asunto: this.formData.asunto,
      origenes_fondo: this.formData.origenes_fondo,
      name: this.formData.name,
      tipo_tasa: this.formData.tipo_tasa,
      tipo_renta: this.formData.tipo_renta,
      periodicidad: this.formData.periodicidad,

      instrumentos: this.formData.instrumentos,
      base_anual: this.formData.base_anual,
      casa: this.formData.casa,
      formato_ingreso: this.formData.formato_ingreso
    };
    return general_info;
  }

  setGeneralInfo(data: GeneralInfo) {
    // Update the general_info data only when the general_info Form had been validated successfully
    this.isGeneralInfoFormValid = true;
    this.formData.asunto = data.asunto;
    this.formData.origenes_fondo = data.origenes_fondo;
    this.formData.name = data.name;
    this.formData.tipo_tasa = data.tipo_tasa;
    this.formData.tipo_renta = data.tipo_renta;
    this.formData.periodicidad = data.periodicidad;

    this.formData.instrumentos = data.instrumentos;
    this.formData.base_anual = data.base_anual;
    this.formData.casa = data.casa;
    this.formData.formato_ingreso = data.formato_ingreso;
    // Validate general_info Step in Workflow
    this.workflowService.validateStep(STEPS.general);
  }

  getWork(): string {
    // Return the work type
    return this.instrumentData || undefined;
  }

  setWork(data: any) {
    // Update the work type only when the Work Form had been validated successfully
    this.isWorkFormValid = true;
    //this.formData.work = data;
    this.instrumentData = data;
    // Validate Work Step in Workflow
    this.workflowService.validateStep(STEPS.work);
  }

  // Return the complement info of an investment proposal
  getComplementInfo(): ComplementInfo {
    var complement_info: ComplementInfo = {
      tipo_mercado: this.formData.tipo_mercado,
      emisores: this.formData.emisores,
      empresa: this.formData.empresa,
      pais: this.formData.pais,
      comentarios: this.formData.comentarios,
      justificacion: this.formData.justificacion,
      plazo: this.formData.plazo,
      operaciones: this.formData.operaciones
    };
    return complement_info;
  }

  // Update the complement info data only when the form had been validated successfully
  setComplementInfo(data: ComplementInfo) {
    this.isComplementInfoFormValid = true;
    this.formData.tipo_mercado = data.tipo_mercado;
    this.formData.emisores = data.emisores;
    this.formData.empresa = data.empresa;
    this.formData.pais = data.pais;
    this.formData.plazo = data.plazo;
    this.formData.operaciones = data.operaciones;
    this.formData.comentarios = data.comentarios;
    this.formData.justificacion = data.justificacion;

    // Validate complement_info Step in Workflow
    this.workflowService.validateStep(STEPS.complement);
  }

  getFormData(): InvestmentProposalForm {
    // Return the entire Form Data
    return this.formData;
  }

  resetFormData(): InvestmentProposalForm {
    // Reset the workflow
    // Return the form data after all this.* members had been reset
    this.formData.clear();
    this.instrumentData = undefined;
    this.setWork(this.instrumentData);
    this.isGeneralInfoFormValid = this.isWorkFormValid = this.isComplementInfoFormValid = false;
    this.workflowService.resetSteps();
    return this.formData;
  }

  isFormValid() {
    // Return true if all forms had been validated successfully; otherwise, return false
    return (
      this.isGeneralInfoFormValid &&
      this.isWorkFormValid &&
      this.isComplementInfoFormValid
    );
  }
}
