# frozen_string_literal: true

require_relative "./xep0122_field"

class BuyAccountCreditForm
	def initialize(customer)
		@customer = customer
	end

	AMOUNT_FIELD =
		XEP0122Field.new(
			"xs:decimal",
			range: (0..1000),
			var: "amount",
			label: "Amount of credit to buy",
			required: true
		).field

	def balance
		{
			type: "fixed",
			value: "Current balance: $#{'%.2f' % @customer.balance}"
		}
	end

	def add_to_form(form)
		@customer.payment_methods.then do |payment_methods|
			form.type = :form
			form.title = "Buy Account Credit"
			form.fields = [
				balance,
				payment_methods.to_list_single,
				AMOUNT_FIELD
			]
		end
	end
end
