# frozen_string_literal: true

class AddBitcoinAddress
	def self.for(iq, alt_form, customer)
		if alt_form.parse(iq.form)[:add_btc_address]
			new(iq, customer)
		else
			DoNot.new(iq)
		end
	end

	def initialize(iq, customer)
		@reply = iq.reply
		@reply.status = :completed
		@customer = customer
	end

	def write
		@customer.add_btc_address.then do |addr|
			form.fields = [{
				var: "btc_address",
				type: "fixed",
				label: "Bitcoin Address",
				value: addr
			}]
			BLATHER << @reply
		end
	end

protected

	def form
		form = @reply.form
		form.type = :result
		form.title = "New Bitcoin Address"
		form.instructions = "Your new address has been created"
		form
	end

	class DoNot
		def initialize(iq)
			@reply = iq.reply
			@reply.status = :completed
		end

		def write
			BLATHER << @reply
		end
	end
end
