# -*- coding: utf-8 -*-
from __future__ import annotations

from crispy_forms.bootstrap import FormActions
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit
from django import forms
from django.contrib.auth.forms import (
    PasswordChangeForm as BasePasswordChangeForm,
)
from django.utils.translation import gettext_lazy as _

from hotpocket_backend.apps.ui.forms.base import Form


class ProfileForm(Form):
    INCLUDE_CANCEL = False

    username = forms.CharField(
        label=_('Username'),
        disabled=True,
        required=False,
    )
    first_name = forms.CharField(
        label=_('First name'),
        max_length=150,
        required=True,
    )
    last_name = forms.CharField(
        label=_('Last name'),
        max_length=150,
        required=True,
    )
    email = forms.EmailField(
        label=_('E-mail address'),
        required=True,
    )

    def get_layout_fields(self) -> list[str]:
        return [
            'username',
            'first_name',
            'last_name',
            'email',
        ]


class FederatedProfileForm(ProfileForm):
    username = forms.CharField(
        label=_('Username'),
        disabled=True,
        required=False,
    )
    first_name = forms.CharField(
        label=_('First name'),
        disabled=True,
        required=False,
    )
    last_name = forms.CharField(
        label=_('Last name'),
        disabled=True,
        required=False,
    )
    email = forms.EmailField(
        label=_('E-mail address'),
        disabled=True,
        required=False,
    )

    def get_submit_button(self) -> Submit:
        return Submit(
            'submit',
            _('Save'),
            css_class='btn btn-primary',
            disabled=True,
        )

    def clean(self) -> dict:
        return {}


class PasswordForm(BasePasswordChangeForm):
    def get_submit_button(self) -> Submit:
        return Submit(
            'submit',
            _('Save'),
            css_class='btn btn-primary',
        )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.helper = FormHelper(self)
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-md-3 col-form-label'
        self.helper.field_class = 'col-md-9'
        self.helper.attrs = {
            'id': self.__class__.__name__,
            'novalidate': '',
        }
        self.helper.layout = Layout(
            'old_password',
            'new_password1',
            'new_password2',
            FormActions(
                self.get_submit_button(),
                template='ui/ui/forms/formactions-horizontal.html',
            ),
        )


class FederatedPasswordForm(PasswordForm):
    old_password = forms.CharField(
        label=_('Old password'),
        disabled=True,
        required=False,
    )
    new_password1 = forms.CharField(
        label=_('New password'),
        disabled=True,
        required=False,
    )
    new_password2 = forms.CharField(
        label=_('New password confirmation'),
        disabled=True,
        required=False,
    )

    def get_submit_button(self) -> Submit:
        return Submit(
            'submit',
            _('Save'),
            css_class='btn btn-primary disable',
            disabled=True,
        )

    def clean(self) -> dict:
        return {}


class SettingsForm(Form):
    INCLUDE_CANCEL = False

    theme = forms.ChoiceField(
        label=_('Theme'),
        disabled=False,
        required=False,
        choices=[
            (None, _('BTHLabs')),
            ('bootstrap', _('Bootstrap')),
            ('cosmo', _('Cosmo')),
            ('solar', _('Solar')),
            ('sandstone', _('Sandstone')),
        ],
    )
    light_mode = forms.BooleanField(
        label=_('Use light mode'),
        required=False,
    )
    auto_load_embeds = forms.ChoiceField(
        label=_('Auto load embedded content'),
        required=False,
        choices=[
            (None, _('---')),
            (True, _('Yes')),
            (False, _('No')),
        ],
        help_text=_((
            'Auto loading embedded content (e.g. YouTube videos) means that '
            'you allow cookies from these sites.'
        )),
    )

    def get_layout_fields(self) -> list[str]:
        return [
            'theme',
            'light_mode',
            'auto_load_embeds',
        ]

    def get_submit_button(self) -> Submit:
        return Submit(
            'submit',
            _('Save'),
            css_class='btn btn-primary',
        )

    def clean(self) -> dict:
        result = super().clean()

        theme = result.get('theme', None)
        if not theme:
            result['theme'] = None

        auto_load_embeds = result.get('auto_load_embeds', None)
        if not auto_load_embeds:
            result['auto_load_embeds'] = None
        else:
            result['auto_load_embeds'] = (auto_load_embeds == 'True')

        return result
