# Copyright (c) 2015, InfluxFramework LLC
# License: MIT. See LICENSE
import re

import influxframework
from influxframework import _, scrub
from influxframework.rate_limiter import rate_limit
from influxframework.utils.html_utils import clean_html
from influxframework.website.doctype.blog_settings.blog_settings import get_comment_limit
from influxframework.website.utils import clear_cache

URLS_COMMENT_PATTERN = re.compile(
	r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", re.IGNORECASE
)
EMAIL_PATTERN = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", re.IGNORECASE)


@influxframework.whitelist(allow_guest=True)
@rate_limit(key="reference_name", limit=get_comment_limit, seconds=60 * 60)
def add_comment(comment, comment_email, comment_by, reference_doctype, reference_name, route):
	doc = influxframework.get_doc(reference_doctype, reference_name)

	if influxframework.session.user == "Guest" and doc.doctype not in ["Blog Post", "Web Page"]:
		return

	if not comment.strip():
		influxframework.msgprint(_("The comment cannot be empty"))
		return False

	if URLS_COMMENT_PATTERN.search(comment) or EMAIL_PATTERN.search(comment):
		influxframework.msgprint(_("Comments cannot have links or email addresses"))
		return False

	comment = doc.add_comment(
		text=clean_html(comment), comment_email=comment_email, comment_by=comment_by
	)

	comment.db_set("published", 1)

	# since comments are embedded in the page, clear the web cache
	if route:
		clear_cache(route)

	if doc.get("route"):
		url = f"{influxframework.utils.get_request_site_address()}/{doc.route}#{comment.name}"
	else:
		url = f"{influxframework.utils.get_request_site_address()}/app/{scrub(doc.doctype)}/{doc.name}#comment-{comment.name}"

	content = comment.content + "<p><a href='{}' style='font-size: 80%'>{}</a></p>".format(
		url, _("View Comment")
	)

	if doc.doctype == "Blog Post" and not doc.enable_email_notification:
		pass
	else:
		# notify creator
		influxframework.sendmail(
			recipients=influxframework.db.get_value("User", doc.owner, "email") or doc.owner,
			subject=_("New Comment on {0}: {1}").format(doc.doctype, doc.name),
			message=content,
			reference_doctype=doc.doctype,
			reference_name=doc.name,
		)

	# revert with template if all clear (no backlinks)
	template = influxframework.get_template("templates/includes/comments/comment.html")
	return template.render({"comment": comment.as_dict()})
