from django.shortcuts import HttpResponse
from Ecommerce import settings  

from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views.generic import View, UpdateView
from .forms import SignupForm
from django.contrib.auth.models import User
from django.utils.encoding import force_bytes, force_text
from django.contrib import messages
from django.contrib.sites.shortcuts import get_current_site
from django.contrib.auth import login, authenticate
from django.utils.http import urlsafe_base64_encode,urlsafe_base64_decode
from django.template.loader import render_to_string
from flipkart.tokens import account_activation_token
from django.core.mail import EmailMessage,send_mail
from .models import Product,Contact,Orders,OrderUpdate
from django.db.models import Q
from math import ceil
import logging
import json 
from django.views.decorators.csrf import csrf_exempt
from PayTm import Checksum
from django.contrib.auth.decorators import login_required

# Get an instance of a logger
MERCHANT_KEY='NK8h30a19NO&z9Gq'
logger = logging.getLogger(__name__)
# Create your views here.
@login_required(login_url='/login')
def index(request):
    allProds = []
    catprods = Product.objects.values('category', 'id')
    cats = {item['category'] for item in catprods}
    for cat in cats:
        prod = Product.objects.filter(category=cat)
        n = len(prod)
        nSlides = n // 4 + ceil((n / 4) - (n // 4))
        allProds.append([prod, range(1, nSlides), nSlides])
    params = {'allProds':allProds}   
    return render(request,'flipkart/index.html',params)
 
@login_required(login_url='/login')
def about(request):
      return render(request,'flipkart/about.html')

def checkout(request):
    if request.method=="POST":
        items_json = request.POST.get('itemsJson', '')
        name = request.POST.get('name', '')
        amount = request.POST.get('amount', '')
        email = request.POST.get('email', '')
        address = request.POST.get('address1', '') + " " + request.POST.get('address2', '')
        city = request.POST.get('city', '')
        state = request.POST.get('state', '')
        zip_code = request.POST.get('zip_code', '')
        phone = request.POST.get('phone', '')
        order = Orders(items_json=items_json, name=name, email=email, address=address, city=city,
                       state=state, zip_code=zip_code, phone=phone)
        order.save()
        thank = True
        id = order.order_id
        param_dict = {

                'MID': 'mstNNq20620258304261',
                'ORDER_ID': str(order.order_id),
                'TXN_AMOUNT': str(amount),
                'CUST_ID': email,
                'INDUSTRY_TYPE_ID': 'Retail',
                'WEBSITE': 'WEBSTAGING',
                'CHANNEL_ID': 'WEB',
                'CALLBACK_URL':'http://127.0.0.1:8000/handlerequest/',

        }
        print(id)
        param_dict['CHECKSUMHASH'] = Checksum.generate_checksum(param_dict, MERCHANT_KEY)
        return render(request, 'flipkart/paytm.html', {'param_dict': param_dict})
        return render(request, 'flipkart/checkout.html', {'thank':thank, 'id': id})
    return render(request, 'flipkart/checkout.html')


def contact(request):
    thank=False
    if request.method=="POST":
        name = request.POST.get('name', '')
        email = request.POST.get('email', '')
        phone = request.POST.get('phone', '')
        desc = request.POST.get('desc', '')
        contact = Contact(name=name, email=email, phone=phone, desc=desc)
        contact.save()
        thank=True
    return render(request,'flipkart/contact.html')

def tracker(request):
      if request.method=="POST":
        orderId = request.POST.get('orderId', '')
        email = request.POST.get('email', '')
        try:
            order = Orders.objects.filter(order_id=orderId, email=email)
            if len(order)>0:
                update = OrderUpdate.objects.filter(order_id=orderId)
                updates = []
                for item in update:
                    updates.append({'text': item.update_desc, 'time': item.timestamp})
                    response = json.dumps({"status":"success", "updates": updates, "itemsJson": order[0].items_json}, default=str)
                return HttpResponse(response)
            else:
                return HttpResponse('{"status":"noitem"}')
        except Exception as e:
            return HttpResponse('{"status":"error"}')

      return render(request, 'flipkart/tracker.html')
    
def search(request):
      return render(request,'flipkart/search.html')

def search_results(request):
    query = request.GET['query']
    print(query)
    allProducts= Product.objects.filter( Q(product_name__icontains=query) | Q(category__icontains=query) )
    print(allProducts) 
    params = {'allProducts':allProducts} 
    return render(request,'flipkart/search-results.html',params)
    # queryset=[]
    # queries=query.split(" ")
    # for q in queries:
    #     posts=Product.objects.filter(Q(product_name__icontrains=q) | Q(category__icontrains=q) | Q(subcategory__icontrains=q).distinct()
    #     for post in posts:
    #         queryset.append(post)
    # return list(set(queryset))     


def prodView(request,myid):
      product = Product.objects.filter(id=myid)
      return render(request,'flipkart/prodView.html',{'product':product[0]})

def activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except(TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None
    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.save()
        login(request, user, backend='django.contrib.auth.backends.ModelBackend')
        # return redirect('home')
        return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
    else:
        return HttpResponse('Activation link is invalid!')

# Sign Up View
def signup(request):
    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            mail_subject = 'Activate your blog account.'
            message = render_to_string('flipkart/acc_active_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid':urlsafe_base64_encode(force_bytes(user.pk)),
                'token':account_activation_token.make_token(user),
            })
            to_email = form.cleaned_data.get('email')

            res  = send_mail(mail_subject, message, settings.EMAIL_HOST_USER, [to_email])  
            if(res == 1):
                msg = "Mail Sent Successfuly"  
            else:  
                msg = "Mail could not sent" 
            return HttpResponse('Please checkout your email address to complete the registration')
    else:
        form = SignupForm()
    return render(request, 'flipkart/signup.html', {'form': form})

@csrf_exempt
def handlerequest(request):
    # paytm will send you post request here
    form = request.POST
    response_dict = {}
    for i in form.keys():
        response_dict[i] = form[i]
        if i == 'CHECKSUMHASH':
            checksum = form[i]

    verify = Checksum.verify_checksum(response_dict, MERCHANT_KEY, checksum)
    if verify:
        if response_dict['RESPCODE'] == '01':
            print('order successful')
        else:
            print('order was not successful because' + response_dict['RESPMSG'])
    return render(request, 'flipkart/paymentstatus.html', {'response': response_dict})