from itertools import chain
import json
import pytest
from django.urls import reverse
from django.core.files.uploadedfile import SimpleUploadedFile

from aircox.models import Program


@pytest.mark.django_db()
def test_edit_program(user, client, program):
    client.force_login(user)
    response = client.get(reverse("program-detail", kwargs={"slug": program.slug}))
    assert response.status_code == 200
    assert "🖉 ".encode() not in response.content
    user.groups.add(program.editors)
    response = client.get(reverse("program-detail", kwargs={"slug": program.slug}))
    assert "🖉 ".encode() in response.content
    assert b"foobar" not in response.content
    response = client.post(reverse("program-edit", kwargs={"pk": program.pk}), {"content": "foobar"})
    response = client.get(reverse("program-detail", kwargs={"slug": program.slug}))
    assert b"foobar" in response.content


@pytest.mark.django_db()
def test_add_cover(user, client, program, png_content):
    assert program.cover is None
    user.groups.add(program.editors)
    client.force_login(user)
    cover = SimpleUploadedFile("cover1.png", png_content, content_type="image/png")
    r = client.post(
        reverse("program-edit", kwargs={"pk": program.pk}), {"content": "foobar", "new_cover": cover}, follow=True
    )
    assert r.status_code == 200
    p = Program.objects.get(pk=program.pk)
    assert "cover1.png" in p.cover.url


@pytest.mark.django_db()
def test_edit_tracklist(user, client, program, episode, tracks):
    user.groups.add(program.editors)
    client.force_login(user)
    episode.status = 0x10  # published
    episode.save()
    r = client.get(reverse("program-detail", kwargs={"slug": episode.program.slug}))
    assert r.status_code == 200
    r = client.get(reverse("episode-detail", kwargs={"slug": episode.slug}))
    assert r.status_code == 200
    r2 = client.get(reverse("episode-edit", kwargs={"pk": episode.pk}))
    assert r2.status_code == 200

    tracklist = [t.id for t in episode.track_set.all().order_by("position")]
    tracklist_details_reversed = [(t.id, t.artist, t.title) for t in episode.track_set.all().order_by("-position")]
    tracklist_details_reversed = list(chain(*tracklist_details_reversed))
    data = """{{"website": [""], "content": ["foobar"], "new_podcast": [""], "form-TOTAL_FORMS": ["3"],
    "form-INITIAL_FORMS": ["3"], "form-MIN_NUM_FORMS": ["0"], "form-MAX_NUM_FORMS": ["1000"], "form-0-position": ["0"],
    "form-0-id": ["{}"], "form-0-": ["", "", "", "", "", ""], "form-0-artist": ["{}"], "form-0-title": ["{}"],
    "form-0-tags": [""], "form-0-album": [""], "form-0-year": [""], "form-1-position": ["1"], "form-1-id": ["{}"],
    "form-1-": ["", "", "", "", "", ""], "form-1-artist": ["{}"], "form-1-title": ["{}"], "form-1-tags": [""],
    "form-1-album": [""], "form-1-year": [""], "form-2-position": ["2"], "form-2-id": ["{}"], "form-2-": ["", "", "",
    "", "", ""], "form-2-artist": ["{}"], "form-2-title": ["{}"], "form-2-tags": [""], "form-2-album": [""],
    "form-2-year": [""]}}""".format(
        *tracklist_details_reversed
    )
    r = client.post(reverse("episode-edit", kwargs={"pk": episode.pk}), json.loads(data), follow=True)
    assert r.status_code == 200
    assert set(episode.track_set.all().values_list("id", flat=True)) == set(tracklist)
