from django.shortcuts import render
from datetime import date, timedelta
from chicago.models import ChicagoBill, ChicagoEvent, TorontoPerson
from councilmatic_core.models import Action
from councilmatic_core.views import *
from django.conf import settings


class ChicagoIndexView(IndexView):
    template_name = 'chicago/index.html'
    bill_model = ChicagoBill
    event_model = ChicagoEvent
    person_model = TorontoPerson

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)

        upcoming_meetings = list(self.event_model.upcoming_committee_meetings())

        date_cutoff = self.event_model.most_recent_past_city_council_meeting().start_time

        # populating activity at last council meeting
        meeting_activity = {}
        meeting_activity['actions'] = Action.actions_on_date(date_cutoff.date())
        meeting_bills = list(set([a.bill for a in meeting_activity['actions']]))
        meeting_activity['bills'] = meeting_bills
        meeting_activity['bills_routine'] = [b for b in meeting_bills if 'Routine' in b.topics]
        meeting_activity['bills_nonroutine'] = [b for b in meeting_bills if 'Non-Routine' in b.topics]




        # populating recent activitiy (since last council meeting)
        recent_activity = {}

        new_bills = ChicagoBill.new_bills_since(date_cutoff)
        recent_activity['new'] = new_bills
        recent_activity['new_routine'] = [b for b in new_bills if 'Routine' in b.topics]
        recent_activity['new_nonroutine'] = [b for b in new_bills if 'Non-Routine' in b.topics]

        updated_bills = ChicagoBill.updated_bills_since(date_cutoff)
        recent_activity['updated_routine'] = [b for b in updated_bills if 'Routine' in b.topics]
        recent_activity['updated_nonroutine'] = [b for b in updated_bills if 'Non-Routine' in b.topics]

        # getting topic counts for meeting bills
        topic_hierarchy = settings.TOPIC_HIERARCHY
        topic_tag_counts = {}
        for b in meeting_bills:
            for topic in b.topics:
                try:
                    topic_tag_counts[topic] += 1
                except KeyError:
                    topic_tag_counts[topic] = 1
        # put together data blob for topic hierarchy
        for parent_blob in topic_hierarchy:
            parent_blob['count'] = 0
            for child_blob in parent_blob['children']:
                child_name = child_blob['name']
                child_blob['count'] = topic_tag_counts[child_name] if child_name in topic_tag_counts else 0
                parent_blob['count'] += child_blob['count']
                for gchild_blob in child_blob['children']:
                    gchild_name = gchild_blob['name']
                    gchild_blob['count'] = topic_tag_counts[gchild_name] if gchild_name in topic_tag_counts else 0

        seo = {}
        seo.update(settings.SITE_META)
        seo.update({'site_url': settings.SITE_URL})
        seo['image'] = '/static/images/city_hall.jpg'

        return {
            'meeting_activity': meeting_activity,
            'recent_activity': recent_activity,
            'last_council_meeting': self.event_model.most_recent_past_city_council_meeting(),
            'next_council_meeting': self.event_model.next_city_council_meeting(),
            'upcoming_committee_meetings': upcoming_meetings,
            'topic_hierarchy': topic_hierarchy,
            'seo': seo,
        }

class ChicagoAboutView(AboutView):
    template_name = 'chicago/about.html'

# this is for handling bill detail urls from the old chicago councilmatuc
def bill_detail_redirect(request, old_id):
    pattern = '?ID=%s&GUID' %old_id

    try:
        obj = ChicagoBill.objects.get(source_url__contains=pattern)
    except:
        raise Http404("No bill found matching the query")

    return redirect('bill_detail', slug=obj.slug, permanent=True)


class ChicagoBillDetailView(BillDetailView):
    model = ChicagoBill

    def get_object(self, queryset=None):
        """
        Returns a bill based on slug. If no bill found,
        looks for bills based on legistar id (so that
        urls from old Chicago councilmatic don't break)
        """

        if queryset is None:
            queryset = self.get_queryset()

        slug = self.kwargs.get(self.slug_url_kwarg)
        if slug is None:
            raise AttributeError("Generic detail view %s must be called with "
                                 "either an object pk or a slug."
                                 % self.__class__.__name__)

        # Try looking up by slug
        if slug is not None:
            slug_field = self.get_slug_field()
            queryset = queryset.filter(**{slug_field: slug})

        try:
            # Get the single item from the filtered queryset
            obj = queryset.get()
        except queryset.model.DoesNotExist:
            raise Http404("No bill found matching the query")

        return obj

class ChicagoCouncilMembersView(CouncilMembersView):

    def get_seo_blob(self):
        seo = {}
        seo.update(settings.SITE_META)
        seo['site_desc'] = "Look up your local Councillor, and see what they're doing in your ward & your city"
        seo['image'] = '/static/images/toronto_map.jpg'
        seo['title'] = 'Wards & Councillors - Toronto Councilmatic'

        return seo

class TorontoPersonDetailView(PersonDetailView):
    model = TorontoPerson

class TorontoEventsView(EventsView):
    def get_context_data(self, **kwargs):
        context = super(TorontoEventsView, self).get_context_data(**kwargs)
        context['event_time_item'] = 'chicago/partials/event_time_item.html'
        return context

class TorontoEventDetailView(EventDetailView):
    template_name = 'chicago/event.html'

@@ -3524,1439 +3524,77 @@
 l'%0A%0A
-# this is for handling bill detail urls from the old chicago councilmatuc%0Adef bill_detail_redirect(request, old_id):%0A    pattern = '?ID=%25s&GUID' %25old_id%0A%0A    try:%0A        obj = ChicagoBill.objects.get(source_url__contains=pattern)%0A    except:%0A        raise Http404(%22No bill found matching the query%22)%0A%0A    return redirect('bill_detail', slug=obj.slug, permanent=True)%0A%0A%0Aclass ChicagoBillDetailView(BillDetailView):%0A    model = ChicagoBill%0A%0A    def get_object(self, queryset=None):%0A        %22%22%22%0A        Returns a bill based on slug. If no bill found,%0A        looks for bills based on legistar id (so that%0A        urls from old Chicago councilmatic don't break)%0A        %22%22%22%0A%0A        if queryset is None:%0A            queryset = self.get_queryset()%0A%0A        slug = self.kwargs.get(self.slug_url_kwarg)%0A        if slug is None:%0A            raise AttributeError(%22Generic detail view %25s must be called with %22%0A                                 %22either an object pk or a slug.%22%0A                                 %25 self.__class__.__name__)%0A%0A        # Try looking up by slug%0A        if slug is not None:%0A            slug_field = self.get_slug_field()%0A            queryset = queryset.filter(**%7Bslug_field: slug%7D)%0A%0A        try:%0A            # Get the single item from the filtered queryset%0A            obj = queryset.get()%0A        except queryset.model.DoesNotExist:%0A            raise Http404(%22No bill found matching the query%22)%0A%0A        return obj
+%0Aclass ChicagoBillDetailView(BillDetailView):%0A    model = ChicagoBill
 %0A%0Acl

Remove some chicago-specific code for old bill redirects.