import tkinter as tk
from tkinter import ttk
import random 

items = [["🍎", 5] ,["🍐", 10],["🍒", 50],["bar", 100],["7", 1000],["💎", 5000]]
torf = {"yes" : True, "y": True, "true": True,"1":True, "no": False, "n": False, "false":False,"0": False, "auto": "auto"}
shoptext = {False: "Open shop", True: "Close shop"}
kredit = 1000
profittext = ""
shopopen = False
maxitem = 2
luck = 2
modifier = 1
luckvaleue = 500
maxitemvaleue = 500
buymaxitemdisplay = "buy better max item"
buymaxitemlabel = str(-maxitemvaleue)
autocost = "-500"
modifiercost = "-1000"
ownauto = False
ownmodifier = False

def getrandnum():
	numb = 0
	for round in range(0, maxitem, 1):
		proceed = random.randint(0, luck)
		if not proceed == 0:
			numb += 1
		else:
			break
	return numb
def buymaxitem():
	global kredit
	global maxitem
	global maxitemvaleue
	global buymaxitemdisplay
	global buymaxitemlabel
	if kredit > maxitemvaleue and not maxitem >= len(items)-1:
		kredit -= maxitemvaleue
		maxitemvaleue *= 2
		maxitem += 1
		moneydis.set(str(kredit))
		stonks.set(str(maxitemvaleue))
		buymaxitemlabel = str(-maxitemvaleue)
		ttk.Label(shopwindow, text=f"maxitem({items[maxitem][0]}){-maxitemvaleue}",width= 17, background="lightgray",relief=tk.RAISED).grid(row=0,column=0)
	if maxitem >= len(items)-1:
		buymaxitemlabel = ""
		buymaxitemdisplay = "max"
		ttk.Label(shopwindow, text=f"maxitem({items[maxitem][0]})",width= 17, background="lightgray",relief=tk.RAISED).grid(row=0,column=0)
	ttk.Button(shopwindow, text=buymaxitemdisplay, command=buymaxitem, width= 20).grid(row=0,column=1)

def buyluck():
	global kredit
	global luck
	global luckvaleue
	if kredit > luckvaleue:
		kredit -= maxitemvaleue
		luckvaleue *= 2
		luck += 1
		moneydis.set(str(kredit))
		stonks.set(str(luckvaleue))
		ttk.Label(shopwindow, text=f"luck({luck-1})-{luckvaleue}",width= 17, background="lightgray",relief=tk.RAISED).grid(row=1,column=0)

def buyauto():
	global shopwindow
	global kredit
	global ownauto
	global autocost
	if kredit > 500 and not ownauto:
		kredit -= 500
		ownauto = True
		autocost = ""
		shopwindow.destroy()
		shopwindow = shop()
		moneydis.set(str(kredit))
		stonks.set(str(-500))
def buymodefier():
	global shopwindow
	global kredit
	global ownmodifier
	global modifiercost
	if kredit > 1000 and not ownmodifier:
		kredit -= 1000
		ownmodifier = True
		modifiercost = ""
		shopwindow.destroy()
		shopwindow = shop()
		moneydis.set(str(kredit))
		stonks.set(str(-1000))

def autoplay():
	global autoentry
	for round in range(1, int(autoentry.get()), 1):
		kolo()
def setmodefier():
	global modefierentry
	global modifier
	modifier = int(modefierentry.get())
	ttk.Label(shopwindow, text=f"modifier {modifier}x {modifiercost}",width= 17, background="lightgray",relief=tk.RAISED).grid(row=3,column=0)


def kolo():
	global kredit
	global profittext
	if kredit < 5 * modifier:
		result.set("out of money")
	else:
		kredit -= 5 * modifier
		num1 = getrandnum()
		num2 = getrandnum()
		num3 = getrandnum()
		result.set(f"[{items[num1][0]}][{items[num2][0]}][{items[num3][0]}]")
		if num1 == num2 and num2 == num3:
			profittext = (items[num1][1] * modifier) - (5 * modifier)
			kredit += items[num1][1] * modifier
		else:
			profittext = str(-5 * modifier)
		moneydis.set(str(kredit))
		stonks.set(str(profittext))

class shop(tk.Toplevel):
	def __init__(self):
		super().__init__()
		self.title('obchod')
		self.geometry("350x100")
		global autoentry
		global modefierentry
		ttk.Label(self, text=f"maxitem({items[maxitem][0]}){buymaxitemlabel}",width= 17, background="lightgray",relief=tk.RAISED).grid(row=0,column=0)
		ttk.Label(self, text=f"luck({luck-1})-{luckvaleue}",width= 17, background="lightgray",relief=tk.RAISED).grid(row=1,column=0)
		ttk.Label(self, text=f"auto spin {autocost}",width= 17, background="lightgray",relief=tk.RAISED).grid(row=2,column=0)
		ttk.Label(self, text=f"modifier {modifier}x {modifiercost}",width= 17, background="lightgray",relief=tk.RAISED).grid(row=3,column=0)
		ttk.Button(self, text=buymaxitemdisplay, command=buymaxitem, width= 20).grid(row=0,column=1)
		ttk.Button(self, text="buy better luck", command=buyluck).grid(row=1,column=1)
		if not ownauto:
			ttk.Button(self, text="buy auto spin", command= buyauto).grid(row=2,column=1)
		else:
			global autoentry
			autoentry = tk.Entry(self)
			autoentry.grid(row=2, column=1)
			ttk.Button(self, text="auto spin", command=autoplay).grid(row=2,column=2)
			
		if not ownmodifier:
			ttk.Button(self, text="buy modifier", command=buymodefier).grid(row=3,column=1)
		else:
			modefierentry = ttk.Entry(self)
			modefierentry.grid(row=3, column=1)
			ttk.Button(self, text="set modefier", command=setmodefier).grid(row=3,column=2)

def onshopclose():
	global shopopen
	shopwindow.destroy()
	shopopen = False
	shopstate.set(str(shoptext[shopopen]))

def openshop():
	global shopwindow
	shopwindow = shop()
	shopwindow.protocol("WM_DELETE_WINDOW", onshopclose)


def opclshop():
	global shopopen
	if not shopopen:
		openshop()
		shopopen = True
	else:
		shopwindow.destroy()
		shopopen = False
	shopstate.set(str(shoptext[shopopen]))


window = tk.Tk

window = tk.Tk()
window.geometry('600x400')
window.title('Výherní automat')


result = tk.StringVar()
moneydis = tk.StringVar()
stonks = tk.StringVar()
shopstate= tk.StringVar()
moneydis.set(str(kredit))
shopstate.set(str(shoptext[shopopen]))

label = tk.Label(window, height=3, width=30, bg="lightblue",relief=tk.RAISED, font=("Arial", 20, "bold"),textvariable= result)
label.pack(pady= 50)

infoframe = tk.Frame(window)
infoframe.pack(pady=10)

buget = tk.Label(infoframe, text="buget", width=25, height=3, bg="lightgreen",relief=tk.RAISED, textvariable= moneydis)
buget.grid(row=0, column=1)

profit = tk.Label(infoframe, text="profit", width=25, height=3, bg="pink",relief=tk.RAISED, textvariable=stonks)       
profit.grid(row=0, column=2)

button = tk.Button(command=kolo, text="Spin", activebackground="blue", activeforeground="white", width=10, height=3)
button.pack()

opcl = tk.Button(command=opclshop, text="shop", activebackground="blue", activeforeground="white", width=10, height=3, textvariable=shopstate)
opcl.pack(side="right")

window.mainloop()

#udělal Martin Válek