import json
import logging

from celery import states
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Avg, Count, F
from django.http import HttpResponse
from django.views.generic import DetailView, ListView
from django.views.generic.list import MultipleObjectMixin
from django_celery_results.models import TaskResult

from games.tasks import update_roms

from .models import Developer, Game, GameCollection, GameSystem, Genre, Publisher

logger = logging.Logger(__name__)


IN_PROGRESS_STATES = [states.PENDING, states.STARTED, states.RETRY]


class RecentGameList(LoginRequiredMixin, ListView):
    model = Game
    paginate_by = 20
    queryset = Game.objects.order_by("-created")[:200]


class LibraryGameList(ListView, LoginRequiredMixin):
    template_name = "games/game_library_list.html"
    model = Game
    paginate_by = 200

    def get_context_data(self, **kwargs):
        game_system_slug = self.request.GET.get("game_system")
        order_by = self.request.GET.get("order_by", "name")
        if order_by[0] == "-":
            order_by = order_by[1:]
            object_list = Game.objects.order_by(F(order_by).desc(nulls_last=True))
        else:
            object_list = Game.objects.order_by(F(order_by).asc(nulls_last=True))
        if game_system_slug:
            object_list = object_list.filter(
                game_system__retropie_slug=game_system_slug
            )
        context = super(LibraryGameList, self).get_context_data(
            object_list=object_list, **kwargs
        )
        return context


class FilterableBaseListView(ListView, LoginRequiredMixin):
    def get_queryset(self, **kwargs):
        order_by = self.request.GET.get("order_by", "name")
        queryset = super().get_queryset(**kwargs)
        queryset = queryset.annotate(num_games=Count("game")).annotate(
            rating=Avg("game__rating")
        )

        if order_by[0] == "-":
            order_by = order_by[1:]
            queryset = queryset.order_by(F(order_by).desc(nulls_last=True))
        else:
            queryset = queryset.order_by(F(order_by).asc(nulls_last=True))
        return queryset


class PublisherList(FilterableBaseListView):
    model = Publisher


class DeveloperList(FilterableBaseListView):
    model = Developer


class GenreList(FilterableBaseListView):
    model = Genre


class GameDetail(DetailView, LoginRequiredMixin):
    model = Game


class GamePlayDetail(DetailView, LoginRequiredMixin):
    template_name = "games/game_play_detail.html"
    model = Game


class GameSystemList(ListView, LoginRequiredMixin):
    model = GameSystem


class GameSystemDetail(DetailView, MultipleObjectMixin, LoginRequiredMixin):
    model = GameSystem
    paginate_by = 20

    def get_context_data(self, **kwargs):
        object_list = Game.objects.filter(game_system=self.get_object())
        context = super(GameSystemDetail, self).get_context_data(
            object_list=object_list, **kwargs
        )
        return context


class GenreDetail(DetailView, MultipleObjectMixin, LoginRequiredMixin):
    model = Genre
    paginate_by = 20

    def get_context_data(self, **kwargs):
        object_list = Game.objects.filter(genre=self.get_object())
        context = super(GenreDetail, self).get_context_data(
            object_list=object_list, **kwargs
        )
        return context


class PublisherDetail(DetailView, MultipleObjectMixin, LoginRequiredMixin):
    model = Publisher
    paginate_by = 20

    def get_context_data(self, **kwargs):
        object_list = Game.objects.filter(publisher=self.get_object())
        context = super(PublisherDetail, self).get_context_data(
            object_list=object_list, **kwargs
        )
        return context


class DeveloperDetail(DetailView, MultipleObjectMixin, LoginRequiredMixin):
    model = Developer
    paginate_by = 20

    def get_context_data(self, **kwargs):
        object_list = Game.objects.filter(developer=self.get_object())
        context = super(DeveloperDetail, self).get_context_data(
            object_list=object_list, **kwargs
        )
        return context


class GameCollectionList(ListView, LoginRequiredMixin):
    model = GameCollection


class GameCollectionDetail(DetailView, LoginRequiredMixin):
    model = GameCollection

    def get_context_data(self, **kwargs):
        collection = self.get_object()
        order_by = self.request.GET.get("order_by", "release_date")
        object_list = collection.games.all()

        if order_by[0] == "-":
            order_by = order_by[1:]
            object_list = object_list.order_by(F(order_by).desc(nulls_last=True))
        else:
            object_list = object_list.order_by(F(order_by).asc(nulls_last=True))

        context = super(GameCollectionDetail, self).get_context_data(
            object_list=object_list, **kwargs
        )
        return context


@login_required
def trigger_rom_update(request):
    full_scan = request.GET.get("full_scan", False)
    if full_scan == "true":
        full_scan = True

    game_systems = request.GET.getlist("game_systems")
    if not game_systems:
        game_systems = list(settings.GAME_SYSTEM_DEFAULTS.keys())

    try:
        update_roms.delay(game_systems, full_scan=full_scan)
    except FileNotFoundError:
        return HttpResponse(
            json.dumps({"success": False, "msg": "Skyscraper is not installed"}),
            status=400,
            content_type="application/json",
        )
    return HttpResponse(
        json.dumps({"success": True, "msg": "Library scan started"}),
        status=200,
        content_type="application/json",
    )


@login_required
def library_update_status(request):
    update_task = TaskResult.objects.filter(
        task_name="games.tasks.update_roms",
        status__in=IN_PROGRESS_STATES,
    ).first()

    if not update_task:
        state = states.SUCCESS
    else:
        state = update_task.status

    return HttpResponse(
        json.dumps({"state": state}),
        status=200,
        content_type="application/json",
    )
