# playerClass.py
# This file contains all the information for the player.
# The main functions include: initiating the character, updating the character state, equiping
                            # and dropping items, moving, and returning player information

# Global imports
import random

# Local imports
from gameLogger import log, toast
from itemClass import item
from itemEffects import itemEffect
from localFunctions import getDistanceOf

ExtraPocketsEffect = itemEffect("Extra Pockets","This item has extra pockets which increases inventory space.", "InventorySpace", 2)
LargePocketsEffect = itemEffect("Large Pockets","This item has large pockets which increases inventory space.", "InventorySpace", 2)

class playerObject():
	def __init__(self, name="Mr. Swagger"):
		self.name  = name
		self.hp    = 20
		self.maxHP = self.hp
		self.lvl   = 100
		self.xp    = 0
		self.lvlup = 1
		self.money = 20

		self.effects = []

		self.defence = 0
		self.attack  = 0
	def generateNewWeapon(self,race=-1,playerLevel=-1,rarity=-1, inv=[]):
		log("Generating new weapon...")
		# Create a new item with some pre-setup stats.
		newItem = item(0,0,"w","Weapon Generation Test","This is being used as a weapon generation test for the game.","weapon")

		# Item generation code:
		# Choose a minimum playerlevel if one not decided:
		log("Setting item level...")
		if playerLevel == -1:
			playerLevel = random.randint(1,20) # with 20 being max so that it does not generate something with level 100 or something
		else:
			playerLevel = playerLevel + random.randint(0,2) - 1
		log("Player level: %d" % (playerLevel))

		# This chooses a rarity to use.
		# This code runs if there is a rarity already specified
		log("Setting item rarity...")
		if not rarity == -1:
			r = self.rarity[rarity]
		# This runs if there is not a rarity specified
		else:
			if playerLevel >= 25:
				num = random.randint(0+int(playerLevel/2),255)
			elif playerLevel >= 20:
				num = random.randint(0+int(playerLevel/2),245)
			elif playerLevel >= 10:
				num = random.randint(0+int(playerLevel/2),240)
			elif playerLevel >= 5:
				num = random.randint(0+int(playerLevel/2),220)
			else:
				num = random.randint(0+int(playerLevel/2),210)
			rares = []
			for rare in self.rarity:
				if self.rarity[rare][1] <= num <= self.rarity[rare][2] and playerLevel>=self.rarity[rare][0]:
					rares.append(rare)
			num = random.randint(0,len(rares)-1)
			rarity = rares[num]
			r = self.rarity[rarity]
		newItem.rarity = rarity
		log("Item worth: %s" % (rarity))

		worth = random.randint(int(playerLevel*self.rarity[rarity][3]/2),playerLevel*self.rarity[rarity][3])
		log("Item worth: %d" % (worth))
		log("Setting item effects and damage.")
		while worth >= 0:
			if worth > 30 and len(newItem.effects) < 3:
				if random.choice([True, False, False, False, False]):
					log("Generating item effect.")

					newEffect = itemEffect()
					n = random.randint(0,0)
					if n == 0:
						newEffect.effect = "DamageInstant"
					newEffect.name = random.choice(open(self.folder + self.effects["weapons"][0] + self.effects["weapons"][1+n]).read().splitlines())
					newEffect.desc = newEffect.name.split("|")[1]
					newEffect.name = newEffect.name.split("|")[0]
					newEffect.effectStrength = random.randint(1,int((worth-30)/3 + 1))
					newEffect.worth = 30 + ((newEffect.effectStrength-1) * 3)


					# Issue #10 happens here.
					newItem.effects.append(newEffect)
					worth -= newItem.effects[len(newItem.effects)-1].worth
					for i in inv:
						if i.iType == "weapon":
							log(str(i))

				else:
					d = random.randint(1,int(worth)/4+1)
					newItem.damage += d
					worth -= (3+random.randint(0,1)) * d
			else:
				newItem.damage += 1
				worth -= 3+random.randint(0,1)

		# This sets up the weapons race if there is not one specified
		if race == -1:
			race = random.choice(self.weaponRaces.keys())
		log("Item race: %s" % (race))

		# This sets the weapon type which will be used later.
		wepType = random.choice(open(self.folder + self.weaponRaces[race][1]).read().splitlines())
		newItem.weaponType = wepType
		newItem.icon = wepType[0]

		log("Setting item name and description.")
		# This generates the item based on its rarity that was decided above
		if rarity == "Very Common" or rarity == "Common" or rarity == "Uncommon":
			newItem.name = "%s" % (wepType)

		elif rarity == "Rare" or rarity == "Exotic":
			newItem.name = "%s %s" % (random.choice(open(self.folder + self.weaponRaces[race][0]).read().splitlines()),
				                      wepType)

		elif rarity == "Legendary" or rarity == "Ascendent":
			newItem.name = "%s %s %s" % (random.choice(open(self.folder + self.weaponRaces[race][0]).read().splitlines()),
				                         wepType,
				                         random.choice(open(self.folder + self.weaponRaces[race][2]).read().splitlines()))
		elif rarity == "Godly":
			newItem.name = "%s %s %s" % (random.choice(open(self.folder + self.weaponRaces[race][0]).read().splitlines()),
			                             wepType,
			                             random.choice(open(self.folder + self.weaponRaces[race][2]).read().splitlines()))

		# You can use <weptype> and <wepname> to make descriptions more dynamic
		newItem.desc = random.choice(open(self.folder+self.weaponRaces[race][3]).read().splitlines())
		for e in newItem.effects:
			newItem.desc = newItem.desc + e.desc
		newItem.desc = newItem.desc.replace("<weptype>",newItem.weaponType)
		newItem.desc = newItem.desc.replace("<wepname>",newItem.name)
		
		# Returns the new item to whatever called for it.
		log("Returning item.")
		return newItem