# Purchase.py

from gpanel import *

dataFile = "samples.dat"

def onMousePressed(x, y):
    global nbItems, purchase
    item = (int)(x // 80)
    if item < n:
        if purchase[item] == 0:
            purchase[item] = 1
            title("Item " + attributes[item] + " added to basket. Total: " + 
                   str(purchase.count(1)))
        else:
            title("Item " + attributes[item] + " already in basket. Total: " + 
                  str(purchase.count(1)))
    else:
        nbItems = purchase.count(1)
        title ("Checked out with " + str(nbItems) + " items")
        if nbItems > 0:
            fOut = open(dataFile, "a")
            sample = ','.join(str(e) for e in purchase)
            fOut.write(sample + "\n")
            fOut.close()
            purchase = [0] * n

attributes = ["bread", "cheese", "apple", "banana", "pasta", "sugo"]
n = 6
purchase = [0] * n

makeGPanel(Size(560, 80), mousePressed = onMousePressed)
title("Click maximal 6 items and check out")
window(0, 560, 0, 560)
for i in range(n):
    image(attributes[i] + ".png", 80 * i, 0)
image("checkout.png", 480, 0)
keep()