import pygame, sys, os, random, math, time, copy, json
from pygame import Rect, draw, QUIT, MOUSEMOTION, MOUSEBUTTONDOWN
from chordConversions import *
from display import resize_img

class Hero:
    def __init__(self, screen, maxAmo, foodAddTo, powersToGet, speedFactor,
                 moneyFactor, image, aimImage):
        
        self.sizeScaleFactor = screen.get_width()
        self.screenSize = (screen.get_width(), screen.get_height())
        rectSize = self.sizeScaleFactor * .1
        self.boundRect = Rect(0,0, rectSize, rectSize)
        self.wantedPos = [screen.get_width()/2, screen.get_height()/2]
        self.boundRect.center = self.wantedPos

        #the money to buy food
        self.incomeP
erTurn = moneyFactor * speedFactor * 1
        
        self
.powersToGet = powersToGet
        self.powerUp = PowerType(PowerType.noneType, None, 0, None, "")
        
        self.foodAddTo = foodAddTo
        self.speedFactor = speedFactor * self.sizeScaleFactor / 300
        self.shootSpeedFactor = speedFactor
        
        self.screen = screen
        self.color = (0, 100, 100)

        self.deadZone = .5
        self.moveSpeed = 350

        self.fDisplay = pygame.font.SysFont('Courier New', int(self.screenSize[0]/40))
        self.fontColor = (255,0,0)
        self.textStartPos = (0, self.screenSize[1] - self.fDisplay.get_height())
        self.textTop = self.textStartPos[1]
        self.tPowerX = self.screenSize[0] / 5 + self.textStartPos[0]

        self.refreshText()

        #self.image = pygame.transform.scale(image, (int(rectSize), int(rectSize)))
        self.image = resize_img(image, rectSize, True)

        self.aimImage = pygame.transform.scale(aimImage, (int(rectSize/2), int(rectSize/2)))
        self.aimRect = self.aimImage.get_rect()

    #moves the amount specified
    def move(self, moveChords):
        if self.powerUp.timeLeft > 0:
            self.powerUp.timeLeft -= self.speedFactor *2
        else:
            self.powerUp.type = PowerType.noneType
        
        self.direction = moveChords[1]

        moveChords[0] -= self.deadZone
        if moveChords[0] < 0:
            moveChords[0] = 0

        moveChords[0] *= self.speedFactor * self.moveSpeed
        
        moveCartChords = list(polar_to_cart(moveChords))

        ## Don't go past edges
        for dimNum in range(2):
            if ((self.wantedPos[dimNum] < 0 and moveCartChords[dimNum] < 0)
                    or (self.wantedPos[dimNum] > self.screenSize[dimNum] and moveCartChords[dimNum] > 0)): 
                moveCartChords[dimNum] = 0
        
        self.wantedPos[0] += moveCartChords[0]
        self.wantedPos[1] += moveCartChords[1]

        self.money += self.incomePerTurn;
        self.refreshText()

        self.boundRect.center = self.wantedPos

    def get_powers(self):
        for powerUp in self.powersToGet.allPowerUps:
            if powerUp.boundRect.colliderect(self.boundRect):
                if powerUp.powerType.type == PowerType.money:
                    self.money += powerUp.powerType.value
                else:
                    self.powerUp = copy.copy(powerUp.powerType)
                    self.refreshText()
                    
                self.powersToGet.remove(powerUp)

    #shoots food in the derection it's facing
    def shoot(self):
        foodCost = self.foodAddTo.get_food_cost()

        shootFood = False
        if self.powerUp.type == PowerType.freeFood:
            shootFood = True #Has power up
        elif self.money >= foodCost:
            self.money -= foodCost #no power up, so charge
            shootFood = True
        
        if shootFood:
            #actually shoot the food
            self.foodAddTo.add_food(self.boundRect.center, (self.shootSpeedFactor, self.direction))
            self.refreshText()

    #re displays the money text
    def refreshText(self):
        try:
            self.tDisplay = self.fDisplay.render('Money: ' + str(round(self.money, 2)), 1, self.fontColor)
        except:
            pass #the money is not entered by the game manager yet
        
        self.tPower = self.fDisplay.render('Power: ', 1, self.fontColor)
        tPowerEnd = self.tPowerX  + self.tPower.get_rect().width
        
        if self.powerUp.type != PowerType.noneType:
            self.tPowerType = self.fDisplay.render(self.powerUp.name, 1, self.fontColor)

            self.tPowerImageX = tPowerEnd
            self.tPowerTypeX = self.tPowerImageX + self.powerUp.image.get_rect().width
        else:
            self.tPowerType = self.fDisplay.render("None", 1, self.fontColor)
            self.tPowerTypeX = tPowerEnd
        
    #draws the hero on the screen
    def draw(self):
        self.screen.blit(self.tDisplay, self.textStartPos)

        self.screen.blit(self.tPower, (self.tPowerX, self.textTop))
        self.screen.blit(self.tPowerType, (self.tPowerTypeX, self.textTop))
        if self.powerUp.type != PowerType.noneType:
            self.screen.blit(self.powerUp.image, (self.tPowerImageX, self.textTop))
            
        self.screen.blit(self.image, self.boundRect)

        aimLoc = list(polar_to_cart((self.boundRect.width * 2, self.direction)))
        self.aimRect.centerx = self.boundRect.centerx + aimLoc[0]
        self.aimRect.centery = self.boundRect.centery + aimLoc[1]
        self.screen.blit(self.aimImage, self.aimRect)

class PeopleImages:
    def __init__(self, peopleImages, images=None, dead=None, eating=None, normal=None):
        if peopleImages:
            self.images = []
            for image in peopleImages.images:
                self.images.append(image)
                
            self.dead = peopleImages.dead
            self.eating = peopleImages.eating
            self.normal = peopleImages.normal

        else: 
            self.images = images
            self.dead = dead
            self.eating = eating
            self.normal = normal

# For storing the sounds that the person can make
class PersonSounds:
    pass
       
class Person:
    SIZE_FACTOR = .07
    def __init__(self, screen, images, sounds, otherPeopleGroup, widthToHeight, moveSpeedFactor):
        self.sizeScaleFactor = screen.get_width()
        self.screenSize = (screen.get_width(), screen.get_height())
        width = self.sizeScaleFactor * Person.SIZE_FACTOR
        self.boundRect = Rect(0,0, width, width * widthToHeight)
        self.boundRect.center = self.wantedPos = [random.randint(0, self.screenSize[0]),
                                                  random.randint(0, self.screenSize[1])]
        self.growFactor = 1.1
        
        self.allImages = PeopleImages(images)

        self.set_size([self.boundRect.width, self.boundRect.height])

        #The probability that the person will change direction or stop when they move
        self.changeDirectionProb = .03
        self.stopProb = .5
        
        self.screen = screen

        self.sounds = sounds

        self.speedFactor = moveSpeedFactor
        self.moveSpeed = self.speedFactor * self.sizeScaleFactor * 1.5

        #health
        self.fullHealth = 1.0
        self.health = self.fullHealth
        self.healthDecreaceAmount = self.speedFactor * 1.5
        self.isAlive = True

        self.timeToDigest = 0.0

        self.set_rand_dir()
        self.moveAmount = list(polar_to_cart((self.moveDistance, self.moveAngle)))

        self.isHungry = True
        self.fullfillment = 0

        self.otherPeople = otherPeopleGroup
        self.donationWait = 1.0
        self.lastDonationTime = self.donationWait
        
    def move(self):
        if(random.random() < self.changeDirectionProb):
            #change direction
            if(random.random() < self.stopProb):
                #stop
                self.moveDistance = 0.0
            else:
                #move somewhere
                self.set_rand_dir()
                self.moveAmount = list(polar_to_cart((self.moveDistance, self.moveAngle)))
