from django import forms

from profiles.models import UserProfile


class UserProfileForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        self.profile = UserProfile.objects.filter(
            user=self.request.user
        ).first()
        if not self.profile:
            raise Exception
        super(UserProfileForm, self).__init__(
            *args, **kwargs, instance=self.profile
        )

    class Meta:
        model = UserProfile
        fields = [
            "timezone",
            "lastfm_username",
            "lastfm_password",
            "lastfm_auto_import",
            "retroarch_path",
            "retroarch_auto_import",
            "archivebox_username",
            "archivebox_password",
            "archivebox_url",
            "bgg_username",
            "lichess_username",
            "webdav_url",
            "webdav_user",
            "webdav_pass",
            "webdav_auto_import",
            "ntfy_url",
            "ntfy_enabled",
            "redirect_to_webpage",
        ]
        widgets = {
            "lastfm_password": forms.PasswordInput(render_value=True),
            "archivebox_password": forms.PasswordInput(render_value=True),
            "webdav_pass": forms.PasswordInput(render_value=True),
        }
