import datetime
import urllib

from django.core import urlresolvers
from django.template.defaultfilters import slugify, truncatewords

from billy.core import mdb as db, settings
from billy.utils import get_domain
from .base import Document
from .metadata import Metadata
from .utils import CachedAttribute


class Event(Document):

    collection = db.events

    @property
    def metadata(self):
        return Metadata.get_object(self[settings.LEVEL_FIELD])

    def bill_objects(self):
        '''Returns a cursor of full bill objects for any bills that have
        ids. Not in use anyware as of 12/18/12, but handy to have around.
        '''
        bills = []
        for bill in self['related_bills']:
            if 'id' in bill:
                bills.append(bill['id'])
        return db.bills.find({"_id": {"$in": bills}})

    def bills(self):
        '''Aliases the inaccesible underscore names and mislabed "bill_id"
        field on event['related_bills'], which points to the mongo id,
        whereas everywhere else bill_id refers to the human id like "HB 123."

        The names were updated by 1f24792 on 12/18/12.
        '''
        for bill in self['related_bills']:
            yield bill

    def committees(self):
        committees = []
        for committee in self['participants']:
            if 'id' in committee:
                _id = committee['id']
                if _id:
                    committees.append(_id)

        return db.committees.find({"_id": {"$in": committees}})

    @CachedAttribute
    def committees_dict(self):
        return dict((cmt['_id'], cmt) for cmt in self.committees())

    def get_absolute_url(self):
        slug = slugify(truncatewords(self['description'], 10))
        url = urlresolvers.reverse('event', args=[self[settings.LEVEL_FIELD],
                                                  self['_id']])
        return '%s%s/' % (url, slug)

    def host(self):
        '''Return the host committee.
        '''
        for participant in self['participants']:
            if participant['type'] == 'host':

                if set(['participant_type', 'id']) < set(participant):
                    # This event uses the id keyname "id".
                    if participant['participant_type'] == 'committee':
                        _id = participant['id']

                if _id is None:
                    # If id is None, this committee id was probably old
                    # and was purged. Just return the committee so the
                    # un-hyperlinked host name can be displayed.
                    if 'participant' in participant:
                        return participant['participant']
                    elif 'committee' in participant:
                        return participant['committee']

    def other_committees(self):
        comms = self.committees_dict.values()
        comms.remove(self.host())
        return comms

    def host_chairs(self):
        '''Returns a list of members that chair the host committee,
        including "co-chair" and "chairperson." This could concievalby
        yield a false positive if the person's title is 'dunce chair'.
        '''
        chairs = []
        # Host is guaranteed to be a committe or none.
        host = self.host()
        if host is None:
            return
        for member, full_member in host.members_objects:
            if 'chair' in member.get('role', '').lower():
                chairs.append((member, full_member))
        return chairs

    def host_has_multiple_chairs(self):
        '''True or false: are there multiple chairs in the host committee?
        '''
        return 1 < len(self.host_chairs())

    def host_members(self):
        '''Return the members of the host committee.
        '''
        host = self.host()
        if host is None:
            return
        for member, full_member in host.members_objects:
            yield full_member

    def gcal_string(self):

        dt_format = "%Y%m%dT%H%M%SZ"
        start_date = self['when'].strftime(dt_format)
        duration = datetime.timedelta(hours=1)
        end_data = (self['when'] + duration)
        end_date = end_data.strftime(dt_format)

        gcal_info = {
            "action": "TEMPLATE",
            "text": self['description'].encode('utf-8'),
            "dates": "%s/%s" % (start_date, end_date),
            "details": "",
            "location": self['location'].encode('utf-8'),
            "trp": "false",
            "sprop": "http://%s/" % get_domain(),
            "sprop": "name:billy"
        }
        gcal_string = urllib.urlencode(gcal_info)
        return gcal_string

@@ -1969,32 +1969,51 @@
 ee.%0A        '''%0A
+        _id = None%0A
         for part
@@ -2037,32 +2037,32 @@
 participants'%5D:%0A
-
             if p
@@ -2091,17 +2091,16 @@
 'host':%0A
-%0A
         
@@ -2340,17 +2340,16 @@
 t%5B'id'%5D%0A
-%0A
         
@@ -2360,49 +2360,20 @@
     
-if _id is None:%0A                    # I
+        i
 f 
+_
 id i
@@ -2382,400 +2382,107 @@
 None
-, this committee id was probably old%0A                    # and was purged. Just return the committee so the%0A                    # un-hyperlinked host name can be displayed.%0A                    if 'participant' in participant:%0A                        return participant%5B'participant'%5D%0A                    elif 'committee' in participant:%0A                        return participant%5B'committee'%5D
+:%0A                            continue%0A                        return self.committees_dict.get(_id)
 %0A%0A  

Remove code for handling old/purged committee ids