# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals

from datetime import date
from django.core.files.uploadedfile import SimpleUploadedFile
from django.http import Http404
import pytest

from millionmilestogether.models import Child, Image
import millionmilestogether.views.images as views

from ..utils import (
    get_friend,
    rendered_template
)
from ...utils import set_fake_image_date


@pytest.mark.django_db
def test_render_image_detail_with_incorrect_login(rf, image, other_user):
    request = rf.get(image.get_absolute_url())
    request.user = other_user
    kwargs = image.get_url_kwargs()

    resp = views.image_detail(request, **kwargs)
    assert resp.status_code == 403
    expected = b"You don't have permission to view this image."
    assert resp.content == expected


@pytest.mark.django_db
def test_render_image_detail_as_friend(rf, image):
    friend = get_friend(image.moment.owner)

    # Register a child the user does have permission to view
    boo = Child.objects.create(
        first_name='Boo',
        date_of_birth=date(2012, 2, 16)
    )
    boo.guardians.add(friend)
    boo.save()

    mike = Child.objects.create(
        first_name='Mike',
        date_of_birth=date(2012, 2, 16)
    )
    mike.guardians.add(image.moment.owner)
    mike.save()

    request = rf.get(image.get_absolute_url())
    request.user = friend
    kwargs = image.get_url_kwargs()

    resp = views.image_detail(request, **kwargs)
    assert resp.status_code == 200
    assert rendered_template(resp) == [
        'millionmilestogether/image/detail.html',
    ]

    assert resp.context_data['read_only']


@pytest.mark.django_db
def test_render_image_detail_with_correct_login(rf, image):
    # Register a child the user does have permission to view
    boo = Child.objects.create(
        first_name='Boo',
        date_of_birth=date(2012, 2, 16)
    )
    boo.guardians.add(image.moment.owner)
    boo.save()

    request = rf.get(image.get_absolute_url())
    request.user = image.moment.owner
    kwargs = image.get_url_kwargs()

    resp = views.image_detail(request, **kwargs)
    assert resp.status_code == 200
    assert rendered_template(resp) == [
        'millionmilestogether/image/detail.html',
    ]

    assert not resp.context_data['read_only']


@pytest.mark.django_db
def test_render_image_detail_with_bad_user_id(rf, image):
    request = rf.get(image.get_absolute_url())
    request.user = image.moment.owner
    kwargs = image.get_url_kwargs()
    kwargs['user_id'] = '42'

    with pytest.raises(Http404):
        views.image_detail(request, **kwargs)


@pytest.mark.django_db
def test_render_image_next_previous(rf, moment):
    image1 = Image.objects.create(
        moment=moment,
        image=SimpleUploadedFile('foo.jpg', '')
    )
    image1.save()

    image2 = Image.objects.create(
        moment=moment,
        image=SimpleUploadedFile('foo.jpg', '')
    )
    image2.save()

    set_fake_image_date(image1, '2013:03:31 06:28:30')
    set_fake_image_date(image2, '2012:03:31 06:28:30')

    def get_response(image):
        request = rf.get(image.get_absolute_url())
        request.user = moment.owner
        kwargs = image.get_url_kwargs()

        return views.image_detail(request, **kwargs)

    resp = get_response(image1)
    assert resp.status_code == 200
    assert rendered_template(resp) == [
        'millionmilestogether/image/detail.html',
    ]

    assert resp.context_data['previous_image_url'] == image2.get_absolute_url()
    assert resp.context_data['next_image_url'] is None

    resp = get_response(image2)
    assert resp.status_code == 200
    assert rendered_template(resp) == [
        'millionmilestogether/image/detail.html',
    ]

    assert resp.context_data['previous_image_url'] is None
    assert resp.context_data['next_image_url'] == image1.get_absolute_url()
