import csv

from django.http import HttpResponse

from common.utils.geocode import geocode_address, get_address, set_areas


def make_export_action(label, **kwargs):
    return [export_as_csv_action(description=label, **kwargs)]


def export_as_csv_action(description="Export selected objects as CSV file",
                         fields=None, exclude=None, header=True):
    """
    This function returns an export csv action
    'fields' and 'exclude' work like in django ModelForm
    'header' is whether or not to output the column names as the first row
    """
    fields = fields or []
    exclude = exclude or []

    def export_as_csv(modeladmin, request, queryset):
        """
        Generic csv export admin action.
        based on http://djangosnippets.org/snippets/1697/
        """
        opts = modeladmin.model._meta
        field_names = set([field.name for field in opts.fields])
        field_names = set(field_names) - set(exclude)

        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename=%s.csv' % str(opts).replace('.', '_')

        writer = csv.writer(response)
        if header:
            writer.writerow(list(field_names))
        for obj in queryset:
            writer.writerow([getattr(obj, field) for field in field_names])
        return response
    export_as_csv.short_description = description
    return export_as_csv


def geocode_allegation(modeladmin, request, queryset):
    for allegation in queryset:
        address = get_address(allegation)
        point = geocode_address(address)

        if point:
            allegation.point = point
            set_areas(allegation, point)
            allegation.save()
