import pytest
import datetime as dt
from pupa.scrape import Event as ScrapeEvent
from pupa.importers import EventImporter
from opencivicdata.models import Event, Jurisdiction


def ge():
    event = ScrapeEvent(
        name="America's Birthday",
        start_time="2014-07-04T05:00Z",
        location="America",
        timezone="America/New_York",
        all_day=True)
    event.add_person("George Washington")
    return event


@pytest.mark.django_db
def test_related_people_event():
    j = Jurisdiction.objects.create(id='jid', division_id='did')
    event1 = ge()
    event2 = ge()

    for event in [event1, event2]:
        item = event.add_agenda_item("Cookies will be served")
        item.add_person(person="John Q. Public")

    obj, what = EventImporter('jid').import_item(event1.as_dict())
    assert what == 'insert'

    obj, what = EventImporter('jid').import_item(event2.as_dict())
    assert what == 'noop'



@pytest.mark.django_db
def test_related_bill_event():
    j = Jurisdiction.objects.create(id='jid', division_id='did')
    event1 = ge()
    event2 = ge()

    for event in [event1, event2]:
        item = event.add_agenda_item("Cookies will be served")
        item.add_bill(bill="HB 101")

    obj, what = EventImporter('jid').import_item(event1.as_dict())
    assert what == 'insert'

    obj, what = EventImporter('jid').import_item(event2.as_dict())
    assert what == 'noop'


@pytest.mark.django_db
def test_related_committee_event():
    j = Jurisdiction.objects.create(id='jid', division_id='did')
    event1 = ge()
    event2 = ge()

    for event in [event1, event2]:
        item = event.add_agenda_item("Cookies will be served")
        item.add_committee(committee="Fiscal Committee")

    obj, what = EventImporter('jid').import_item(event1.as_dict())
    assert what == 'insert'

    obj, what = EventImporter('jid').import_item(event2.as_dict())
    assert what == 'noop'


@pytest.mark.django_db
def test_media_event():
    j = Jurisdiction.objects.create(id='jid', division_id='did')
    event1 = ge()
    event2 = ge()

    for event in [event1, event2]:
        item = event.add_agenda_item("Cookies will be served")
        item.add_media_link(
            note="Hello, World",
            media_type='application/octet-stream',
            url="http://hello.world/foo"
        )

    obj, what = EventImporter('jid').import_item(event1.as_dict())
    assert what == 'insert'

    obj, what = EventImporter('jid').import_item(event2.as_dict())
    assert what == 'noop'


@pytest.mark.django_db
def test_media_event():
    j = Jurisdiction.objects.create(id='jid', division_id='did')
    event1 = ge()
    event2 = ge()

    for event in [event1, event2]:
        event.add_document(note="Presentation",
                           url="http://example.com/presentation.pdf")

    obj, what = EventImporter('jid').import_item(event1.as_dict())
    assert what == 'insert'

    obj, what = EventImporter('jid').import_item(event2.as_dict())
    assert what == 'noop'


@pytest.mark.django_db
def test_full_event():
    j = Jurisdiction.objects.create(id='jid', division_id='did')
    event = ge()

    obj, what = EventImporter('jid').import_item(event.as_dict())
    assert what == 'insert'

    obj, what = EventImporter('jid').import_item(event.as_dict())
    assert what == 'noop'

    event = ge()
    event.location['name'] = "United States of America"
    obj, what = EventImporter('jid').import_item(event.as_dict())
    assert what == 'update'


@pytest.mark.django_db
def test_bad_event_time():
    j = Jurisdiction.objects.create(id='jid', division_id='did')
    event = ge()
    event.start_time="2014-07-04T05:00"
    pytest.raises(
        ValueError,
        EventImporter('jid').import_item,
        event.as_dict()
    )


@pytest.mark.django_db
def test_top_level_media_event():
    j = Jurisdiction.objects.create(id='jid', division_id='did')
    event = ge()
    event.add_media_link(
        "fireworks",
        "http://example.com/fireworks.mov",
        media_type='application/octet-stream'
    )
    obj, what = EventImporter('jid').import_item(event.as_dict())
    assert what == 'insert'

@@ -3908,39 +3908,55 @@
 'did')%0A    event
- = ge()
+1, event2 = ge(), ge()%0A
 %0A    event.add_m
@@ -3945,24 +3945,25 @@
 )%0A%0A    event
+1
 .add_media_l
@@ -3966,25 +3966,156 @@
 ia_link(
-%0A        
+%22fireworks%22, %22http://example.com/fireworks.mov%22,%0A                          media_type='application/octet-stream')%0A    event2.add_media_link(
 %22firewor
@@ -4118,24 +4118,16 @@
 eworks%22,
-%0A       
  %22http:/
@@ -4147,32 +4147,50 @@
 fireworks.mov%22,%0A
+                  
         media_ty
@@ -4218,22 +4218,114 @@
 -stream'
+)%0A
 %0A    
-)
+obj, what = EventImporter('jid').import_item(event1.as_dict())%0A    assert what == 'insert'%0A
 %0A    obj
@@ -4363,32 +4363,33 @@
 mport_item(event
+2
 .as_dict())%0A    
@@ -4392,28 +4392,26 @@
     assert what == '
-insert
+noop
 '%0A

Fix media test to check for dupes