# Copyright (c) 2018, SolutionERP LLC and Contributors
# License: GNU General Public License v3. See license.txt

import xhiveframework
from xhiveframework import _

from xhiveerp.xhiveerp_integrations.doctype.gocardless_settings.gocardless_settings import (
	get_gateway_controller,
	gocardless_initialization,
)

no_cache = 1

expected_keys = ("redirect_flow_id", "reference_doctype", "reference_docname")


def get_context(context):
	context.no_cache = 1

	# all these keys exist in form_dict
	if not (set(expected_keys) - set(xhiveframework.form_dict.keys())):
		for key in expected_keys:
			context[key] = xhiveframework.form_dict[key]

	else:
		xhiveframework.redirect_to_message(
			_("Some information is missing"),
			_("Looks like someone sent you to an incomplete URL. Please ask them to look into it."),
		)
		xhiveframework.local.flags.redirect_location = xhiveframework.local.response.location
		raise xhiveframework.Redirect


@xhiveframework.whitelist(allow_guest=True)
def confirm_payment(redirect_flow_id, reference_doctype, reference_docname):

	client = gocardless_initialization(reference_docname)

	try:
		redirect_flow = client.redirect_flows.complete(
			redirect_flow_id, params={"session_token": xhiveframework.session.user}
		)

		confirmation_url = redirect_flow.confirmation_url
		gocardless_success_page = xhiveframework.get_hooks("gocardless_success_page")
		if gocardless_success_page:
			confirmation_url = xhiveframework.get_attr(gocardless_success_page[-1])(
				reference_doctype, reference_docname
			)

		data = {
			"mandate": redirect_flow.links.mandate,
			"customer": redirect_flow.links.customer,
			"redirect_to": confirmation_url,
			"redirect_message": "Mandate successfully created",
			"reference_doctype": reference_doctype,
			"reference_docname": reference_docname,
		}

		try:
			create_mandate(data)
		except Exception as e:
			xhiveframework.log_error("GoCardless Mandate Registration Error")

		gateway_controller = get_gateway_controller(reference_docname)
		xhiveframework.get_doc("GoCardless Settings", gateway_controller).create_payment_request(data)

		return {"redirect_to": confirmation_url}

	except Exception as e:
		xhiveframework.log_error("GoCardless Payment Error")
		return {"redirect_to": "/integrations/payment-failed"}


def create_mandate(data):
	data = xhiveframework._dict(data)
	xhiveframework.logger().debug(data)

	mandate = data.get("mandate")

	if xhiveframework.db.exists("GoCardless Mandate", mandate):
		return

	else:
		reference_doc = xhiveframework.db.get_value(
			data.get("reference_doctype"),
			data.get("reference_docname"),
			["reference_doctype", "reference_name"],
			as_dict=1,
		)
		xhiveerp_customer = xhiveframework.db.get_value(
			reference_doc.reference_doctype, reference_doc.reference_name, ["customer_name"], as_dict=1
		)

		try:
			xhiveframework.get_doc(
				{
					"doctype": "GoCardless Mandate",
					"mandate": mandate,
					"customer": xhiveerp_customer.customer_name,
					"gocardless_customer": data.get("customer"),
				}
			).insert(ignore_permissions=True)

		except Exception:
			xhiveframework.log_error("Gocardless: Unable to create mandate")
