from .models import PriceList
from users.models import Profile

class Cart():
    def __init__(self, request):
        self.session = request.session
        self.request = request
        
        cart = self.session.get('session_key')
        
        if 'session_key' not in request.session:
            cart = self.session['session_key'] = {}
            
        self.cart = cart
        
    def db_add(self, product, quantity):
        product_id = str(product)
        quantity = int(quantity)
        if quantity < 0:
            quantity = 0
        self.cart[product_id] = quantity
            
        self.session.modified = True
        
    def change(self, product, quantity):
        product_id = str(product.id)
        quantity = int(quantity)
        if quantity < 0:
            quantity = 0
        self.cart[product_id] = quantity
            
        self.session.modified = True
        if self.request.user.is_authenticated:
            current_user = Profile.objects.filter(user__id=self.request.user.pk)
            cart_str = str(self.cart).replace("\'", "\"")
            current_user.update(old_cart=cart_str) 
        
    def __len__(self):
        return len(self.cart)
    
    def get_prods(self):
        product_ids = self.cart.keys()
        products = []
        del_keys = []
        for key in product_ids:
            try:
                products.append(PriceList.objects.filter(pk=key)[0])
            except IndexError:
                del_keys.append(key)
        
        for key in del_keys:
            self.delete(key)
            
        return products
    
    def get_quants(self):
        quantities = self.cart
        return quantities
    
    def get_all_quantity(self):
        quantity_all = 0
        for value in self.cart.values():
            quantity_all += value
        return quantity_all
    
    def get_product_quantity(self, product):
        try:
            return self.cart[str(product.id)]
        except KeyError:
            return 0
    
    def delete(self, product):
        product_id = str(product)
        if product_id in self.cart:
            del self.cart[product_id]
            
        self.session.modified = True
        if self.request.user.is_authenticated:
            current_user = Profile.objects.filter(user__id=self.request.user.pk)
            cart_str = str(self.cart).replace("\'", "\"")
            current_user.update(old_cart=cart_str) 
        
    def clear(self):
        self.cart.clear()
        
    def total(self):
        product_ids = self.cart.keys()
        products = PriceList.objects.filter(pk__in=product_ids)
        quantities = self.cart
        total = 0
        for key, value in quantities.items():
            key = int(key)
            for product in products:
                if product.pk == key:
                    total = total + (product.price * value)
                    
        return total
        