# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE

# License: MIT. See LICENSE

import jingrow
from jingrow import _
from jingrow.model.page import Page


class WebsiteSlideshow(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.types import DF
		from jingrow.website.pagetype.website_slideshow_item.website_slideshow_item import (
			WebsiteSlideshowItem,
		)

		header: DF.HTMLEditor | None
		slideshow_items: DF.Table[WebsiteSlideshowItem]
		slideshow_name: DF.Data

	# end: auto-generated types
	def validate(self):
		self.validate_images()

	def on_update(self):
		# a slide show can be in use and any change in it should get reflected
		from jingrow.website.utils import clear_cache

		clear_cache()

	def validate_images(self):
		"""atleast one image file should be public for slideshow"""
		files = map(lambda row: row.image, self.slideshow_items)
		if files:
			result = jingrow.get_all("File", filters={"file_url": ("in", list(files))}, fields="is_private")
			if any(file.is_private for file in result):
				jingrow.throw(_("All Images attached to Website Slideshow should be public"))


def get_slideshow(pg):
	if not pg.slideshow:
		return {}

	slideshow = jingrow.get_pg("Website Slideshow", pg.slideshow)

	return {
		"slides": slideshow.get({"pagetype": "Website Slideshow Item"}),
		"slideshow_header": slideshow.header or "",
	}
