from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.template import loader
from django.contrib.auth.models import User
from .models import Match, Club, Tournament, Team, Player
from .serializers import UserSerializer, MatchSerializer, ClubSerializer, TournamentSerializer, TeamSerializer, PlayerSerializer
from rest_framework import viewsets
from rest_framework import permissions

def index(request):
    club = Club.objects.first()
    live_matches = Match.objects.filter(enddate__isnull=True).order_by('court')
    ended_matches = Match.objects.filter(enddate__isnull=False).order_by('court')
    template = loader.get_template('scores/index.html')
    context = {
        'club': club,
        'live_matches': live_matches,
        'ended_matches': ended_matches,
    }
    return HttpResponse(template.render(context, request))

def tv_index(request):
    club = Club.objects.first()
    live_matches = Match.objects.filter(enddate__isnull=True).order_by('court')
    ended_matches = Match.objects.filter(enddate__isnull=False).order_by('court')
    template = loader.get_template('scores/index.html')
    context = {
        'club': club,
        'tv': True,
        'live_matches': live_matches,
        'ended_matches': ended_matches,
    }
    return HttpResponse(template.render(context, request))

def club(request, club_id):

    club = get_object_or_404(Club, pk=club_id)
    template = loader.get_template('scores/club.html')
    context = {
        'club': club,
    }
    return HttpResponse(template.render(context, request))


def club_name(request, club_name):

    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournaments = Tournament.objects.filter(club=club.id).order_by('id').reverse()
    template = loader.get_template('scores/club.html')
    context = {
        'club': club,
        'tournaments': tournaments,
    }
    return HttpResponse(template.render(context, request))

def tv_club_name(request, club_name):

    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournaments = Tournament.objects.filter(club=club.id)
    template = loader.get_template('scores/club.html').order_by('id').reverse()
    context = {
        'club': club,
        'tournaments': tournaments,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))

def tournament_name(request, tournament_shortname):
    tournamentFound = Tournament.objects.filter(shortname__iexact=tournament_shortname.lower()).first()
    return tournament(request, tournamentFound.id)

def club_name_tournament(request, club_name, tournament_id):
    return tournament(request, tournament_id)

def club_name_tournament_name(request, club_name, tournament_shortname):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournamentFound = Tournament.objects.filter(club_id=club.id, shortname__iexact=tournament_shortname.lower()).first()
    return tournament(request, tournamentFound.id)

def tournament(request, tournament_id):

    tournament = get_object_or_404(Tournament, pk=tournament_id)
    live_matches = Match.objects.filter(tournament=tournament.id, enddate__isnull=True).order_by('court')
    ended_matches = Match.objects.filter(tournament=tournament.id, enddate__isnull=False).order_by('court')

    template = loader.get_template('scores/tournament.html')
    context = {
        'tournament': tournament,
        'live_matches': live_matches,
        'ended_matches': ended_matches,
    }
    return HttpResponse(template.render(context, request))

def match(request, match_id):

    match = get_object_or_404(Match, pk=match_id)
    template = loader.get_template('scores/match.html')
    context = {
        'match': match,
    }
    return HttpResponse(template.render(context, request))

def match_tv(request, match_id):

    match = get_object_or_404(Match, pk=match_id)
    template = loader.get_template('scores/match.html')
    context = {
        'match': match,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))

    
def teams_tournament_name(request, tournament_shortname):
    tournament = get_object_or_404(Tournament, shortname__iexact=tournament_shortname.lower())
    return teams_tournament(request, tournament.id)

def teams_club_name_tournament(request, club_name, tournament_id):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournament = get_object_or_404(Tournament, pk=tournament_id)
    return teams_tournament(request, tournament.id)

def teams_club_name_tournament_name(request, club_name, tournament_shortname):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournament = Tournament.objects.filter(club_id=club.id, shortname__iexact=tournament_shortname.lower()).first()
    return teams_tournament(request, tournament.id)

def teams_tournament(request, tournament_id):

    tournament = get_object_or_404(Tournament, pk=tournament_id)
    template = loader.get_template('scores/teams.html')
    context = {
        'tournament': tournament,
    }
    return HttpResponse(template.render(context, request))

def tv_index(request):
    club = Club.objects.first()
    live_matches = Match.objects.filter(enddate__isnull=True).order_by('court')
    ended_matches = Match.objects.filter(enddate__isnull=False).order_by('court')
    template = loader.get_template('scores/index.html')
    context = {
        'club': club,
        'live_matches': live_matches,
        'ended_matches': ended_matches,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))

def tv_tournament_name(request, tournament_shortname):
    tournamentFound = Tournament.objects.filter(shortname__iexact=tournament_shortname.lower()).first()
    return tv_tournament(request, tournamentFound.id)

def tv_club_name_tournament(request, club_name, tournament_id):
    return tv_tournament(request, tournament_id)

def tv_club_name_tournament_name(request, club_name, tournament_shortname):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournamentFound = Tournament.objects.filter(club_id=club.id, shortname__iexact=tournament_shortname.lower()).first()
    return tv_tournament(request, tournamentFound.id)

def tv_tournament(request, tournament_id):

    tournament = get_object_or_404(Tournament, pk=tournament_id)
    live_matches = Match.objects.filter(tournament=tournament.id, enddate__isnull=True).order_by('court')
    ended_matches = Match.objects.filter(tournament=tournament.id, enddate__isnull=False).order_by('court')

    template = loader.get_template('scores/tournament.html')
    context = {
        'tournament': tournament,
        'live_matches': live_matches,
        'ended_matches': ended_matches,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))
    
def tv_teams_tournament_name(request, tournament_shortname):
    tournament = get_object_or_404(Tournament, shortname__iexact=tournament_shortname.lower())
    return tv_teams_tournament(request, tournament.id)

def tv_teams_club_name_tournament(request, club_name, tournament_id):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournament = get_object_or_404(Tournament, pk=tournament_id)
    return tv_teams_tournament(request, tournament.id)

def tv_teams_club_name_tournament_name(request, club_name, tournament_shortname):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournament = Tournament.objects.filter(club_id=club.id, shortname__iexact=tournament_shortname.lower()).first()
    return tv_teams_tournament(request, tournament.id)

def tv_teams_tournament(request, tournament_id):

    tournament = get_object_or_404(Tournament, pk=tournament_id)
    template = loader.get_template('scores/teams.html')
    context = {
        'tournament': tournament,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))

def ranking_tournament_id(request, club_name, tournament_id):
    tournament = get_object_or_404(Tournament, pk=tournament_id)
    template = loader.get_template('scores/ranks.html')
    context = {
        'tournament': tournament,
    }
    return HttpResponse(template.render(context, request))

def tv_ranking_tournament_id(request, club_name, tournament_id):
    tournament = get_object_or_404(Tournament, pk=tournament_id)
    template = loader.get_template('scores/ranks.html')
    context = {
        'tournament': tournament,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))


def tv_ranking(request, club_name, tournament_shortname):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournament = Tournament.objects.filter(club_id=club.id, shortname__iexact=tournament_shortname.lower()).first()
    template = loader.get_template('scores/ranks.html')
    context = {
        'tournament': tournament,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))

def ranking(request, club_name, tournament_shortname):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournament = Tournament.objects.filter(club_id=club.id, shortname__iexact=tournament_shortname.lower()).first()
    template = loader.get_template('scores/ranks.html')
    context = {
        'tournament': tournament,
    }
    return HttpResponse(template.render(context, request))

def brackets_tournament_id(request, club_name, tournament_id):
    tournament = get_object_or_404(Tournament, pk=tournament_id)
    template = loader.get_template('scores/brackets.html')
    allBrackets = Match.objects.filter(tournament=tournament.id).order_by('court')
    context = {
        'tournament': tournament,
        'brackets': allBrackets,
    }
    return HttpResponse(template.render(context, request))

def tv_brackets_tournament_id(request, club_name, tournament_id):
    tournament = get_object_or_404(Tournament, pk=tournament_id)
    template = loader.get_template('scores/brackets.html')
    allBrackets = Match.objects.filter(tournament=tournament.id).order_by('court')
    context = {
        'tournament': tournament,
        'brackets': allBrackets,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))


def tv_brackets(request, club_name, tournament_shortname):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournament = Tournament.objects.filter(club_id=club.id, shortname__iexact=tournament_shortname.lower()).first()
    template = loader.get_template('scores/brackets.html')
    allBrackets = Match.objects.filter(tournament=tournament.id).order_by('court')
    context = {
        'tournament': tournament,
        'brackets': allBrackets,
        'tv': True,
    }
    return HttpResponse(template.render(context, request))

def brackets(request, club_name, tournament_shortname):
    club = get_object_or_404(Club, name__iexact=club_name.lower())
    tournament = Tournament.objects.filter(club_id=club.id, shortname__iexact=tournament_shortname.lower()).first()
    template = loader.get_template('scores/brackets.html')
    allBrackets = Match.objects.filter(tournament=tournament.id).order_by('court')
    context = {
        'tournament': tournament,
        'brackets': allBrackets,
    }
    return HttpResponse(template.render(context, request))

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]

class ClubViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows clubs to be viewed or edited.
    """
    queryset = Club.objects.all().order_by('id')
    serializer_class = ClubSerializer
    permission_classes = [permissions.IsAuthenticated]

class TournamentViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows clubs to be viewed or edited.
    """
    queryset = Tournament.objects.all().order_by('id')
    serializer_class = TournamentSerializer
    permission_classes = [permissions.IsAuthenticated]

class MatchViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows matches to be viewed or edited.
    """
    queryset = Match.objects.all().order_by('court')
    serializer_class = MatchSerializer
    permission_classes = [permissions.IsAuthenticated]

class TeamViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows teams to be viewed or edited.
    """
    queryset = Team.objects.all().order_by('id')
    serializer_class = TeamSerializer
    permission_classes = [permissions.IsAuthenticated]

class PlayerViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows teams to be viewed or edited.
    """
    queryset = Player.objects.all().order_by('id')
    serializer_class = PlayerSerializer
    permission_classes = [permissions.IsAuthenticated]