from pygame import Surface, font
from copy import copy
from random import randint, choice
import string

from lib.transactionButton import TransactionButton

SHOP_PREFIX = ["archer", "baker", "fisher", "miller", "rancher", "robber"]
SHOP_SUFFIX = ["cave", "creek", "desert", "farm", "field", "forest", "hill", "lake", "mountain", "pass", "valley", "woods"]

class Shop():
	def __init__(self, name, inventory, priceModifier, groupInventory, groupMoney, itemPrices, position, blitPosition, money, resourcePath):
		self.yValue = 40
		self.groupInventory = groupInventory
		self.groupMoney = groupMoney
		self.priceModifier = priceModifier
		self.itemPrices = itemPrices
		self.inventory = inventory
		self.position = position
		self.blitPosition = blitPosition
		self.resourcePath = resourcePath
		self.buyButtonList = []
		self.sellButtonList = []
		self.xPos = (-self.position * 40) + 1280
		
		self.shopSurface = Surface((500, 300)).convert()
		self.sepLine = Surface((self.shopSurface.get_width(), 10)).convert()
		self.sepLine.fill((0, 0, 0))
		
		self.invContainer = Surface((self.shopSurface.get_width() - 20,
									 self.shopSurface.get_height() / 2 - 35)).convert()
		self.invContainer.fill((255, 255, 255))
		self.titleFont = font.Font("res/fonts/west.ttf", 17)
		self.textFont = font.Font("res/fonts/west.ttf", 15)
		
		if (name == ""):
			self.name = (choice(SHOP_PREFIX) + "'s " + choice(SHOP_SUFFIX)).capitalize()
		else:
			self.name = name
			
		if (self.inventory == {}):
			inventoryRandom = copy(self.groupInventory)
			for key in list(inventoryRandom.keys()):
				inventoryRandom[key] = randint(0, 10)
				
			inventoryRandom["Food"] *= 20
			self.inventory = inventoryRandom
			
		if (money is None):
			self.money = randint(200, 500)
		else:
			self.name = name
		self.render()
		
	def get_surface(self):
		self.render()
		return self.shopSurface
		
	def update(self, groupInv, groupMoney):
		self.groupInventory = groupInv
		self.groupMoney = groupMoney
		self.render()
		
	def move(self, moveValue):
		self.xPos += (2 * moveValue)
		self.render()
		
	def render(self):
		self.yValue = 40
		self.shopSurface.fill((133, 94, 66))
		self.shopSurface.blit(self.titleFont.render(self.name + " - $" + str(self.money), 1, (0, 0, 255)), (10, 5))
		self.shopSurface.blit(self.invContainer, (10, 25))
		self.shopSurface.blit(self.invContainer, (10, self.shopSurface.get_height() / 2 + 30))
		self.shopSurface.blit(self.textFont.render("Inventory", 1, (255, 0, 0)), (10, 25))
		self.shopSurface.blit(self.textFont.render("Amount", 1, (255, 0, 0)), (130, 25))
		self.shopSurface.blit(self.textFont.render("Price", 1, (255, 0, 0)), (200, 25))
		
		for key in list(self.inventory.keys()):
			self.shopSurface.blit(self.textFont.render(key + ":", 1, (0, 0, 0)), (10, self.yValue))
			self.shopSurface.blit(self.textFont.render(str(self.inventory[key]), 1,
													   (0, 0, 0)), (150, self.yValue))
			self.shopSurface.blit(self.textFont.render("$"+str(self.itemPrices[key] * self.priceModifier), 1, 
													   (0, 0, 0)), (200, self.yValue))
			if (len(self.buyButtonList) < len(self.inventory.keys())):
				buttonPos = tuple(map(sum, zip(self.blitPosition, (250, self.yValue))))
				self.buyButtonList.append(TransactionButton(transaction = "buy",
															item = key,
															imagePosition = (250, self.yValue),
															rectPosition = buttonPos,
															resourcePath = self.resourcePath))
			self.yValue += 30
			
		for button in self.buyButtonList:
			self.shopSurface.blit(button.image, button.imagePosition)
			
		self.shopSurface.blit(self.sepLine, (0, float(self.shopSurface.get_height()) / 2))
		self.shopSurface.blit(self.titleFont.render("You - $" + str(self.groupMoney), 1, (0, 0, 255)),
												    (10, float(self.shopSurface.get_height()) / 2 + 10))
		self.shopSurface.blit(self.titleFont.render("Inventory", 1, (255, 0, 0)),
													(10, float(self.shopSurface.get_height()) / 2 + 30))
		self.shopSurface.blit(self.titleFont.render("Amount", 1, (255, 0, 0)),
													(130, float(self.shopSurface.get_height()) / 2 + 30))
		self.shopSurface.blit(self.titleFont.render("Price", 1, (255, 0, 0)),
													(200, float(self.shopSurface.get_height()) / 2 + 30))
													
		self.yValue = (float(self.shopSurface.get_height()) / 2) + 45
		
		
		for key in list(self.groupInventory.keys()):
			self.shopSurface.blit(self.textFont.render(key + ":", 1, (0, 0, 0)), (10, self.yValue))
			self.shopSurface.blit(self.textFont.render(str(self.groupInventory[key]), 1,
													  (0, 0, 0)), (150, self.yValue))
			self.shopSurface.blit(self.textFont.render("$" + str(self.itemPrices[key] * self.priceModifier), 1,
													  (0, 0, 0)), (200, self.yValue))
			if (len(self.sellButtonList) < len(self.inventory.keys())):
				buttonPos = tuple(map(sum, zip(self.blitPosition, (250, self.yValue))))
				self.sellButtonList.append(TransactionButton(transaction = "sell",
															 item = key,
															 imagePosition = (250, self.yValue),
															 rectPosition = buttonPos,
															 resourcePath = self.resourcePath))
			self.yValue += 30
			
		for button in self.sellButtonList:
			self.shopSurface.blit(button.image, button.imagePosition)
													
													
[((1236, 1271), 'pygame.font.Font', 'font.Font', (['"""res/fonts/west.ttf"""', '(17)'], {}), "('res/fonts/west.ttf', 17)\n", (1245, 1271), False, 'from pygame import Surface, font\n'), ((1290, 1325), 'pygame.font.Font', 'font.Font', (['"""res/fonts/west.ttf"""', '(15)'], {}), "('res/fonts/west.ttf', 15)\n", (1299, 1325), False, 'from pygame import Surface, font\n'), ((1510, 1535), 'copy.copy', 'copy', (['self.groupInventory'], {}), '(self.groupInventory)\n', (1514, 1535), False, 'from copy import copy\n'), ((1738, 1755), 'random.randint', 'randint', (['(200)', '(500)'], {}), '(200, 500)\n', (1745, 1755), False, 'from random import randint, choice\n'), ((913, 932), 'pygame.Surface', 'Surface', (['(500, 300)'], {}), '((500, 300))\n', (920, 932), False, 'from pygame import Surface, font\n'), ((1607, 1621), 'random.randint', 'randint', (['(0)', '(10)'], {}), '(0, 10)\n', (1614, 1621), False, 'from random import randint, choice\n'), ((3230, 3371), 'lib.transactionButton.TransactionButton', 'TransactionButton', ([], {'transaction': '"""buy"""', 'item': 'key', 'imagePosition': '(250, self.yValue)', 'rectPosition': 'buttonPos', 'resourcePath': 'self.resourcePath'}), "(transaction='buy', item=key, imagePosition=(250, self.\n    yValue), rectPosition=buttonPos, resourcePath=self.resourcePath)\n", (3247, 3371), False, 'from lib.transactionButton import TransactionButton\n'), ((4905, 5047), 'lib.transactionButton.TransactionButton', 'TransactionButton', ([], {'transaction': '"""sell"""', 'item': 'key', 'imagePosition': '(250, self.yValue)', 'rectPosition': 'buttonPos', 'resourcePath': 'self.resourcePath'}), "(transaction='sell', item=key, imagePosition=(250, self.\n    yValue), rectPosition=buttonPos, resourcePath=self.resourcePath)\n", (4922, 5047), False, 'from lib.transactionButton import TransactionButton\n'), ((1394, 1413), 'random.choice', 'choice', (['SHOP_SUFFIX'], {}), '(SHOP_SUFFIX)\n', (1400, 1413), False, 'from random import randint, choice\n'), ((1364, 1383), 'random.choice', 'choice', (['SHOP_PREFIX'], {}), '(SHOP_PREFIX)\n', (1370, 1383), False, 'from random import randint, choice\n')]