import { Component, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core"
import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms"
import { Agreement } from "../agreement.model"

import { currencyFormatFI } from "../../utils/string.utils"
import { JsonPipe } from "@angular/common"
import { InvestmentAmountComponent } from "../investment-amount/investment-amount.component"
import { InvestmentOriginComponent } from "../investment-origin/investment-origin.component"
import { SummaryComponent } from "../summary/summary.component"

@Component({
  standalone: true,
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  selector: "app-index-page",
  templateUrl: "./index-page.component.html",
  imports: [
    JsonPipe,
    ReactiveFormsModule,
    InvestmentAmountComponent,
    InvestmentOriginComponent,
    SummaryComponent
  ],
  styleUrls: ["./index-page.component.scss"]
})
export class IndexPageComponent {
  constructor(private formBuilder: FormBuilder) {}

  currentStep = 0
  completed = false

  form = this.formBuilder.group({
    investmentAmount: this.formBuilder.group({
      agreement: [null, [Validators.required]],
      additionalInvestment: 50
    }),
    investmentOrigin: this.formBuilder.group({
      salary: false,
      gift: false,
      heritage: false,
      entrepreneurialIncome: false,
      assets: false,
      otherIncome: false,
      investment: false,
      extras: null
    })
  })

  agreements: Record<number, Agreement> = {
    1: new Agreement("Vakuutus 1", 2500),
    2: new Agreement("Vakuutus 2", 1000)
  }

  get selectedAgreement() {
    return this.agreements[this.form.value.investmentAmount.agreement ?? 1]
  }

  formatNumber(value: number) {
    return currencyFormatFI.format(value)
  }

  handleStepChange(event: CustomEvent) {
    this.currentStep = event.detail.toStep
  }

  handleNextClick(event: Event) {
    event.preventDefault()
    this.currentStep++
  }

  handleSubmit(event: Event) {
    event.preventDefault()
    this.completed = true
  }
}
