from django.shortcuts import render, redirect, reverse
from django.contrib.auth.decorators import login_required
from pos.pos import PosSys
from pos.storefront import StoreFront
from django.http import JsonResponse
from django.contrib import messages
# Create your views here.
def index(request):
    return render(request,'front/index.html')

@login_required
def myStore(request):
    pos = PosSys.restoreStore(request.user)
    storeInfo = pos.uiSys.renderStoreInfo()
    primaryMenu = pos.uiSys.menu('primary')
    formInstance = pos.uiSys.form('tableForm')

    print("Instance")
    print(formInstance)

    if request.method == "POST":
        formInstance = pos.uiSys.form('tableForm', request=request)
        if formInstance.is_valid():
            formInstance.save()
            print("valid")
        else:
            print(formInstance.errors)
            print("invalide")
        return redirect('front:myStore')

    return render(request, 'front/mystore.html', {'pos': pos, 'storeInfo': storeInfo, 'primaryMenu': primaryMenu, 'form': formInstance})

def storeFrontView(request):
    if request.storeFront:
        menus = request.storeFront.getMenus()[:10]
        promotions = request.storeFront.getPosts(cat='Promotions')
        recs = request.storeFront.getPosts(cat="Recommendations")
        reviews = request.storeFront.getPosts(cat="Reviews")
        return render(request, 'front/storeFront.html', {'promotions': promotions, 'recs': recs, 'reviews': reviews, 'menus': menus})
    return JsonResponse({'msg': "Please provide store id in querystring"})


def storeMenus(request, storeId):
    return render(request, 'front/storeMenus.html')

def viewMenu(request, menuId):
    return render(request, 'front/viewMenu.html')

def viewPost(request, pid):
    post = request.storeFront.getPostById(pid)
    return render(request, 'front/viewPost.html', {'post': post})

def viewStoreMap(request):
    location = request.storeFront.location
    form = request.storeFront.messageForm()
    if request.method == 'POST':
        form = request.storeFront.messageForm(request=request)
        if form.is_valid():
            o = form.save(commit=False)
            o.store = request.storeFront.store
            o.save()
            messages.success(request, 'Thank you for your contact')
            return redirect(reverse('front:viewStoreMap')+f"?cstr={request.storeFront.storeId}")
    return render(request, 'front/storeMap.html', {'geo': location[1], 'address': location[0], 'form': form})


def storeContact(request):
    form = request.storeFront.messageForm()
    if request.method == 'POST':
        form = request.storeFront.messageForm(request=request)
        if form.is_valid():
            o = form.save()
            messages.success(request, 'Thank you for your contact')
            return redirect(reverse('front:storeContact')+"?cstr="+request.storeFront.storeId)
    return render(request, 'front/storeContact.html', {'form': form})

def search(request):
    results  = None
    q = request.GET.get('q', '')
    print(q)
    if request.storeFront:
        results = request.storeFront.search(q=q)
    else:
        results = StoreFront.globalSearch(q)
        print(results)
    return render(request, 'front/search.html', {'results': results})

