"""
Created on 08.05.2011

@author: christian
"""
from django.db import models
from django.db.models import ForeignKey  # @UnusedImport
from django.utils import six

from . import forms, widgets


class BooleanField(models.BooleanField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.BooleanField}
        defaults.update(kwargs)
        return super(BooleanField, self).formfield(**defaults)


class CharField(models.CharField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.CharField}
        defaults.update(kwargs)
        return super(CharField, self).formfield(**defaults)


class DateField(models.DateField):
    def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.DateField}
        defaults.update(kwargs)
        return super(DateField, self).formfield(**defaults)


class DateTimeField(models.DateTimeField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.DateTimeField}
        defaults.update(kwargs)
        return super(DateTimeField, self).formfield(**defaults)


class EmailField(models.EmailField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.EmailField}
        defaults.update(kwargs)
        return super(EmailField, self).formfield(**defaults)


class FileField(models.FileField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.FileField}
        defaults.update(kwargs)
        return super(FileField, self).formfield(**defaults)


class ForeignKey(models.ForeignKey):
    def formfield(self, **kwargs):
        db = kwargs.pop('using', None)
        if isinstance(self.rel.to, six.string_types):
            raise ValueError("Cannot create form field for %r yet, because "
                             "its related model %r has not been loaded yet" %
                             (self.name, self.rel.to))
        queryset = self.rel.to._default_manager.using(db)
        queryset = queryset.complex_filter(self.rel.limit_choices_to)
        defaults = {
            'form_class': forms.ModelChoiceField,
            'queryset': queryset,
            'to_field_name': self.rel.field_name,
        }
        defaults.update(kwargs)
        return super(ForeignKey, self).formfield(**defaults)


class ImageField(models.ImageField):
    pass


class IntegerField(models.IntegerField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.IntegerField}
        defaults.update(kwargs)
        return super(IntegerField, self).formfield(**defaults)


class NullBooleanField(models.NullBooleanField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.BooleanField}
        defaults.update(kwargs)
        return super(NullBooleanField, self).formfield(**defaults)


class PositiveSmallIntegerField(models.PositiveSmallIntegerField):
    def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.IntegerField,
            'min_value': 0,
            'max_value': 32767
        }
        defaults.update(kwargs)
        return super(PositiveSmallIntegerField, self).formfield(**defaults)


class PositiveIntegerField(models.IntegerField):
    def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.IntegerField,
            'min_value': 0
        }
        defaults.update(kwargs)
        return super(PositiveIntegerField, self).formfield(**defaults)


class SlugField(models.SlugField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.SlugField}
        defaults.update(kwargs)
        return super(SlugField, self).formfield(**defaults)


class TextField(models.TextField):
    def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.CharField,
            'widget': widgets.Textarea
        }
        defaults.update(kwargs)
        return super(TextField, self).formfield(**defaults)


class URLField(models.URLField):
    def formfield(self, **kwargs):
        defaults = {'form_class': forms.URLField}
        defaults.update(kwargs)
        return super(URLField, self).formfield(**defaults)
