# coding: utf-8
from __future__ import unicode_literals

from django.core.exceptions import ValidationError
from django.forms import Field, TextInput

from .. import conf
from ..exceptions import InvalidRequestError
from ..api_resources import File, FileGroup
from . import conf as dj_conf


class FileWidget(TextInput):
    """Django form widget that sets up Uploadcare Widget.

    It adds js and hidden input with basic Widget's params, e.g.
    *data-public-key*.

    """

    input_type = 'hidden'
    is_hidden = False

    class Media:
        js = (dj_conf.uploadcare_js,)

    def __init__(self, attrs=None):
        default_attrs = {
            'role': 'uploadcare-uploader',
            'data-public-key': conf.pub_key,
        }

        if dj_conf.upload_base_url is not None:
            default_attrs['data-upload-base-url'] = dj_conf.upload_base_url

        if attrs is not None:
            default_attrs.update(attrs)

        super(FileWidget, self).__init__(default_attrs)

    def render(self, name, value, attrs):
        return super(FileWidget, self).render(name, value, attrs)


class FileField(Field):
    """Django form field that uses ``FileWidget`` with default arguments.

    It always returns URL.

    """

    widget = FileWidget

    def to_python(self, value):
        if value is None or value == '':
            return value

        try:
            return File(value).cdn_url
        except InvalidRequestError as exc:
            raise ValidationError(
                'Invalid value for a field: {exc}'.format(exc=exc)
            )


class ImageField(FileField):
    """Django form field that sets up ``FileWidget`` to work with images.
    """

    def __init__(self, manual_crop=None, *args, **kwargs):
        self.manual_crop = manual_crop
        super(ImageField, self).__init__(*args, **kwargs)

    def widget_attrs(self, widget):
        attrs = {'data-images-only': ''}
        if self.manual_crop is not None:
            attrs['data-crop'] = self.manual_crop
        return attrs


class FileGroupField(Field):
    """Django form field that sets up ``FileWidget`` in multiupload mode.
    """

    widget = FileWidget

    def to_python(self, value):
        if value is None or value == '':
            return value

        try:
            return FileGroup(value).cdn_url
        except InvalidRequestError as exc:
            raise ValidationError(
                'Invalid value for a field: {exc}'.format(exc=exc)
            )

    def widget_attrs(self, widget):
        attrs = {'data-multiple': ''}
        return attrs


class ImageGroupField(FileGroupField):
    """Django form field that sets up ``FileWidget`` in image multiupload mode.
    """

    def widget_attrs(self, widget):
        attrs = {'data-multiple': '', 'data-images-only': ''}
        return attrs

@@ -1572,16 +1572,164 @@
      )%0A%0A
+    def widget_attrs(self, widget):%0A        attrs = %7B%7D%0A        if not self.required:%0A            attrs%5B'data-clearable'%5D = ''%0A        return attrs%0A%0A
 %0Aclass I
@@ -2038,25 +2038,83 @@
     attrs = 
-%7B
+super(ImageField, self).widget_attrs(widget)%0A        attrs%5B
 'data-images
@@ -2111,37 +2111,38 @@
 ata-images-only'
-:
+%5D =
  ''
-%7D
 %0A        if self
@@ -2763,24 +2763,95 @@
 tiple': ''%7D%0A
+        if not self.required:%0A            attrs%5B'data-clearable'%5D = ''%0A
         retu
@@ -3045,30 +3045,72 @@
 s = 
-%7B'data-multiple': '', 
+super(ImageGroupField, self).widget_attrs(widget)%0A        attrs%5B
 'dat
@@ -3123,21 +3123,22 @@
 es-only'
-:
+%5D =
  ''
-%7D
 %0A       

add data-images-only attribute if value is not required