from django.shortcuts import render, redirect
import os.path
import os
from django.core.files.storage import FileSystemStorage
from .models import Media
from .forms import ImageForm
from django.conf import settings

def uploadContent(request):
    if request.method == "GET":
        return render(request, "uploadChoice.html")
    elif request.method == "POST":
        choice = request.POST["choices"]
        if choice == "image":
            return redirect("/upload_image/")
        elif choice == "video":
            return redirect("/upload_video/")
        else:
            return
    else:
        return

# Please fix the fact that this shit gets uploaded literally everywhere

def imageUpload(request):
    """Process images uploaded by users"""
    
    if request.user.groups.filter(name='Processor').exists():
        warning = True
    else:
        warning = False
    
    media = Media.objects.filter(user_id=request.user.id).last()
    print(media.tokenized)
    if media.tokenized is None:
        return render(request, "wait.html")

    print(request.user.id)
    if request.method == 'POST':

        if "image" in request.FILES['image'].content_type: 
            print("Image")
            form = ImageForm(request.POST, request.FILES)
            if form.is_valid():
                img_obj = form.instance
                img_obj.user = request.user
                form.save()
                return render(request, 'uploadImage.html', {'form': form, 'img_obj': img_obj})
        
        elif "video" in request.FILES['image'].content_type:
            form = ImageForm(request.POST, request.FILES)
            print("Video")
            print(request.FILES['image'])
            if form.is_valid(): 
                video_obj = form.instance
                video_obj.user = request.user
                video_obj.is_video = True
                form.save()
                return render(request, 'uploadImage.html', {'form': form, 'img_obj': video_obj, 'warning':warning})
            else:
                raise Exception("What")
        else:
            raise TypeError("Invalid File")
            
    else:
        form = ImageForm()
    return render(request, 'uploadImage.html', {'form': form, 'warning':warning})

# def videoUpload(request):
#     """Process videos uploaded by users"""
#     if request.method == 'POST':
#         form = VideoForm(request.POST, request.FILES)
#         if form.is_valid():
#             userIdModel = Videos.objects.filter(user=request.user.id)
#             img_obj = form.instance
#             img_obj.user = request.user
#             form.save()
#             return render(request, 'uploadVideo.html', {'form': form, 'img_obj': img_obj})

#     else:
#         form = VideoForm()
#     return render(request, 'uploadVideo.html', {'form': form})


def viewMedia(request):
    get_status = Media.objects.filter(user_id=request.user.id)
    approved = []
    declined = []
    processing = []
    for i in get_status:
        if i.tokenized:
            approved.append(i)
        if not i.tokenized:
            declined.append(i)
        if i.tokenized is None:
            processing.append(i)
        

    context={
        "approved":len(approved),
        "declined":len(declined),
        "processing":len(processing),

    }
    return render (request, 'showAllImage.html', context)

