from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView
from django.shortcuts import render

from django.http import JsonResponse


##################################
##################################
class ExpertsView(TemplateView):
    template_name = "redirect/Experts.html"

    def get(self, request, *args, **kwargs):
        context = {'existing_questions': "ok"}
        return render(request=request, template_name = self.template_name, context=context)


def ajax_set_room(request):
    # Link to the proper objects
    address_from_client = request.GET.get('address', None)
    try:
        ofi = open("./redirect/anthropofield_rooms.txt", "w")
    except IOError:
        # Send back response
        data = {
            'address_setting_result': "Error, nothing set",
        }
        return JsonResponse(data)
    with ofi:
        # Update the file with the correct URL
        ofi.write(address_from_client)
        ofi.close()
        # Send back response
        data = {
            'address_setting_result': address_from_client,
        }
        return JsonResponse(data)


##################################
#################################
def get_room_redir(request):
    ofi = open('./redirect/anthropofield_rooms.txt', 'r')
    text = ofi.read()
    ofi.close()
    if text=="closed":
        return HttpResponse("Désolé, ce lien ne sera activé que lorsque l'expérience commencera")
    else:
        return HttpResponseRedirect(text)
