from django.conf import settings
from .pos import PosSys
from .storefront import StoreFront
import json
from .models import Store

def pos(request):
    store = Store.objects.get(pk=settings.STORE_ID)
    cstr = request.GET.get('cstr', None)
    if cstr:
        storeFront = StoreFront(int(cstr))
    else:
        storeFront = None

    if hasattr(request, 'user') and request.user.is_authenticated:
        user = request.user
        pos = PosSys.restoreStore(user)
        storeInfo = pos.uiSys.renderStoreInfo()

        #ms = jso.loads(pos.settingSys.get('menus'))
        ms = []
        try:
            ms = json.loads(pos.settingSys.get('menus'))
        except TypeError:
            ms = []

        print(ms)
        print(type(ms))
        temp = []
        for i in ms:
            temp.append({'title': i['title'], 'menu': pos.uiSys.menu(i['name'])})
            #ms.append(pos.uiSys.menu(m))

        ctx  = {
            'pos': pos,
            'info': storeInfo,
            'menus': temp,
            'primaryMenu': pos.uiSys.menu('primary'),
            'store': store,
            'storeFront': storeFront,
        }
        return ctx
    else:
        ctx = {
            'pos': None,
            'info': None,
            'menus': [],
            'store': store,
            'storeFront': storeFront,
        }
        return ctx


