import jingrow
from jingrow.model.page import Page
from jingrow.utils import rounded

from jcloud.utils import group_children_in_result


class Plan(Page):
	def get_price_for_interval(self, interval, currency):
		price_per_day = self.get_price_per_day(currency)

		if interval == "Daily":
			return price_per_day

		if interval == "Monthly":
			return rounded(price_per_day * 30)

	def get_price_per_day(self, currency):
		price = self.price_cny if currency == "CNY" else self.price_usd
		price_per_day = rounded(price / self.period, 2)
		return price_per_day

	@property
	def period(self):
		return jingrow.utils.get_last_day(None).day

	@classmethod
	def get_plans(cls, pagetype, fields=["*"], filters=None):
		filters = filters or {}
		fields.append("`tabHas Role`.role")
		filters.update({"enabled": True})
		plans = jingrow.get_all(
			pagetype, filters=filters, fields=fields, order_by="price_usd asc"
		)
		plans = filter_by_roles(plans)

		return plans


def filter_by_roles(plans):
	plans = group_children_in_result(plans, {"role": "roles"})

	out = []
	for plan in plans:
		if jingrow.utils.has_common(plan["roles"], jingrow.get_roles()):
			plan.pop("roles", "")
			out.append(plan)

	return out
