"""
SYNCRO VIEWS
Frontend

"""

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from syncro.helpers import serieshelper, speakerhelper, searchhelpers


@login_required(login_url='/accounts/login/')
def home(request):
    template = loader.get_template('syncro_frontend/home.html')
    return HttpResponse(template.render({
        "series_count": serieshelper.get_series_count(),
        "speakers_count": speakerhelper.get_speaker_count()
    }, request))


@login_required(login_url='/accounts/login/')
def search_main(request):
    if request.method == 'GET':
        search_val = request.GET.get('search_value')
        print("[SEARCH][MAIN] searching for %s" % str(search_val))

        # search
        context = searchhelpers.search_main(search_val, request.user)
        if context is False:
            print("[SEARCH][MAIN] search failed!")
            return HttpResponseRedirect("/syncro")

        # return
        template = loader.get_template('syncro_frontend/search_result.html')
        return HttpResponse(template.render(context, request))
    else:
        print("[SEARCH][MAIN] wrong method %s" % str(request.method))
        return HttpResponseRedirect("/syncro")



# END
