<reponame>leejunweisg/NTUtor<gh_stars>0
from django.shortcuts import render
from django.contrib.auth.models import User 
from django.http.response import JsonResponse, HttpResponseRedirect
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from chats.models import Message 
from listings.models import Listing, TuitionSession       
from users.models import Profile                                        
from chats.serializers import MessageSerializer, UserSerializer 
 

# Views to return our serialized data 
# Users View
@login_required
@csrf_exempt
def user_list(request, pk=None):
    if request.method == 'GET':
        if pk:                                                                      
		# If PrimaryKey (id) of the user is specified in the url
            users = User.objects.filter(id=pk)              # Select only that particular user
        else:
            users = User.objects.all()                             # Else get all user list
        serializer = UserSerializer(users, many=True, context={'request': request}) 
        return JsonResponse(serializer.data, safe=False)               # Return serialized data
    elif request.method == 'POST':
        data = JSONParser().parse(request)           # On POST, parse the request object to obtain the data in json
        serializer = UserSerializer(data=data)        # Serialize the data
        if serializer.is_valid():
            serializer.save()                                            # Save if valid
            return JsonResponse(serializer.data, status=201)     # Return back the data on success
        return JsonResponse(serializer.errors, status=400)     # Return back the errors  if not valid

# Message View
@login_required
@csrf_exempt
def message_list(request, sender=None, receiver=None, listingID=None):
    """List all required messages, or create a new message."""
    if request.method == 'GET':
		#Requires sender, receiver, listingID as URL parameters
        messages = Message.objects.filter(listingID=listingID,sender_id=sender, receiver_id=receiver)
        serializer = MessageSerializer(messages, many=True, context={'request': request})
        print("GET serializer:",serializer)		
        return JsonResponse(serializer.data, safe=False)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        print(data)
        serializer = MessageSerializer(data=data)
        print("serializer:",serializer)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)

# View Chat History
@login_required
def chat_view(request):
    """Render the template with required context variables"""
    if request.method == "GET":
        h_list = get_history_list(request,Message.objects.all(),Listing.objects.all())
        return render(request, 'chat/chat_history.html', 
                      {'users': User.objects.exclude(username=request.user.username), #Returning context for all users except the current logged-in user
					  'listings': Listing.objects.all(),
					  'history_list': h_list}) 

# Function to get history list
@login_required
def get_history_list(request, messages, listings):
	h_list = []
	for message in messages:
		for listing in listings:
			if listing.listingID == message.listingID.pk:

				if message.receiver_id == request.user.id:
					value1 = [int(request.user.id) , int(message.sender_id) , int(message.listingID.pk)]
					if value1 not in h_list:
						h_list.append(value1)
				elif message.sender_id == request.user.id:
					value1 = [int(request.user.id), int(message.receiver_id) , int(message.listingID.pk)]
					if value1 not in h_list:
						h_list.append(value1)
	return h_list						
						
# View to render template for sending and receiving messages	
# Takes arguments 'listingID' ,'sender' and 'receiver' to identify the message list to return
@login_required
def message_listing_view(request, sender, receiver, listingID): 
    """Render the template with required context variables"""
    # identify tutor and tutee
    obj = Listing.objects.get(listingID=listingID) #listingID
    listType = obj.typeOfListing
    #print(obj)
    tutor =""
    tutee =""
    tutorID = ""
    tuteeID = ""

    sender_name = Profile.objects.get(user_id=sender)
    receiver_name = Profile.objects.get(user_id=receiver)
    #print(receiver_name)

    if listType == "providing": #Listing host is learner
        #print(obj.user)
        tutee = obj.user
        #compare sender and receiver, whatever is not tutee must be the tutor
        if tutee != sender_name:
            tutor = sender_name
            tutorID = sender
            tuteeID = receiver
        else :
            tutor = receiver_name
            tutorID = receiver
            tuteeID = sender
    else : #listing host is teacher
        tutor = obj.user
        if tutor != sender_name:
            tutee = sender_name
            tuteeID = sender        
            tutorID = receiver
        else :
            tutee = receiver_name
            tuteeID = receiver
            tutorID = sender
    #print(obj)
    #print(tutorID)
    #print(tuteeID)

    tuitionSession, created = TuitionSession.objects.get_or_create(tutor=tutor, learner=tutee, listing=obj)
    #TuitionSession.objects.get(tutor=tutor, learner=tutee, listing=obj).offer
    #print(tuitionSession.offer)
    #print(tuitionSession)

    listings = Listing.objects.get(listingID = listingID)
    
    
    context = {
        'listing_id' : listingID,
        'users': User.objects.exclude(username=request.user.username), #List of users
        'receiver': User.objects.get(id=receiver), # Receiver context user object for using in template
        'messages': Message.objects.filter(listingID=listingID,sender_id=sender, receiver_id=receiver) |
                                Message.objects.filter(listingID=listingID,sender_id=receiver, receiver_id=sender), # Return context with message objects where users are either sender or receiver.
        'listing': listings,
        'tuitionSession': tuitionSession,
        'user' : Profile.objects.get(user=request.user.id),
        'tutorID': tutorID,
        'receiverID' : receiver,
    }

    if request.method == "GET":
            return render(request, "chat/chat.html", context = context) 
    elif request.method == "POST":
        if request.POST.get("startSession"):
            tuitionSession.offer = 1 
            tuitionSession.save()
            context['tuitionSession'] = 1
            return HttpResponseRedirect(request.path)
        elif request.POST.get("acceptSession"):
            tuitionSession.offer = 2 
            tuitionSession.save()
            context['tuitionSession'] = 2
            return HttpResponseRedirect(request.path)
        elif request.POST.get("completeSession"):
            tuitionSession.offer = 3 
            context['tuitionSession'] = 3
            tuitionSession.completed = True
            tuitionSession.save()
            return HttpResponseRedirect(request.path)


@login_required
def message_view(request, sender, receiver): 
    """Render the template with required context variables"""
    if request.method == "GET":
        return render(request, "chat/messages.html",
                      {'users': User.objects.exclude(username=request.user.username), #List of users
                       'receiver': User.objects.get(id=receiver), # Receiver context user object for using in template
                       'messages': Message.objects.filter(sender_id=sender, receiver_id=receiver) | 
								   Message.objects.filter(sender_id=receiver, receiver_id=sender)})

from django.shortcuts import render
from django.contrib.auth.models import User 
from django.http.response import JsonResponse, HttpResponseRedirect
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from chats.models import Message 
from listings.models import Listing, TuitionSession       
from users.models import Profile                                        
from chats.serializers import MessageSerializer, UserSerializer 
 

# Views to return our serialized data 
# Users View
@login_required
@csrf_exempt
def user_list(request, pk=None):
    if request.method == 'GET':
        if pk:                                                                      
		# If PrimaryKey (id) of the user is specified in the url
            users = User.objects.filter(id=pk)              # Select only that particular user
        else:
            users = User.objects.all()                             # Else get all user list
        serializer = UserSerializer(users, many=True, context={'request': request}) 
        return JsonResponse(serializer.data, safe=False)               # Return serialized data
    elif request.method == 'POST':
        data = JSONParser().parse(request)           # On POST, parse the request object to obtain the data in json
        serializer = UserSerializer(data=data)        # Serialize the data
        if serializer.is_valid():
            serializer.save()                                            # Save if valid
            return JsonResponse(serializer.data, status=201)     # Return back the data on success
        return JsonResponse(serializer.errors, status=400)     # Return back the errors  if not valid

# Message View
@login_required
@csrf_exempt
def message_list(request, sender=None, receiver=None, listingID=None):
    """List all required messages, or create a new message."""
    if request.method == 'GET':
		#Requires sender, receiver, listingID as URL parameters
        messages = Message.objects.filter(listingID=listingID,sender_id=sender, receiver_id=receiver)
        serializer = MessageSerializer(messages, many=True, context={'request': request})
        print("GET serializer:",serializer)		
        return JsonResponse(serializer.data, safe=False)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        print(data)
        serializer = MessageSerializer(data=data)
        print("serializer:",serializer)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)

# View Chat History
@login_required
def chat_view(request):
    """Render the template with required context variables"""
    if request.method == "GET":
        h_list = get_history_list(request,Message.objects.all(),Listing.objects.all())
        return render(request, 'chat/chat_history.html', 
                      {'users': User.objects.exclude(username=request.user.username), #Returning context for all users except the current logged-in user
					  'listings': Listing.objects.all(),
					  'history_list': h_list}) 

# Function to get history list
@login_required
def get_history_list(request, messages, listings):
	h_list = []
	for message in messages:
		for listing in listings:
			if listing.listingID == message.listingID.pk:

				if message.receiver_id == request.user.id:
					value1 = [int(request.user.id) , int(message.sender_id) , int(message.listingID.pk)]
					if value1 not in h_list:
						h_list.append(value1)
				elif message.sender_id == request.user.id:
					value1 = [int(request.user.id), int(message.receiver_id) , int(message.listingID.pk)]
					if value1 not in h_list:
						h_list.append(value1)
	return h_list						
						
# View to render template for sending and receiving messages	
# Takes arguments 'listingID' ,'sender' and 'receiver' to identify the message list to return
@login_required
def message_listing_view(request, sender, receiver, listingID): 
    """Render the template with required context variables"""
    # identify tutor and tutee
    obj = Listing.objects.get(listingID=listingID) #listingID
    listType = obj.typeOfListing
    #print(obj)
    tutor =""
    tutee =""
    tutorID = ""
    tuteeID = ""

    sender_name = Profile.objects.get(user_id=sender)
    receiver_name = Profile.objects.get(user_id=receiver)
    #print(receiver_name)

    if listType == "providing": #Listing host is learner
        #print(obj.user)
        tutee = obj.user
        #compare sender and receiver, whatever is not tutee must be the tutor
        if tutee != sender_name:
            tutor = sender_name
            tutorID = sender
            tuteeID = receiver
        else :
            tutor = receiver_name
            tutorID = receiver
            tuteeID = sender
    else : #listing host is teacher
        tutor = obj.user
        if tutor != sender_name:
            tutee = sender_name
            tuteeID = sender        
            tutorID = receiver
        else :
            tutee = receiver_name
            tuteeID = receiver
            tutorID = sender
    #print(obj)
    #print(tutorID)
    #print(tuteeID)

    tuitionSession, created = TuitionSession.objects.get_or_create(tutor=tutor, learner=tutee, listing=obj)
    #TuitionSession.objects.get(tutor=tutor, learner=tutee, listing=obj).offer
    #print(tuitionSession.offer)
    #print(tuitionSession)

    listings = Listing.objects.get(listingID = listingID)
    
    
    context = {
        'listing_id' : listingID,
        'users': User.objects.exclude(username=request.user.username), #List of users
        'receiver': User.objects.get(id=receiver), # Receiver context user object for using in template
        'messages': Message.objects.filter(listingID=listingID,sender_id=sender, receiver_id=receiver) |
                                Message.objects.filter(listingID=listingID,sender_id=receiver, receiver_id=sender), # Return context with message objects where users are either sender or receiver.
        'listing': listings,
        'tuitionSession': tuitionSession,
        'user' : Profile.objects.get(user=request.user.id),
        'tutorID': tutorID,
        'receiverID' : receiver,
    }

    if request.method == "GET":
            return render(request, "chat/chat.html", context = context) 
    elif request.method == "POST":
        if request.POST.get("startSession"):
            tuitionSession.offer = 1 
            tuitionSession.save()
            context['tuitionSession'] = 1
            return HttpResponseRedirect(request.path)
        elif request.POST.get("acceptSession"):
            tuitionSession.offer = 2 
            tuitionSession.save()
            context['tuitionSession'] = 2
            return HttpResponseRedirect(request.path)
        elif request.POST.get("completeSession"):
            tuitionSession.offer = 3 
            context['tuitionSession'] = 3
            tuitionSession.completed = True
            tuitionSession.save()
            return HttpResponseRedirect(request.path)


@login_required
def message_view(request, sender, receiver): 
    """Render the template with required context variables"""
    if request.method == "GET":
        return render(request, "chat/messages.html",
                      {'users': User.objects.exclude(username=request.user.username), #List of users
                       'receiver': User.objects.get(id=receiver), # Receiver context user object for using in template
                       'messages': Message.objects.filter(sender_id=sender, receiver_id=receiver) | 
								   Message.objects.filter(sender_id=receiver, receiver_id=sender)})
# Views to return our serialized data # Users View # If PrimaryKey (id) of the user is specified in the url # Select only that particular user # Else get all user list # Return serialized data # On POST, parse the request object to obtain the data in json # Serialize the data # Save if valid # Return back the data on success # Return back the errors  if not valid # Message View List all required messages, or create a new message. #Requires sender, receiver, listingID as URL parameters # View Chat History Render the template with required context variables #Returning context for all users except the current logged-in user # Function to get history list # View to render template for sending and receiving messages # Takes arguments 'listingID' ,'sender' and 'receiver' to identify the message list to return Render the template with required context variables # identify tutor and tutee #listingID #print(obj) #print(receiver_name) #Listing host is learner #print(obj.user) #compare sender and receiver, whatever is not tutee must be the tutor #listing host is teacher #print(obj) #print(tutorID) #print(tuteeID) #TuitionSession.objects.get(tutor=tutor, learner=tutee, listing=obj).offer #print(tuitionSession.offer) #print(tuitionSession) #List of users # Receiver context user object for using in template # Return context with message objects where users are either sender or receiver. Render the template with required context variables #List of users # Receiver context user object for using in template