from django import forms
from profiles.models import UserProfile
from scrobbles.constants import Visibility
from scrobbles.models import Scrobble


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, instance=self.profile, **kwargs)

    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",
            "inaturalist_username",
            "inaturalist_user_id",
            "inaturalist_auto_import",
            "webdav_url",
            "webdav_user",
            "webdav_pass",
            "webdav_auto_import",
            "ntfy_url",
            "ntfy_enabled",
            "mopidy_api_url",
            "favorites_mopidy_playlist",
            "monthly_mopidy_playlist_pattern",
            "redirect_to_webpage",
            "blog_enabled",
            "enable_public_widgets",
            "widget_custom_css",
            "home_scrobble_limit",
            "live_now_playing",
            "weigh_in_units",
            "volume_unit",
            "default_water_format",
            "default_water_size",
            "water_reminder_enabled",
            "trends_disabled",
            "fasting_vacation_exempt",
            "fasting_periodic_hours",
            "fasting_full_hours",
        ]
        widgets = {
            "lastfm_password": forms.PasswordInput(render_value=True),
            "archivebox_password": forms.PasswordInput(render_value=True),
            "webdav_pass": forms.PasswordInput(render_value=True),
        }


MEDIA_TYPE_LABELS = {mt.value: mt.label for mt in Scrobble.MediaType}

INHERIT = ""


class BulkVisibilityForm(forms.Form):
    bulk_action = forms.ChoiceField(
        choices=[
            (Visibility.PUBLIC, "Public"),
            (Visibility.PRIVATE, "Private"),
        ],
        widget=forms.RadioSelect,
        required=False,
        label="Set all non-shared scrobbles to",
    )

    def __init__(self, *args, **kwargs):
        self.profile = kwargs.pop("profile")
        super().__init__(*args, **kwargs)
        media_types = Scrobble.MediaType.values
        choices = [
            (Visibility.PUBLIC, "Public"),
            (Visibility.SHARED, "Shared"),
            (Visibility.PRIVATE, "Private"),
        ]
        existing_overrides = self.profile.media_type_visibility or {}
        for mt in sorted(media_types):
            label = MEDIA_TYPE_LABELS.get(mt, mt)
            self.fields[f"media_type_{mt}"] = forms.ChoiceField(
                choices=choices,
                required=False,
                label=label,
                initial=existing_overrides.get(mt, Visibility.PRIVATE),
            )

    def clean(self):
        cleaned = super().clean()
        overrides = {}
        for mt in Scrobble.MediaType.values:
            val = cleaned.get(f"media_type_{mt}")
            if val:
                overrides[mt] = val
        cleaned["media_type_visibility"] = overrides
        return cleaned
