# Variables
shop_name = "Isa's Potions" # Change to your shop name which you will be using

# Item id to buy and sell.
buy_sell_item = 5256 # 5256 - eggs

# Sell all items from backpack, inventory and depotmail (sells everything at once).
sellWhole = True

# Don't modify these.
shopEnterDelay = 6000
ignore_menu = False
last_time_shop = 0
buyNow = True

def onTick():
	if last_time_shop > 0:
		cur_time = script.GetCurrentTime()
		if (cur_time - last_time_shop) > 20:
			script.StatusMessage('Was stuck...')
			doLeaveShop()
			
def onScriptActivation():
	global buyNow
	script.EnableTickEvent(10000)
	if script.GetFreeBackpackSlot() == 255: # full backpack, time to sell.
		buyNow = False
	doLeaveShop()

def onReceiveEnteredMenu(shopPacket, menuId, title):
	global ignore_menu, shopId
	
	if title == shop_name:
		ignore_menu = False
		script.SetVar('checkedBackpack', False)
		script.SetVar('checkedInv', False)
		script.SetVar('checkedDep', False)

		refreshTime()
		script.StatusMessage('Shop: ' + title)
		if shopPacket == 1:
			if buyNow:
				script.ChooseShopOption(menuId, 1) # Buy
			else:
				script.ChooseShopOption(menuId, 2) # Sell
	elif title == 'Buy':
		ignore_menu = False
		script.SetVar('lastMenuId', menuId)
		refreshTime()
		script.StatusMessage('Buying item...')
	elif title == 'Sell':
		ignore_menu = False
		script.SetVar('lastMenuId', menuId)
		refreshTime()
		script.StatusMessage('Selling items...')
	elif title == 'Review Sell':
		ignore_menu = True
		refreshTime()
		script.ChooseShopOption(menuId, 1)
	else:
		ignore_menu = True

def onReceiveMenuRows(rows):
	global buyNow

	if ignore_menu == True:
		return

	found = 0

	if buyNow and script.GetFreeBackpackSlot() == 255:
		script.StatusMessage('Backpack is now full.')
		buyNow = False
		onFinished()
		return

	if sellWhole and not buyNow:
		last_time_shop = script.GetCurrentTime()

		menuId = script.GetVar('lastMenuId')

		if not script.GetVar('checkedBackpack'):
			script.ChooseMenuOption(menuId, 1, 0)
			script.SetVar('checkedBackpack', True)
			return
		if not script.GetVar('checkedInv'):
			script.ChooseMenuOption(menuId, 1, 25600)
			script.SetVar('checkedInv', True)
			return
		if not script.GetVar('checkedDep'):
			script.ChooseMenuOption(menuId, 1, 25856)
			script.SetVar('checkedDep', True)
			return

		script.StatusMessage('Done selling.')
	else:
		for row in rows:
			list = row.split()
			menuId = int(list[0])
			rowId = int(list[1])
			itemId = int(list[-1])

			if itemId != buy_sell_item:
				continue

			refreshTime()
			found = 1

			script.ChooseMenuOption(menuId, 1, rowId)
			break # break to avoid flood

	if found == 0:
		script.StatusMessage('Done selling.')
		if buyNow and script.GetFreeBackpackSlot() == 255:
			buyNow = False
		elif not buyNow and script.GetItemsCount(buy_sell_item, False) < 1:
			buyNow = True
		onFinished()

def onFinished():
	doLeaveShop()
	
def refreshTime():
	global last_time_shop
	last_time_shop = script.GetCurrentTime()

def onReceiveMenuText(menuId, title, text):
	global buyNow
	if (not title == 'Review Sell') and 'No entry in the list' in text:
		if buyNow and script.GetFreeBackpackSlot() == 255:
			buyNow = False
		elif not buyNow and script.GetItemsCount(buy_sell_item, False) < 1:
			buyNow = True
		onFinished()

def doLeaveShop():
	if script.IsInShop():
		script.LeaveShopEx()
	executeEnterTask()

def executeEnterTask():
	refreshTime()
	script.RunEvent('enterShop', shopEnterDelay)

def enterShop():
	if script.EnterShop():
		script.StatusMessage('Entering shop.')
	else:
		script.Alarm('Can\'t find the shop.')
		executeEnterTask()

