from django.http import HttpResponse
from django.shortcuts import render, redirect, get_object_or_404, get_list_or_404
from .models import Post, Author, Category, Tag
from django.contrib import messages, auth
from .forms import FeedbackForm
from django.core.mail import mail_admins
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django_project import helpers





def index(request):
    return HttpResponse("Hello Django")


# view function to display a list of post
def post_list(request):
    posts = Post.objects.order_by("-id").all()
    posts = helpers.pg_records(request, posts, 3)
    return render(request, 'blog/post_list.html', {'posts': posts })

# view function to display a single post
def post_detail(request, pk, post_slug):
    post = get_object_or_404(Post.objects.order_by("-id"), pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

# view function to display post by category
def post_by_category(request, category_slug):
    category = get_object_or_404(Category, slug=category_slug)
    posts = get_list_or_404(Post.objects.order_by("-id"), category=category)
    posts = helpers.pg_records(request, posts,5)
    context = {
        'category': category,
        'posts': posts
        }
    print(category) 
    return render(request, 'blog/post_by_category.html', context)

# view function to display post by tag
def post_by_tag(request, tag_slug):
    tag = get_object_or_404(Tag, slug=tag_slug)
    posts = get_list_or_404(Post.objects.order_by("-id"), tags=tag)
    posts = helpers.pg_records(request, posts, 5)
    context = {
        'tag': tag,
        'posts': posts
        }
    return render(request, 'blog/post_by_tag.html', context)

def test_redirect(request):
    c = Category.objects.get(name='python')
    return redirect(c)

def feedback(request):
    if request.method == "POST":
        f = FeedbackForm(request.POST)
        
        if f.is_valid():
            name = f.cleaned_data['name']
            sender = f.cleaned_data['email']
            subject = "You have a new Feedback from {}:{}".format(name, sender)
            message = "Subject: {}\n\nMessage: {}".format(f.cleaned_data['subject'], f.cleaned_data['message'])
            mail_admins(subject, message)
            
            f.save()
            messages.add_message(request, messages.INFO, 'Feedback Submitted.')
            return redirect('feedback')
    else:
        f = FeedbackForm()
    return render(request, 'blog/feedback.html', {'form': f})

def test_cookie(request):
    if not request.COOKIES.get('color'):
        response = HttpResponse("Cookie Set")
        response.set_cookie('color', 'blue', 3600*24*365*2)
        return response
    else:
        return HttpResponse("Your favorit color is {0}".format(request.COOKIES['color']))

def track_user(request):
    # store response in response variable
    response = render(request, 'blog/track_user.html')
    if not request.COOKIES.get('visits'):
        response.set_cookie('visits', '1', 3600*24*365*2)
    else:
        visits = int(request.COOKIES.get('visits', '1')) + 1
        response.set_cookie('visits', str(visits), 3600*24*365*2)
    return response
def stop_tracking(request):
    if request.COOKIES.get('visits'):
        response = HttpResponse("Cookies Cleared")
        response.delete_cookie("visits")
    else:
        response = HttpResponse("We are not tracking you.")
    return response

def test_session(request):
    request.session.set_test_cookie()
    return HttpResponse("Testing session cookie")

def test_delete(request):
    if request.session.test_cookie_worked():
        request.session.delete_test_cookie()
        return HttpResponse("Cookie test passed")
    else:
        response = HttpResponse("Cookie test failed")
    return response

def save_session_data(request):
    # set new data
    request.session['id'] = 1
    request.session['name'] = 'root'
    request.session['password'] = 'rootpass'
    return HttpResponse("Session data saved")

def access_session_data(request):
    response = ""
    if request.session.get('id'):
        response += "Id : {0} <br>".format(request.session.get('id'))
    if request.session.get('name'):
        response += "Name : {0} <br>".format(request.session.get('name'))
    if request.session.get('password'):
        response += "Password : {0} <br>".format(request.session.get('password'))

    if not response:
        return HttpResponse("No session data")
    else:
        return HttpResponse(response)

def delete_session_data(request):
    try:
        del request.session['id']
        del request.session['name']
        del request.session['password']
    except KeyError:
        pass
    return HttpResponse("Session Data Cleared")

def lousy_login(request):
    if request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')
        if username == "root" and password == "pass":
            request.session['logged_in'] = True
            return redirect('lousy_secret')
        else:
            messages.error(request, 'Error wrong username/password')
    return render(request, 'blog/lousy_login.html')

def lousy_secret(request):
    if not request.session.get('logged_in'):
        return redirect('lousy_login')
    return render(request, 'blog/lousy_secret_page.html')

def lousy_logout(request):
    try:
        del request.session['logged_in']
    except KeyError:
        return redirect('lousy_login')
    return render(request, 'blog/lousy_logout.html')


def login(request):
    if request.user.is_authenticated():
        return redirect('account_info')

    if request.method ==  "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = auth.authenticate(username=username, password=password)

        if user is not None:
            # correct username and password login the user
            auth.login(request, user)
            return redirect('account_info')

        else:
            messages.error(request, 'Error wrong username/password')
    return render(request, 'cadmin/login.html')

def logout(request):
    auth.logout(request)
    return render(request, 'blog/logout.html')

def admin_page(request):
    if not request.user.is_authenticated():
        return redirect('cadmin_login')
    
    return render(request, 'cadmin/admin_page.html')

def contact_usg(request):
    return render(request, 'blog/contact_usg.html')

def contact_usv(request):
    return render(request, 'blog/contact_usv.html')
	
def home_page(request):
    return render(request, 'site/home_page.html')

def today_is(request):
    now = datetime.datetime.now()
    html = "<html><body>Current date and time: {0}</body></html>".format(now)
    return HttpResponse(html)

def opening_hours(request):
    return render(request, 'site/opening_hours.html')

def gps_page(request):
    return render(request, 'site/gps_page.html')

def cctv_page(request):
    return render(request, 'site/cctv_page.html')

def security_page(request):
    return render(request, 'site/security_page.html')

def services(request):
    return render(request, 'site/services.html')

def google(request):
    return render(request, 'blog/google9170e2a0ec62a1c0.html')

def bing(request):
    return render(request, 'blog/BingSiteAuth.xml')
    



