from django.shortcuts import render
from django.shortcuts import redirect
from core.models import Video

# Create your views here.

def core(request):
    videos = Video.objects.all()
    return render(request, 'base.html', {'videos': videos[::-1]})

def random(request):
    videos = Video.objects.all().order_by("?")
    ran = videos.first()
    return redirect('/view/'+str(ran.id))

def status(request):
    highest_id = Video.objects.order_by('-id').first().id
    video_list = []

    for i in range(highest_id + 1):
        try:
            video = Video.objects.get(id=i)
            video_list.append(video)
        except Video.DoesNotExist:
            video_list.append(Video(id=i))

    return render(request, 'status.html', {'video_list': video_list[::-1]})

def view(request, id):
    vid = Video.objects.get(id = id)
    if vid:
        return render(request, "view.html", {'video': vid})

def about(request):
    return render(request, 'about.html')
