from django import forms
from django.forms.models import modelformset_factory


from aircox import models


__all__ = ("CommentForm", "PageForm", "ProgramForm", "EpisodeForm", "SoundForm", "EpisodeSoundFormSet", "TrackFormSet")


class CommentForm(forms.ModelForm):
    nickname = forms.CharField()
    email = forms.EmailField(required=False)
    content = forms.CharField(widget=forms.Textarea())

    nickname.widget.attrs.update({"class": "input"})
    email.widget.attrs.update({"class": "input"})
    content.widget.attrs.update({"class": "textarea"})

    class Meta:
        model = models.Comment
        fields = ["nickname", "email", "content"]


class ImageForm(forms.Form):
    file = forms.ImageField()


class PageForm(forms.ModelForm):
    class Meta:
        fields = ("title", "status", "cover", "content")
        model = models.Page


class ProgramForm(PageForm):
    class Meta:
        fields = PageForm.Meta.fields
        model = models.Program


class EpisodeForm(PageForm):
    class Meta:
        model = models.Episode
        fields = PageForm.Meta.fields


class SoundForm(forms.ModelForm):
    """SoundForm used in EpisodeUpdateView."""

    class Meta:
        model = models.Sound
        fields = ["name", "program", "file", "broadcast", "duration", "is_public", "is_downloadable"]


class SoundCreateForm(forms.ModelForm):
    """SoundForm used in EpisodeUpdateView."""

    class Meta:
        model = models.Sound
        fields = ["name", "program", "file", "broadcast", "is_public", "is_downloadable"]


TrackFormSet = modelformset_factory(
    models.Track,
    fields=[
        "position",
        "episode",
        "artist",
        "title",
        "tags",
    ],
    widgets={"episode": forms.HiddenInput(), "position": forms.HiddenInput()},
    can_delete=True,
    extra=0,
)
"""Track formset used in EpisodeUpdateView."""


EpisodeSoundFormSet = modelformset_factory(
    models.EpisodeSound,
    fields=(
        "position",
        "episode",
        "sound",
        "broadcast",
    ),
    widgets={
        "broadcast": forms.CheckboxInput(),
        "episode": forms.HiddenInput(),
        # "sound": forms.HiddenInput(),
        "position": forms.HiddenInput(),
    },
    can_delete=True,
    extra=0,
)
