# Copyright (c) 2021, XhiveFramework Technologies and contributors
# For license information, please see license.txt

import xhiveframework
from xhiveframework.model.document import Document
from xhiveframework.realtime import get_website_room


class DiscussionReply(Document):
	def on_update(self):
		xhiveframework.publish_realtime(
			event="update_message",
			room=get_website_room(),
			message={"reply": xhiveframework.utils.md_to_html(self.reply), "reply_name": self.name},
			after_commit=True,
		)

	def after_insert(self):
		replies = xhiveframework.db.count("Discussion Reply", {"topic": self.topic})
		topic_info = xhiveframework.get_all(
			"Discussion Topic",
			{"name": self.topic},
			["reference_doctype", "reference_docname", "name", "title", "owner", "creation"],
		)

		template = xhiveframework.render_template(
			"xhiveframework/templates/discussions/reply_card.html",
			{
				"reply": self,
				"topic": {"name": self.topic},
				"loop": {"index": replies},
				"single_thread": True if not topic_info[0].title else False,
			},
		)

		sidebar = xhiveframework.render_template(
			"xhiveframework/templates/discussions/sidebar.html", {"topic": topic_info[0]}
		)

		new_topic_template = xhiveframework.render_template(
			"xhiveframework/templates/discussions/reply_section.html", {"topic": topic_info[0]}
		)

		xhiveframework.publish_realtime(
			event="publish_message",
			room=get_website_room(),
			message={
				"template": template,
				"topic_info": topic_info[0],
				"sidebar": sidebar,
				"new_topic_template": new_topic_template,
				"reply_owner": self.owner,
			},
			after_commit=True,
		)

	def after_delete(self):
		xhiveframework.publish_realtime(
			event="delete_message",
			room=get_website_room(),
			message={"reply_name": self.name},
			after_commit=True,
		)


@xhiveframework.whitelist()
def delete_message(reply_name):
	owner = xhiveframework.db.get_value("Discussion Reply", reply_name, "owner")
	if owner == xhiveframework.session.user:
		xhiveframework.delete_doc("Discussion Reply", reply_name)
