from django.contrib import messages
from django.shortcuts import render, redirect
from django.shortcuts import get_object_or_404
from CaptainConsole.models import Products, ProductImages, Reviews, Profile, PreviouslyViewed, SearchHistory, ContactInfo, PaymentAndShipping, Orders
from CaptainConsole.forms.cc_form import ProductCreateForm, ProductUpdateForm, AddImageForm, ProfileForm, ReviewCreateForm, CustomUserCreationForm, PreviouslyViewedForm, SearchHistoryForm, ContactInfoForm, PaymentAndShippingForm, OrderForm
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
from django.utils import timezone
from cart.cart import Cart
import ast

# Create your views here.
def home(request):
    if 'search_filter' in request.GET:
        search_filter = request.GET['search_filter']
        if search_filter != "":
            if request.user.is_authenticated:
                form = SearchHistoryForm()
                search = form.save(commit=False)
                search.user = request.user
                search.searchQuery = search_filter
                search.datetime = timezone.now()
                search.save()
        ty = request.headers['addType']
        cat = request.headers['addCategory']
        order = request.headers['addFilter']
        products = []

        if order == 'by_name':
            productdata = [{
                'id': x.id,
                'name': x.name,
                'price': x.price,
                'manufacturer': x.manufacturer,
                'category': x.category,
                'description': x.description,
                'type': x.type,
                'mainImageLink': x.mainImageLink
            } for x in Products.objects.filter(name__icontains=search_filter).order_by('name')]
        elif order == 'by_price':
            productdata = [{
                'id': x.id,
                'name': x.name,
                'price': x.price,
                'manufacturer': x.manufacturer,
                'category': x.category,
                'description': x.description,
                'type': x.type,
                'mainImageLink': x.mainImageLink
            } for x in Products.objects.filter(name__icontains=search_filter).order_by('price')]
        else:
            productdata = [{
                'id': x.id,
                'name': x.name,
                'price': x.price,
                'manufacturer': x.manufacturer,
                'category': x.category,
                'description': x.description,
                'type': x.type,
                'mainImageLink': x.mainImageLink
            } for x in Products.objects.filter(name__icontains=search_filter)]

        for product in productdata:
            add_to_list = True
            if cat != 'none':
                if product['category'] != cat:
                    add_to_list = False
            if ty != 'none':
                if product['type'] != ty:
                    add_to_list = False
            if add_to_list:
                products.append(product)

        return JsonResponse({ 'data': products })
    if request.user.is_authenticated:
        context = {'products': Products.objects.all(),
                   'previous': PreviouslyViewed.objects.filter(user=request.user).order_by('-datetime')[:5]}
    else:
        context = {'products': Products.objects.all()}
    return render(request, 'CaptainConsole/home.html', context)

def get_product_by_id(request, id):
    if request.user.is_authenticated:
        form = PreviouslyViewedForm()
        prod = get_object_or_404(Products, pk=id)
        prev = form.save(commit=False)
        prev.product = prod
        prev.user = request.user
        prev.datetime = timezone.now()
        prev.save()
    context = {'product': get_object_or_404(Products, pk=id), 'images': ProductImages.objects.all(),
               'reviews': Reviews.objects.all(), 'profiles': Profile.objects.all()}
    return render(request, 'CaptainConsole/product.html', context)

@login_required
def add_product(request):
    return render(request, 'CaptainConsole/create_product.html')

@login_required
def create_product(request):
    if request.method == 'POST':
        form = ProductCreateForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:
        form = ProductCreateForm()
    return render(request, 'CaptainConsole/create_product.html', {'form': form})

def create_user(request):
    if request.method == 'POST':
        form = CustomUserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')
    else:
        form = CustomUserCreationForm()
    return render(request, 'CaptainConsole/signup.html', {'form': form})

@login_required
def delete_product(request, id):
    product = get_object_or_404(Products, pk=id)
    product.delete()
    return redirect('home')

@login_required
def update_product(request, id):
    instance = get_object_or_404(Products, pk=id)
    if request.method == 'POST':
        form = ProductUpdateForm(data=request.POST, instance=instance)
        if form.is_valid():
            form.save()
            return redirect('product_details', id=id)
    else:
        form = ProductUpdateForm(instance=instance)
    return render(request, 'CaptainConsole/update_product.html', {
        'form': form,
        'id': id
    })

@login_required
def add_image(request, id):
    if request.method == 'POST':
        form = AddImageForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('product_details', id=id)
    else:
        form = AddImageForm()
    return render(request, 'CaptainConsole/add_image.html', {
        'form': form,
        'id': id
    })

@login_required
def profile(request):
    profile = Profile.objects.filter(user=request.user).first()
    if request.method == 'POST':
        form = ProfileForm(instance=profile, data=request.POST)
        if form.is_valid():
            profile = form.save(commit=False)
            profile.user = request.user
            profile.save()
            return redirect('profile')
    return render(request, 'CaptainConsole/profile.html', {
        'form': ProfileForm(instance=profile)
    })

def review(request, id):
    review = Reviews.objects.filter(user=request.user).first()
    if request.method == 'POST':
        form = ReviewCreateForm(instance=review, data=request.POST)
        if form.is_valid():
            review = form.save(commit=False)
            review.user = request.user
            review.product_id = id
            review.datetime = timezone.now()
            review.save()
            return redirect('product_details', id=id)
    return render(request, 'CaptainConsole/review.html', {
        'form': ReviewCreateForm(instance=review)
    })


@login_required
def search_history(request):
    userID = request.user.id
    searches = []
    for x in SearchHistory.objects.all():
        if x.user_id == userID:
            searches.append(x)
    return render(request, 'CaptainConsole/search_history.html', {'searches': searches})


@login_required
def cart_add(request, id):
    cart = Cart(request)
    product = Products.objects.get(id=id)
    cart.add(product=product)
    return redirect("home")

@login_required
def item_clear(request, id):
    cart = Cart(request)
    product = Products.objects.get(id=id)
    cart.remove(product)
    return redirect("cart_detail")

@login_required
def item_increment(request, id):
    cart = Cart(request)
    product = Products.objects.get(id=id)
    cart.add(product=product)
    return redirect("cart_detail")

@login_required
def item_decrement(request, id):
    cart = Cart(request)
    product = Products.objects.get(id=id)
    cart.decrement(product=product)
    return redirect("cart_detail")

@login_required
def cart_clear(request):
    cart = Cart(request)
    cart.clear()
    return redirect("cart_detail")

@login_required
def cart_detail(request):
    cart = Cart(request)
    return render(request, 'CaptainConsole/cart-detail.html', {'cart': cart})

@login_required
def cart_info(request):
    cart = Cart(request)
    return render(request, 'CaptainConsole/cart-detail.html', {'cart': cart})

@login_required
def contact_info(request):
    cart = Cart(request)
    total = cart.get_total_price()
    contactinfo = ContactInfo.objects.filter(user=request.user).first()
    if request.method == 'POST':
        form = ContactInfoForm(instance=contactinfo, data=request.POST)
        if form.is_valid():
            contactinfo = form.save(commit=False)
            contactinfo.user = request.user
            contactinfo.save()
            return redirect('shipping_and_payment')
    return render(request, 'CaptainConsole/contact_info.html', {'form': ContactInfoForm(instance=contactinfo), 'total': total})

@login_required
def shipping_and_payment(request):
    cart = Cart(request)
    total = cart.get_total_price()
    paymentinfo = PaymentAndShipping.objects.filter(user=request.user).first()
    if request.method == 'POST':
        form = PaymentAndShippingForm(instance=paymentinfo, data=request.POST)
        if form.is_valid():
            paymentinfo = form.save(commit=False)
            paymentinfo.user = request.user
            paymentinfo.save()
            return redirect('order_review')
    return render(request, 'CaptainConsole/shipping_and_payment.html', {'form': PaymentAndShippingForm(instance=paymentinfo), 'total': total})

@login_required
def order_review(request):
    cart = Cart(request)
    contactinfo = ContactInfo.objects.filter(user=request.user).first()
    paymentinfo = PaymentAndShipping.objects.filter(user=request.user).first()
    return render(request, 'CaptainConsole/order_review.html', {
        'cart': cart,
        'payment': paymentinfo,
        'contact': contactinfo})

@login_required
def save_order(request):
    contactinfo = ContactInfo.objects.filter(user=request.user).first()
    paymentinfo = PaymentAndShipping.objects.filter(user=request.user).first()
    cart = Cart(request)
    cartinfo = cart.cart
    order = Orders(user=request.user, fullname=contactinfo.fullname, email=contactinfo.email, phone=contactinfo.phone,
               address=contactinfo.address, city=contactinfo.city, zip=contactinfo.zip, country=contactinfo.country,
               nameoncard=paymentinfo.nameoncard, creditcardnumber=paymentinfo.creditcardnumber,
               expirationdate=paymentinfo.expirationdate, cvv=paymentinfo.cvv, price=cart.get_total_price(),
               shippingcompany=paymentinfo.shippingcompany, shippingoption=paymentinfo.shippingoption, orderitems=cartinfo)
    order.save()
    cart.clear()
    return redirect('order_confirmation')

@login_required
def order_confirmation(request):
    orderinfo = Orders.objects.filter(user=request.user).last()
    orderinfo.orderitems = ast.literal_eval(orderinfo.orderitems)
    return render(request, 'CaptainConsole/order_confirmation.html', {'order': orderinfo})

@login_required
def order_history(request):
    allorders = Orders.objects.filter(user=request.user)
    for order in allorders:
        order.orderitems = ast.literal_eval(order.orderitems)
    return render(request, 'CaptainConsole/order_history.html', {'orders': allorders})

@login_required
def delete_search_history(request):
    SearchHistory.objects.all().delete()
    return redirect('home')

@login_required
def delete_previously_viewed(request):
    PreviouslyViewed.objects.all().delete()
    return redirect('home')

