from django.contrib.auth.models import User
from django import forms
from personal_area.models import Credit, Deposit


class UserRegistrationForm(forms.ModelForm):
    password = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('username', 'first_name', 'email')

    def clean_password2(self):
        cd = self.cleaned_data
        if cd['password'] != cd['password2']:
            raise forms.ValidationError('Passwords don\'t match.')
        return cd['password2']


class GetCredit(forms.ModelForm):
    # start_sum = forms.CharField(label='amount')

    class Meta:
        model = Credit
        fields = ('start_sum', )

    def check(self):
        s = int(self.data['start_sum'])
        if s in (-2147483647, 2147483647):
            return s
        return -1


class PayCredit(forms.Form):
    amount = forms.IntegerField(label='amount')

    def check(self):
        s = int(self.data['amount'])
        if s in (-2147483647, 2147483647):
            return s
        return -1


class GetDeposit(forms.ModelForm):
    class Meta:
        model = Deposit
        fields = ('start_sum', )

    def check(self):
        s = int(self.data['start_sum'])
        if s in (-2147483647, 2147483647):
            return s
        return -1


class PayDeposit(forms.Form):
    amount = forms.IntegerField(label='amount')

    def check(self):
        s = int(self.data['amount'])
        if s in (-2147483647, 2147483647):
            return s
        return -1
