# Concave
# Copyright (C) 2012 Daniel Malmqvist
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import constants
import random
import pygame
import g

class Item():
    def __init__(self, x, y, itemType, level = -1, levelPoints = 0):
        if level == -1:
            self.level = g.getCurrentLevel()
        else:
            self.level = level

        self.x = x
        self.y = y
        self.type = itemType
        self.used = False
        self.level = level
        
        self.hitBox = pygame.rect.Rect(self.x, self.y, 32, 32)
        
        self.strExtra = 0
        self.magExtra = 0
        self.vitExtra = 0
        
        self.name = ""
        self.sell = 100
        self.buy = 300
        
        if level >= 0:
            self.spreadPoints(levelPoints)
            self.generateName()

    def spreadPoints(self, points):
        randAttribute1 = random.randint(0,2)
        randAttribute2 = random.randint(0,2)
        randAttribute3 = random.randint(0,2)
        

        if randAttribute1 == 0:
            self.strExtra += max(1, random.randint(0,points/3))
        elif randAttribute1 == 1:
            self.magExtra += max(1, random.randint(0,points/3))
        elif randAttribute1 == 2:
            self.vitExtra += max(1, random.randint(0,points/3))
        
        if randAttribute2 == 0:
            self.strExtra += max(1, random.randint(0,points/3))
        elif randAttribute2 == 1:
            self.magExtra += max(1, random.randint(0,points/3))
        elif randAttribute2 == 2:
            self.vitExtra += max(1, random.randint(0,points/3))
        
        if randAttribute3 == 0:
            self.strExtra += max(1, random.randint(0,points/3))
        elif randAttribute3 == 1:
            self.magExtra += max(1, random.randint(0,points/3))
        elif randAttribute3 == 2:
            self.vitExtra += max(1, random.randint(0,points/3))
                
    
    def generateName(self):
        if self.type == constants.HEAD():
            self.name = "Hood"
        elif self.type == constants.TORSO():
            self.name = "Armor"
        elif self.type == constants.LEGS():
            self.name = "Pants"
        elif self.type == constants.FEET():
            self.name = "Shoes"
        elif self.type == constants.BELT():
            self.name = "Belt"
        elif self.type == constants.DAGGER():
            self.name = "Dagger"
        elif self.type == constants.LONGSPEAR():
            self.name = "Long spear"
        elif self.type == constants.LONGSWORD():
            self.name = "Long sword"
        elif self.type == constants.RAPIER():
            self.name = "Rapier"
        elif self.type == constants.SPEAR():
            self.name = "Spear"
        elif self.type == constants.STAFF():
            self.name = "Staff"
        
        if self.vitExtra > 0 and self.strExtra == 0 and self.magExtra == 0:
            self.name += " of health"
        elif self.vitExtra == 0 and self.strExtra > 0 and self.magExtra == 0:
            self.name += " of might"
        elif self.vitExtra == 0 and self.strExtra == 0 and self.magExtra > 0:
            self.name += " of magic"
        elif self.vitExtra > 0 and self.strExtra > 0 and self.magExtra > 0:
            self.name = "Balanced " + self.name.lower()
        
        elif self.vitExtra > 0 and self.strExtra > 0 and self.magExtra == 0:
            self.name += " of might & health"
        elif self.vitExtra > 0 and self.strExtra == 0 and self.magExtra > 0:
            self.name += " of magic & health"
        elif self.vitExtra == 0 and self.strExtra > 0 and self.magExtra > 0:
            self.name += " of might & magic"
    
    
        self.sell = (self.strExtra + self.magExtra + self.vitExtra) * 10
        self.buy = 5 * self.sell
        
    def getImageIndex(self):
        # Sheet, x, y
        if self.type == constants.STAIRDOWN():
            return (0, 0, 0)
        elif self.type == constants.STAIRUP():
            return (0, 1, 0)
        
        return (0, 0, 0) 
    
    def getHitBox(self):
        return self.hitBox
    
    def getAttributeBonus(self):
        pass
    
    def toSaveText(self):
        text = str(self.type) + ","
        text += str(self.level) + ","
        text += str(self.strExtra) + ","
        text += str(self.magExtra) + ","
        text += str(self.vitExtra)
        
        return text
    
    def use(self):
        if self.type == constants.CHEST():
            chestItems = [constants.BELT(), constants.FEET(), constants.GOLD(), constants.HANDS(), constants.HEAD(), constants.LEGS(), constants.TORSO()]
            self.type = random.choice(chestItems)
        
            return True
        elif self.type == constants.STAIRDOWN():
            g.setCurrentLevel(g.getCurrentLevel()+1)
            return True
        
        elif self.type == constants.STAIRUP():
            g.setCurrentLevel(g.getCurrentLevel()-1)
            return True
        
        if not self.used:
            self.used = True
            return True
        return False

    def getText(self, comparedTo = None):
        if self.type == constants.CHEST():
            return "Chest"
        elif comparedTo:
            text = self.name + "\n"
            
            if self.type in [constants.HEAD(), constants.LEGS(), constants.TORSO(), constants.FEET(), constants.BELT()]:
                if comparedTo:
                    if self.level - comparedTo.level > 0:
                        text += "Armor:" + str(self.level) + " (+" + str(self.level - comparedTo.level) +")\n"
                    elif self.level - comparedTo.level == 0:
                        text += "Armor:" + str(self.level) + "\n"
                    else:
                        text += "Armor:" + str(self.level) + " (-" + str(self.level - comparedTo.level) +")\n"
                else:
                    text += "Armor:" + str(self.level)
            
            
            if self.strExtra-comparedTo.strExtra > 0:
                text += "STR:" + str(self.strExtra) + " (+" + str(self.strExtra-comparedTo.strExtra) + ")\n"
            elif self.strExtra-comparedTo.strExtra == 0:
                text += "STR:" + str(self.strExtra) +"\n"
            else:
                text += "STR:" + str(self.strExtra) + " (-" + str(self.strExtra-comparedTo.strExtra) + ")\n"
                
            if self.magExtra-comparedTo.magExtra > 0:
                text += "MAG:" + str(self.magExtra) + " (+" + str(self.magExtra-comparedTo.magExtra) + ")\n"
            elif self.magExtra-comparedTo.magExtra == 0:
                text += "MAG:" + str(self.magExtra) +"\n"
            else:
                text += "MAG:" + str(self.magExtra) + " (-" + str(self.magExtra-comparedTo.magExtra) + ")\n"
                
            if self.vitExtra-comparedTo.vitExtra > 0:
                text += "VIT:" + str(self.vitExtra) + " (+" + str(self.vitExtra-comparedTo.vitExtra) + ")"
            elif self.vitExtra-comparedTo.vitExtra == 0:
                text += "VIT:" + str(self.vitExtra)
            else:
                text += "VIT:" + str(self.vitExtra) + " (-" + str(self.vitExtra-comparedTo.vitExtra) + ")"
        else:
            text = self.name + "\n"
            if self not in [g.getInventroy().beltArmor, g.getInventroy().bodyArmor, g.getInventroy().feetArmor, g.getInventroy().headArmor, g.getInventroy().legsArmor, g.getInventroy().weapon]:
                if self.strExtra > 0:
                    text += "STR:" + str(self.strExtra) + " (+" + str(self.strExtra) +")"
                    if self.magExtra > 0 or self.vitExtra > 0:
                        text += "\n"
                    
                if self.magExtra > 0:
                    text += "MAG:" + str(self.magExtra) + " (+" + str(self.magExtra) +")"
                    if self.vitExtra > 0:
                        text += "\n"
                        
                if self.vitExtra > 0:
                    text += "VIT:" + str(self.vitExtra) + " (+" + str(self.vitExtra) +")"
            else:
                if self.strExtra > 0:
                    text += "STR:" + str(self.strExtra)
                    if self.magExtra > 0 or self.vitExtra > 0:
                        text += "\n"
                    
                if self.magExtra > 0:
                    text += "MAG:" + str(self.magExtra)
                    if self.vitExtra > 0:
                        text += "\n"
                        
                if self.vitExtra > 0:
                    text += "VIT:" + str(self.vitExtra)
                
        
        
        
        
        
        
        if (self.type == constants.LONGSPEAR() or self.type == constants.LONGSWORD() or 
                                self.type == constants.RAPIER()):
            text += "\nLong range"
        
        
        if self in g.getInventroy().allItems():
            text += "\nSell: " + str(self.sell)
        else:
            text += "\nBuy: " + str(self.buy)
        
        
        return text

