"""
SIKD Navigator
"""

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse
from django import template
#Import model yg dibutuhkan
from artikel.models import Konten #Artikel
from banner.models import Slide #Banner
from ekosistem.models import Link #Banner
from faq.models import Category #FAQ


#@login_required(login_url="/login/")

def index(request):  
    context = {}
    context['segment'] = 'index'
    context['judul'] = 'Home'
    context['headline'] = 'SIKD NAVIGATOR'

    html_template = loader.get_template( 'index.html' )
    return HttpResponse(html_template.render(context, request))

@login_required(login_url="/login/")
def sinergidjpk(request):
    
    context = {}
    context['segment'] = 'index'
    html_template = loader.get_template( 'sinergidjpk.html' )
    return HttpResponse(html_template.render(context, request))


#Halaman utama SIKD Navigator
def home(request):
    #ambil artikel
    #list_konten = Konten.objects.all()
    list_sekilas = Konten.objects.filter(id=1)
    list_regulasi = Konten.objects.filter(id=2)
    list_pembakuan = Konten.objects.filter(id=3)
    list_faq_category = Category.objects.all()
    list_banner = Slide.objects.filter(featured=1).all()
    #list_ekosistem = Link.objects.order_by('id').all()
    list_ekosistem = Link.objects.filter(featured=1).order_by('id').all()
    
    context = {}
    context['segment'] = 'index'
    context['judul'] = 'Home'
    context['headline'] = 'SIKD NAVIGATOR'
    context['Konten_Sekilas'] = list_sekilas
    context['Konten_Regulasi'] = list_regulasi
    context['Konten_Pembakuan'] = list_pembakuan
    context['List_Category'] = list_faq_category
    context['List_Banner'] = list_banner
    context['List_Ekosistem'] = list_ekosistem
    

    html_template = loader.get_template( 'index.html' )
    return HttpResponse(html_template.render(context, request))

#@login_required(login_url="/login/")
def pages(request):
    context = {}
    # All resource paths end in .html.
    # Pick out the html file name from the url. And load that template.
    try:
        
        load_template      = request.path.split('/')[-1]
        context['segment'] = load_template
        
        html_template = loader.get_template( load_template )
        return HttpResponse(html_template.render(context, request))
        
    except template.TemplateDoesNotExist:

        html_template = loader.get_template( 'page-404.html' )
        return HttpResponse(html_template.render(context, request))

    except:
        html_template = loader.get_template( 'page-500.html' )
        return HttpResponse(html_template.render(context, request))
