import json
import requests
from django.shortcuts import render, redirect

from opencivicdata.models import Person

from tot import settings
from preferences.models import PersonFollow


def find_legislator(request):
    senator = request.session.get('sen_from_address')
    representative = request.session.get('rep_from_address')
    address = request.session.get('address')
    lat = request.session.get('lat') or '30.4'
    lon = request.session.get('lon') or '-84.3'

    senator_contact_details = None
    representative_contact_details = None

    if senator and representative:
        senator = json.loads(senator)
        if senator['name'] != 'none found':
            senator_obj = Person.objects.get(id=senator['id'])
            senator_contact_details = get_contact_details(senator_obj)

        representative = json.loads(representative)
        if representative['name'] != 'none found':
            rep_obj = Person.objects.get(id=representative['id'])
            representative_contact_details = get_contact_details(rep_obj)

    return render(
        request,
        'legislators/find_legislator.html', {
            'address_senator': senator,
            'address_representative': representative,
            'address': address,
            'lat': float(lat),
            'lng': float(lon),
            'senator_contact_details': senator_contact_details,
            'representative_contact_details': representative_contact_details
        }
    )


def get_contact_details(legislator):
    # Parsed out seperately for nicer rendering in the templates
    raw_contact_details = legislator.contact_details.all()
    contact_details = {'email': '', 'capitol': [], 'district': []}
    for entry in raw_contact_details:
        if entry.type == 'email':
            contact_details['email'] = entry.value
        if entry.note == 'capitol':
            contact_details['capitol'].append(entry)
        if entry.note == 'district':
            contact_details['district'].append(entry)

    return contact_details


def legislator_detail(request, legislator_id):
    legislator = Person.objects.get(id=legislator_id)
    memberships = legislator.memberships.all().select_related(
        'organization__classification'
    ).select_related('post')
    post = memberships.filter(post__isnull=False)[0]
    party = memberships.filter(organization__classification='party')[0].organization.name
    if party == 'Democratic':
        party = 'Democrat'

    contact_details = get_contact_details(legislator)

    votes = legislator.votes.all().prefetch_related(
        'vote_event__bill__legislative_session'
    ).order_by(
        '-vote_event__start_date'
    )[:20]  # TODO - how to handle vote history?

    recent_votes = votes[:settings.NUMBER_OF_LATEST_ACTIONS]
    sponsored_bills = [
        sponsorship.bill for sponsorship in legislator.billsponsorship_set.all().prefetch_related(
            'bill__actions'
        ).prefetch_related('bill__sponsorships')
    ]
    for bill in sponsored_bills:
        bill.latest_action = list(bill.actions.all())[-1]

    message = ''
    if request.method == 'POST':
        try:
            PersonFollow.objects.get(user=request.user, person_id=legislator_id)
            message = 'You are already following {}.'.format(legislator.name)
        except PersonFollow.DoesNotExist:
            PersonFollow.objects.create(user=request.user, person_id=legislator_id)
            message = 'You are now following {}.'.format(legislator.name)

    return render(
        request,
        'legislators/detail.html',
        {
            'legislator': legislator,
            'contact_details': contact_details,
            'post': post.post,
            'party': party,
            'votes': votes,
            'recent_votes': recent_votes,
            'sponsored_bills': sponsored_bills,
            'message': message
        }
    )


def latest_latlon(request):
    # TODO - Fix so that it gets API info and does not refresh.
    return redirect('/#legislators')


def get_latlon(request):
    apikey = settings.ANON_API_KEY
    if request.is_ajax():
        lat = request.GET.get('lat', '')
        lon = request.GET.get('lon', '')
        address = request.GET.get('address', '')

        api_resp = requests.get(
            settings.DOMAIN + '/api/people/?latitude={}&longitude={}&apikey={}'.format(
                lat, lon, apikey
            )
        ).json()

        if api_resp['meta']['pagination']['count'] == 2:
            for person in api_resp['data']:
                person_dict = {
                    'name': person['attributes']['name'],
                    'url': person['links']['self'],
                    'id': person['id'],
                    'image': person['attributes']['image']
                }
                if 'Senators' in person['attributes']['image']:
                    request.session['sen_from_address'] = json.dumps(person_dict)
                else:
                    request.session['rep_from_address'] = json.dumps(person_dict)
        else:
            request.session['sen_from_address'] = request.session['rep_from_address'] = json.dumps({'name': 'none found'})

    request.session['lat'] = lat
    request.session['lon'] = lon
    request.session['address'] = address
    request.session.modified = True
    return redirect(find_legislator)

@@ -2678,13 +2678,8 @@
    )
-%5B:20%5D
   # 
@@ -2711,16 +2711,27 @@
  history
+ pagination
 ?%0A%0A    r

Remove limit for now