from django import forms
from .models import Package, PackageScreenshot, PackageUrl, PackageAuthor
from extra_views import InlineFormSetFactory
from django.utils.translation import gettext as _



class AuthorField(forms.CharField):
	def __init__(self, delimiter=",", **kwargs):
		self.delimiter = delimiter
		super().__init__(**kwargs)

	def to_python(self, value):
		"""Return a set of PackageAuthor objects"""
		# print("AuthorField.to_python()")
		# print(value)

		if self.disabled:
			return value

		if value in self.empty_values:
			return None
		elif isinstance(value, (list, set)):
			return value

		return [
			author
			for author
			in [
				author.strip() for author in value.split(self.delimiter)
			]
			if author
		]

	def prepare_value(self, value):
		if not value:
			return ""

		if isinstance(value, str):
			return value

		# Make a list of just the names
		author_names = [str(author) for author in value]

		# Case-insensitive sort
		sorted_author_names = sorted(author_names, key=str.casefold)

		return self.delimiter.join(sorted_author_names)



class RatingForm(forms.Form):
	score = forms.IntegerField(max_value=5, min_value=1)


class PackageEditForm(forms.ModelForm):
	created_by = AuthorField(label=_("Authors"), help_text="A comma-separated list of author names")

	class Meta:
		model = Package
		fields = [
			'name',
			'tags',
			'created_by',
			'created_at',
			'description',
		]


class PackageCreateForm(forms.ModelForm):
	created_by = AuthorField(label=_("Authors"), help_text="A comma-separated list of author names")

	class Meta:
		model = Package
		fields = [
			'file',
			'name',
			'tags',
			'created_by',
			'created_at',
			'description',
		]
		labels = {
			'created_at': _('Created'),
		}


class ScreenshotForm(forms.ModelForm):
	class Meta:
		model = PackageScreenshot
		# fields = [
		# 	'image',
		# ]
		fields = '__all__'


class ScreenshotInline(InlineFormSetFactory):
	model = PackageScreenshot
	# fields = [
	# 	'image',
	# ]
	fields = '__all__'


class PackageUrlInline(InlineFormSetFactory):
	model = PackageUrl
	fields = '__all__'



class PackageAuthorForm(forms.ModelForm):
	class Meta:
		model = PackageAuthor
		fields = ['name',]
