# frozen_string_literal: true

require "em-http"
require "em_promise"
require "em-synchrony/em-http" # For aget vs get
require "money/bank/open_exchange_rates_bank"
require "nokogiri"

require_relative "em"

class CryptoSellPrices
	def initialize(redis, oxr_app_id)
		@redis = redis
		@oxr = Money::Bank::OpenExchangeRatesBank.new(
			Money::RatesStore::Memory.new
		)
		@oxr.app_id = oxr_app_id
	end

	def usd
		EMPromise.all([cad, cad_to_usd]).then { |(a, b)| a * b }
	end

	def ticker_row_selector
		raise NotImplementedError, "Subclass must implement"
	end

	def crypto_name
		raise NotImplementedError, "Subclass must implement"
	end

	def cad
		fetch_canadianbitcoins.then do |http|
			cb = Nokogiri::HTML.parse(http.response)

			row = cb.at(self.ticker_row_selector)
			unless row.at("td").text == self.crypto_name
				raise "#{crypto_name} row has moved"
			end

			BigDecimal(
				row.at("td:nth-of-type(4)").text.match(/^\$(\d+\.\d+)/)[1]
			)
		end
	end

protected

	def fetch_canadianbitcoins
		EM::HttpRequest.new(
			"https://www.canadianbitcoins.com",
			tls: { verify_peer: true }
		).aget
	end

	def cad_to_usd
		@redis.get("cad_to_usd").then do |rate|
			next rate.to_f if rate

			EM.promise_defer {
				# OXR gem is not async, so defer to threadpool
				@oxr.update_rates
				@oxr.get_rate("CAD", "USD")
			}.then do |orate|
				@redis.setex("cad_to_usd", 60 * 60, orate).then { orate }
			end
		end
	end
end

class BCHSellPrices < CryptoSellPrices
	def crypto_name
		"Bitcoin Cash"
	end

	def ticker_row_selector
		"#ticker > table > tbody > tr:nth-of-type(2)"
	end
end

class BTCSellPrices < CryptoSellPrices
	def crypto_name
		"Bitcoin"
	end

	def ticker_row_selector
		"#ticker > table > tbody > tr"
	end
end
