from django.forms import Form, fields
from django.core.exceptions import ValidationError


class RegisterForm(Form):
    username = fields.CharField(
        required=True,
        min_length=2,
        max_length=20,
        error_messages={
            'required': '用户名不可以为空！',
            'min_length': '用户名应大于2位！',
            'max_length': '用户名应小于20位！',
        },
    )

    nickname = fields.CharField(
        required=True,
        min_length=2,
        max_length=20,
        error_messages={
            'required': '昵称不可以为空！',
            'min_length': '昵称应大于2位！',
            'max_length': '昵称应小于20位！',
        },
    )

    password1 = fields.CharField(
        required=True,
        min_length=6,
        max_length=20,
        error_messages={
            'required': '密码不可以位空！',
            'min_length': '密码应大于6位！',
            'max_length': '密码应小于20位！',
        },
    )

    password2 = fields.CharField(
        required=True,
        error_messages={
            'required': '请再次输入密码！',
        },
    )

    email = fields.EmailField(
        required=True,
        error_messages={
            'required': '请随便填个邮箱吧...',
        },
    )

    def clean_password2(self):
        if not self.errors.get("password1"):
            if self.cleaned_data["password2"] != self.cleaned_data["password1"]:
                raise ValidationError("您输入的密码不一致，请重新输入！")
            return self.cleaned_data


class LoginForm(Form):
    username = fields.CharField(
        required=True,
        error_messages={
            'required': '请输入用户名',
        },
    )

    password = fields.CharField(
        required=True,
        error_messages={
            'required': '请输入密码',
        },
    )


class CreateForm(Form):
    text = fields.CharField(
        required=True,
        max_length=300,
        error_messages={
            'required': '请输入题目',
            'max_length': '不得超过300字'
        }
    )

    image = fields.ImageField(required=False)

    a = fields.CharField(
        required=True,
        max_length=300,
        error_messages={
            'required': '请输入选项',
            'max_length': '不得超过300字'
        }
    )

    b = fields.CharField(
        required=True,
        max_length=300,
        error_messages={
            'required': '请输入选项',
            'max_length': '不得超过300字'
        }
    )

    c = fields.CharField(
        required=True,
        max_length=300,
        error_messages={
            'required': '请输入选项',
            'max_length': '不得超过300字'
        }
    )

    d = fields.CharField(
        required=True,
        max_length=300,
        error_messages={
            'required': '请输入选项',
            'max_length': '不得超过300字'
        }
    )

    answer = fields.ChoiceField(choices=(('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')))


class AnswerForm(Form):
    questionID = fields.UUIDField()
    answer = fields.ChoiceField(choices=(('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')))
