from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from pos.pos import PosSys
from pos.plot import PlotSys
from .utils import uiContext
from django.contrib import messages
from pos.forms import CodeForm
import plotly.graph_objects as go
import numpy as np
from base64 import b64encode
from plotly.offline import plot

np.random.seed(1)

N = 100
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sz = np.random.rand(N) * 30

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=x,
    y=y,
    mode="markers",
    marker=go.scatter.Marker(
        size=sz,
        color=colors,
        opacity=0.6,
        colorscale="Viridis"
    )
))

img_bytes = fig.to_image(format="png")
encoding = b64encode(img_bytes).decode()
img_b64 = "data:image/png;base64," + encoding
gantt_plot = plot(fig, output_type="div")

@login_required
def index(request):
    img = img_b64
    return render(request, 'pos/index.html', {'img': img})


@login_required
def admin(request):
    print(type(request))
    request.tum = "Hello"
    print("xxx")
    print(request.tum)
    ctx = uiContext(request.user, ['admin_menu1', 'admin_menu2'])
    img = img_b64

    plot = PlotSys(None)
    pie = plot.pieChart()
    form = CodeForm()
    return render(request, 'pos/admin.html', {'img': img, 'chart': gantt_plot, 'pie': pie, 'form': form})

@login_required
def adminOrderList(request):
    orderSys = request.pos.orderSys
    #orders = orderSys.list()
    filters, pageObj = orderSys.filter(request)
    displayList = orderSys.displayList

    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminOrderList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj
    })

@login_required
def adminOrderCreate(request):
    orderSys = request.pos.orderSys
    form = orderSys.form()
    if request.method == "POST":
        form = orderSys.form(request=request)
        if form.is_valid():
            o = form.save()
            messages.success(request, 'Order Created')
            return redirect('pos:orderEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminOrderCreate.html', {'form': form})

@login_required
def adminOrderEdit(request, oid):
    orderSys = request.pos.orderSys

    form = orderSys.form(oid=int(oid))
    formset = orderSys.formset(oid=int(oid))

    if request.method == "POST":

        if 'delete' in request.POST:
            orderSys.delete(oid=int(oid))
            return redirect('pos:adminOrderList')

        form = orderSys.form(request=request, oid=int(oid))
        formset = orderSys.formset(request=request, oid=int(oid))
        if form.is_valid() and formset.is_valid():
            o = form.save()
            o2 = formset.save()
            messages.success(request, 'Order Created')
            return redirect('pos:orderEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminOrderEdit.html', {'form': form, 'formset': formset})

@login_required
def adminMenuList(request):
    sys = request.pos.menuSys
    filters, pageObj = sys.filter(request)
    displayList = sys.displayList


    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminMenuList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj
    })

@login_required
def adminMenuCreate(request):
    sys = request.pos.menuSys
    form = sys.form()
    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():
            o = form.save(commit=False)
            o.store = request.pos.store
            o.save()
            messages.success(request, 'Menu Created')
            return redirect('pos:menuEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminMenuEdit.html', {'form': form})

@login_required
def adminMenuEdit(request, oid):
    sys = request.pos.menuSys

    form = sys.form(oid=int(oid))
    formset = sys.formset(oid=int(oid))
    formset2 = sys.formset2(oid=int(oid))

    formsets = {'Ingredient':formset, 'Files':formset2}

    if request.method == "POST":

        if 'delete' in request.POST:
            sys.delete(oid=int(oid))
            return redirect('pos:adminMenuList')

        form = sys.form(request=request, oid=int(oid))
        formset = sys.formset(request=request, oid=int(oid))
        formset2 = sys.formset2(request=request, oid=int(oid))
        if form.is_valid() and formset.is_valid() and formset2.is_valid():
            o = form.save()
            o2 = formset.save()
            o3 = formset2.save()
            messages.success(request, 'Menu updated')
            return redirect('pos:menuEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminMenuEdit.html', {'form': form, 'formsets': formsets})

@login_required
def adminBookingList(request):
    sys = request.pos.bookingSys
    filters, pageObj = sys.filter(request)
    displayList = sys.displayList

    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminBookingList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj
    })

@login_required
def adminBookingCreate(request):
    sys = request.pos.bookingSys
    form = sys.form()
    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():
            o = form.save(commit=False)
            o.store = request.pos.store
            o.save()
            messages.success(request, 'Booking Created')
            return redirect('pos:bookingEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminBookingEdit.html', {'form': form})

@login_required
def adminBookingEdit(request, oid):
    sys = request.pos.bookingSys

    form = sys.form(oid=int(oid))


    if request.method == "POST":

        if 'delete' in request.POST:
            sys.delete(oid=int(oid))
            return redirect('pos:adminMenuList')

        form = sys.form(request=request, oid=int(oid))
        # formset = sys.formset(request=request, oid=int(oid))
        # formset2 = sys.formset2(request=request, oid=int(oid))
        # if form.is_valid() and formset.is_valid() and formset2.is_valid():
        if form.is_valid():
            o = form.save()
            # o2 = formset.save()
            # o3 = formset2.save()
            messages.success(request, 'Booking updated')
            return redirect('pos:bookingEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminBookingEdit.html', {'form': form,})

@login_required
def adminCustomerList(request):
    sys = request.pos.customerSys
    filters, pageObj = sys.filter(request)
    displayList = sys.displayList

    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminCustomerList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj
    })

@login_required
def adminCustomerCreate(request):
    sys = request.pos.customerSys
    form = sys.form()
    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():
            o = form.save(commit=False)
            o.owner = request.user
            o.store = request.pos.store
            o.save()
            messages.success(request, 'Customer Created')
            return redirect('pos:customerEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminCustomerEdit.html', {'form': form})

@login_required
def adminCustomerEdit(request, oid):
    sys = request.pos.customerSys

    form = sys.form(oid=int(oid))


    if request.method == "POST":

        if 'delete' in request.POST:
            sys.delete(oid=int(oid))
            return redirect('pos:adminCustomerList')

        form = sys.form(request=request, oid=int(oid))
        # formset = sys.formset(request=request, oid=int(oid))
        # formset2 = sys.formset2(request=request, oid=int(oid))
        # if form.is_valid() and formset.is_valid() and formset2.is_valid():
        if form.is_valid():
            o = form.save()
            # o2 = formset.save()
            # o3 = formset2.save()
            messages.success(request, 'Customer updated')
            return redirect('pos:customerEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminCustomerEdit.html', {'form': form,})



@login_required
def adminUserList(request):
    sys = request.pos.userSys
    filters, pageObj = sys.filter(request)
    displayList = sys.displayList

    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminUserList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj
    })

@login_required
def adminUserCreate(request):
    sys = request.pos.userSys
    form = sys.form()
    #profileForm = sys.formset()

    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():
            o = form.save(commit=False)
            o.save()
            o.profile.store = request.pos.store
            o.profile.save()

            messages.success(request, 'User Created')
            return redirect('pos:userEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminUserEdit.html', {'form': form})

@login_required
def adminUserEdit(request, oid):
    sys = request.pos.userSys

    form = sys.form2(oid=int(oid))
    profileForm = sys.formset(oid=int(oid))

    if request.method == "POST":

        if 'delete' in request.POST:
            sys.delete(oid=int(oid))
            return redirect('pos:adminUserList')

        form = sys.form2(request=request, oid=int(oid))
        profileForm = sys.formset(request=request, oid=int(oid))
        # formset = sys.formset(request=request, oid=int(oid))
        # formset2 = sys.formset2(request=request, oid=int(oid))
        # if form.is_valid() and formset.is_valid() and formset2.is_valid():
        if form.is_valid() and profileForm.is_valid():
            o = form.save()
            o2 = profileForm.save()
            # o2 = formset.save()
            # o3 = formset2.save()
            messages.success(request, 'User updated')
            return redirect('pos:userEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminUserEdit.html', {'form': form, 'formsets': { 'Profile': profileForm}})

@login_required
def adminPostList(request):
    sys = request.pos.postSys
    filters, pageObj = sys.filter(request)
    displayList = sys.displayList

    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminPostList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj
    })

@login_required
def adminPostCreate(request):
    sys = request.pos.postSys
    form = sys.form()

    sf = sys.sideFields(form)

    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():

            o = form.save(commit=False)
            o.store = request.pos.store
            o.owner = request.user
            o.save()
            form.save_m2m()

            messages.success(request, 'Post Created')
            return redirect('pos:postEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminPostEdit.html', {'form': form, 'sideFields': sf})

@login_required
def adminPostEdit(request, oid):
    sys = request.pos.postSys

    form = sys.form(oid=int(oid))
    sf = sys.sideFields(form)
    formset = sys.formset(oid=int(oid))


    if request.method == "POST":

        if 'delete' in request.POST:
            sys.delete(oid=int(oid))
            return redirect('pos:adminPostList')

        form = sys.form(request=request, oid=int(oid))
        formset = sys.formset(request=request, oid=int(oid))
        # formset2 = sys.formset2(request=request, oid=int(oid))
        # if form.is_valid() and formset.is_valid() and formset2.is_valid():
        if form.is_valid() and formset.is_valid():
            o = form.save(commit=False)
            o.save()
            form.save_m2m()

            o2 = formset.save()
            # o3 = formset2.save()
            messages.success(request, 'Post updated')
            return redirect('pos:postEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminPostEdit.html', {'form': form,'formsets': {"Files":formset}, 'sideFields': sf})

@login_required
def adminKitchenList(request):
    return render(request, 'pos/adminKitchenList.html')


@login_required
def adminTableList(request):
    sys = request.pos.tableSys
    filters, pageObj = sys.filter(request)
    displayList = sys.displayList

    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminTableList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj
    })



@login_required
def adminTableCreate(request):
    sys = request.pos.tableSys
    form = sys.form()

    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():
            o = form.save(commit=False)
            o.store = request.pos.store
            o.save()
            messages.success(request, 'Table Created')
            return redirect('pos:tableEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminTableEdit.html', {'form': form})

@login_required
def adminTableEdit(request, oid):
    sys = request.pos.tableSys

    form = sys.form(oid=int(oid))
    #formset = sys.formset(oid=int(oid))

    if request.method == "POST":

        if 'delete' in request.POST:
            sys.delete(oid=int(oid))
            return redirect('pos:adminTableList')

        form = sys.form(request=request, oid=int(oid))
        #formset = sys.formset(request=request, oid=int(oid))
        #if form.is_valid() and formset.is_valid():
        if form.is_valid():
            o = form.save()
            #o2 = formset.save()
            messages.success(request, 'Table updated')
            return redirect('pos:tableEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminTableEdit.html', {'form': form,})

@login_required
def adminMessageList(request):
    sys = request.pos.messageSys
    filters, pageObj = sys.filter(request)
    displayList = sys.displayList

    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminMessageList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj
    })



@login_required
def adminMessageCreate(request):
    sys = request.pos.messageSys
    form = sys.form()

    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():
            o = form.save(commit=False)
            o.store = request.pos.store
            o.save()
            messages.success(request, 'Message Created')
            return redirect('pos:messageEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminMessageEdit.html', {'form': form})

@login_required
def adminMessageEdit(request, oid):
    sys = request.pos.messageSys

    form = sys.form(oid=int(oid))
    #formset = sys.formset(oid=int(oid))

    if request.method == "POST":

        if 'delete' in request.POST:
            sys.delete(oid=int(oid))
            return redirect('pos:messageList')

        form = sys.form(request=request, oid=int(oid))
        #formset = sys.formset(request=request, oid=int(oid))
        #if form.is_valid() and formset.is_valid():
        if form.is_valid():
            o = form.save()
            #o2 = formset.save()
            messages.success(request, 'Message updated')
            return redirect('pos:messageEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminMessageEdit.html', {'form': form,})

@login_required
def adminMarketing(request):
    return render(request, 'pos/adminMarketing.html')

@login_required
def adminCustomer(request):
    return render(request, 'pos/adminCustomer.html')

@login_required
def adminUI(request):
    sys = request.pos.uiSys
    return render(request, 'pos/adminUI.html')

@login_required
def adminStore(request):
    sys = request.pos.storeSys

    form = sys.form()
    formset = sys.formset()
    formset2 = sys.formset2()

    formsets = {'Store File': formset, 'Settings': formset2}

    if request.method == "POST":
        form = sys.form(request=request)
        formset = sys.formset(request=request)
        formset2 = sys.formset2(request=request)

        if form.is_valid() and formset.is_valid() and formset2.is_valid():
            o = form.save()
            o2 = formset.save()
            o3 = formset2.save()

            messages.success(request, 'Store Data Saved')
            return redirect('pos:adminStore')
    return render(request, 'pos/adminStore.html', {'form': form, 'formsets': formsets, 'formset2': formset2})

@login_required
def adminCode(request):
    sys = request.pos.codeSys
    form = sys.form()
    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():
            o = form.save()
            messages.success(request, 'Code Saved')
            return redirect('pos:adminCode')
    return render(request, 'pos/adminCode.html', {'form': form})



@login_required
def adminUIMenuList(request):
    sys = request.pos.uiSys
    filters, pageObj = sys.filter(request)
    displayList = sys.displayList

    primaryMenu  = sys.menu('primary')
    footer1Menu  = sys.menu('foooter1')
    footer2Menu  = sys.menu('foooter2')

    if request.method == "POST":
        print(request.POST.getlist("ids[]"))

    return render(request, 'pos/adminUIMenuList.html',{
        #'orders': orders,
        'filters': filters,
        'displayList': displayList,
        'pageObj': pageObj,
        'primaryMenu': primaryMenu,
        'footer1Menu': footer1Menu,
        'footer2Menu': footer2Menu
    })



@login_required
def adminUIMenuCreate(request):
    sys = request.pos.uiSys
    form = sys.form()

    if request.method == "POST":
        form = sys.form(request=request)
        if form.is_valid():
            o = form.save(commit=False)
            o.store = request.pos.store
            o.save()
            messages.success(request, 'UIMenu Created')
            return redirect('pos:UIMenuEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminUIMenuEdit.html', {'form': form})

@login_required
def adminUIMenuEdit(request, oid):
    sys = request.pos.uiSys

    form = sys.form(oid=int(oid))
    #formset = sys.formset(oid=int(oid))

    if request.method == "POST":

        if 'delete' in request.POST:
            sys.delete(oid=int(oid))
            return redirect('pos:adminUIMenuList')

        form = sys.form(request=request, oid=int(oid))
        #formset = sys.formset(request=request, oid=int(oid))
        #if form.is_valid() and formset.is_valid():
        if form.is_valid():
            o = form.save()
            #o2 = formset.save()
            messages.success(request, 'UIMenu updated')
            return redirect('pos:UIMenuEdit', oid=o.id)
        else:
            messages.error(request, form.errors)
    return render(request, 'pos/adminUIMenuEdit.html', {'form': form,})
