# frozen_string_literal: true

class Registration
	def self.for(iq, customer, web_register_manager)
		raise "TODO" if customer&.active?

		EMPromise.resolve(customer&.registered?).then do |registered|
			if registered
				Registered.new(iq, result.phone)
			else
				web_register_manager.choose_tel(iq).then do |(riq, tel)|
					Activation.for(riq, customer, tel)
				end
			end
		end
	end

	class Registered
		def initialize(iq, tel)
			@reply = iq.reply
			@reply.status = :completed
			@tel = tel
		end

		def write
			@reply.note_type = :error
			@reply.note_text = <<~NOTE
				You are already registered with JMP number #{@tel}
			NOTE
			BLATHER << @reply
			nil
		end
	end

	class Activation
		def self.for(iq, customer, tel)
			return EMPromise.resolve(new(iq, customer, tel)) if customer

			# Create customer_id
			raise "TODO"
		end

		def initialize(iq, customer, tel)
			@reply = iq.reply
			reply.allowed_actions = [:next]

			@customer = customer
			@tel = tel
		end

		attr_reader :reply, :customer, :tel

		FORM_FIELDS = [
			{
				var: "activation_method",
				type: "list-single",
				label: "Activate using",
				required: true,
				options: [
					{
						value: "bitcoin",
						label: "Bitcoin"
					},
					{
						value: "credit_card",
						label: "Credit Card"
					},
					{
						value: "code",
						label: "Referral or Activation Code"
					}
				]
			},
			{
				var: "plan_name",
				type: "list-single",
				label: "What currency should your account balance be in?",
				required: true,
				options: [
					{
						value: "cad_beta_unlimited-v20210223",
						label: "Canadian Dollars"
					},
					{
						value: "usd_beta_unlimited-v20210223",
						label: "United States Dollars"
					}
				]
			}
		].freeze

		def write
			form = reply.form
			form.type = :form
			form.title = "Activate JMP"
			form.instructions = "Going to activate #{tel} (TODO RATE CTR)"
			form.fields = FORM_FIELDS

			COMMAND_MANAGER.write(reply).then { |iq|
				Payment.for(iq, customer, tel)
			}.then(&:write)
		end
	end

	module Payment
		def self.for(iq, customer, tel)
			case iq.form.field("activation_method")&.value&.to_s
			when "bitcoin"
				Bitcoin.new(iq, customer, tel)
			when "credit_card"
				raise "TODO"
			when "code"
				raise "TODO"
			else
				raise "Invalid activation method"
			end
		end

		class Bitcoin
			def initialize(iq, customer, tel)
				@reply = iq.reply
				reply.note_type = :info
				reply.status = :completed

				plan_name = iq.form.field("plan_name").value.to_s
				@customer = customer.with_plan(plan_name)
				@customer_id = customer.customer_id
				@tel = tel
				@addr = ELECTRUM.createnewaddress
			end

			attr_reader :reply, :customer_id, :tel

			def save
				EMPromise.all([
					REDIS.mset(
						"pending_tel_for-#{customer_id}", tel,
						"pending_plan_for-#{customer_id}", @customer.plan_name
					),
					@addr.then do |addr|
						REDIS.sadd("jmp_customer_btc_addresses-#{customer_id}", addr)
					end
				])
			end

			def note_text(amount, addr)
				<<~NOTE
					Activate your account by sending at least #{'%.6f' % amount} BTC to
					#{addr}

					You will receive a notification when your payment is complete.
				NOTE
			end

			def write
				EMPromise.all([
					@addr,
					save,
					BTC_SELL_PRICES.public_send(@customer.currency.to_s.downcase)
				]).then do |(addr, _, rate)|
					min = CONFIG[:activation_amount] / rate
					reply.note_text = note_text(min, addr)
					BLATHER << reply
					nil
				end
			end
		end
	end
end
