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

import influxframework
from influxframework import _

from influxerp.influxerp_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(influxframework.form_dict.keys())):
		for key in expected_keys:
			context[key] = influxframework.form_dict[key]

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


@influxframework.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": influxframework.session.user}
		)

		confirmation_url = redirect_flow.confirmation_url
		gocardless_success_page = influxframework.get_hooks("gocardless_success_page")
		if gocardless_success_page:
			confirmation_url = influxframework.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:
			influxframework.log_error("GoCardless Mandate Registration Error")

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

		return {"redirect_to": confirmation_url}

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


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

	mandate = data.get("mandate")

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

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

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

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