from __future__ import division
import pygame, math
import constants, vect


class image_data:
    def __init__(self, file):
        #world.name = name
        self.orig_image = pygame.image.load(file).convert_alpha()
        self.orig_rect = self.orig_image.get_rect()
        self.scaled_image = self.orig_image
        self.scaled_rect = self.scaled_image.get_rect()
        
        self.lengthx = self.orig_rect.width/constants.WORLD_PPM
        self.lengthy = self.orig_rect.height/constants.WORLD_PPM
        self.radius = max(self.orig_rect.width,self.orig_rect.height)/2/constants.WORLD_PPM
        
    def scale_image(self, scale):
        self.scaled_image = pygame.transform.scale(self.orig_image, (
            int(math.ceil(self.orig_rect.width*scale)),int(math.ceil(self.orig_rect.height*scale))))
        self.scaled_rect = self.scaled_image.get_rect()

def attenuate_sound(object):
    if object:
        if object.world.player:
            if object.world.player.health >=0:
                distance = vect.distance_to(object.location, object.world.player.location)
            else:
                distance = 0
        else:
            distance = 0
        factor = 1/max(1,math.sqrt(distance))
    else:
        factor = 0
    return factor

def play_sound(sound, volume_factor):
    """ 50m = max sounds["dist"""
    if volume_factor > 1/15:
        channel = pygame.mixer.find_channel()
        if channel:
            channel.set_volume(volume_factor)
            channel.play(sound)
           
def load_resources(world):
    load_sounds(world)
    load_images(world)

def load_sounds(world):
    """adds sounds"""
    world.sounds = {}
    
    world.sounds["brains"] = pygame.mixer.Sound("./res/sound/brains.ogg")
    world.sounds["brains"].set_volume(1)
    
    world.sounds["chicken"] =  pygame.mixer.Sound("./res/sound/chicken.ogg")
    world.sounds["chicken"].set_volume(1)
    
    world.sounds["meat"] =  pygame.mixer.Sound("./res/sound/meat.ogg")
    world.sounds["meat"].set_volume(1)
    
    world.sounds["hungry"] =  pygame.mixer.Sound("./res/sound/hungry.ogg") 
    world.sounds["hungry"].set_volume(1)
    
    world.sounds["zombie_normal_groan"] =  pygame.mixer.Sound("./res/sound/zombie_groan_normal.ogg")
    world.sounds["zombie_massive_groan"] =  pygame.mixer.Sound("./res/sound/zombie_groan_massive.ogg")
    world.sounds["zombie_fast_groan"] =  pygame.mixer.Sound("./res/sound/zombie_groan_fast.ogg")
    
    world.sounds["reload_Pistol"] =  pygame.mixer.Sound("./res/sound/reload_pistol.ogg")
    world.sounds["reload_SMG"] =  pygame.mixer.Sound("./res/sound/reload_rifle.ogg")
    world.sounds["reload_AssaultR"] =  pygame.mixer.Sound("./res/sound/reload_rifle.ogg")
    world.sounds["reload_SniperR"] =  pygame.mixer.Sound("./res/sound/reload_rifle.ogg")
    world.sounds["reload_GaussR"] =  pygame.mixer.Sound("./res/sound/reload_rifle.ogg")
    world.sounds["reload_LaserR"] =  None
    world.sounds["reload_GaussR"] =  pygame.mixer.Sound("./res/sound/reload_rifle.ogg")
    world.sounds["reload_MiniGun"] =  pygame.mixer.Sound("./res/sound/reload_rifle.ogg")
    
    
    world.sounds["rip"] =  pygame.mixer.Sound("./res/sound/rip.ogg") 
    world.sounds["rip_carcass"] =  pygame.mixer.Sound("./res/sound/rip_carcass.ogg") 
    world.sounds["fire"] =  pygame.mixer.Sound("./res/sound/fire1.ogg") 
    
    world.sounds["shoot_Pistol"] =  pygame.mixer.Sound("./res/sound/pistol_close.ogg")
    world.sounds["shoot_Pistol"].set_volume(1)
    
    world.sounds["shoot_SMG"] =  pygame.mixer.Sound("./res/sound/smg_close.ogg")
    world.sounds["shoot_SMG"].set_volume(0.6)
    
    world.sounds["shoot_AssaultR"] =  pygame.mixer.Sound("./res/sound/ar_close.ogg")
    world.sounds["shoot_AssaultR"].set_volume(1)
    
    world.sounds["shoot_Shotgun"] =  pygame.mixer.Sound("./res/sound/shotgun.ogg")
    world.sounds["shoot_Shotgun"].set_volume(1)
    world.sounds["reload_Shotgun"] =  pygame.mixer.Sound("./res/sound/reload_shotgun.ogg")
    
    world.sounds["shoot_SniperR"] =  pygame.mixer.Sound("./res/sound/rifle_close.ogg")
    world.sounds["shoot_SniperR"].set_volume(1)
    
    world.sounds["shoot_LaserR"] =  pygame.mixer.Sound("./res/sound/laser1.ogg")
    world.sounds["shoot_LaserR"].set_volume(0.1)
    
    world.sounds["shoot_MiniGun"] =  pygame.mixer.Sound("./res/sound/MG_close.ogg")
    world.sounds["shoot_MiniGun"].set_volume(1)
    
    world.sounds["shoot_GaussR"] =  pygame.mixer.Sound("./res/sound/GR_close.ogg")
    world.sounds["shoot_GaussR"].set_volume(1)
                            
    world.sounds["grenade_concussion"] =  pygame.mixer.Sound("./res/sound/explosion.ogg")
    world.sounds["grenade_concussion"].set_volume(1)
    
    world.sounds["grenade_molotov"] =  pygame.mixer.Sound("./res/sound/explosion.ogg")
    world.sounds["grenade_molotov"].set_volume(1)
    
    world.sounds["grenade_incendiary"] =  pygame.mixer.Sound("./res/sound/explosion.ogg")
    world.sounds["grenade_incendiary"].set_volume(1)
    
    world.sounds["explosion"] =  pygame.mixer.Sound("./res/sound/explosion.ogg")
    world.sounds["explosion"].set_volume(1)
    
    world.sounds["make_zombie"] =  pygame.mixer.Sound("./res/sound/make_zombie.ogg")
    world.sounds["make_zombie"].set_volume(0.02)
    
    world.sounds["bullet_flesh"] =  pygame.mixer.Sound("./res/sound/bullet_flesh.ogg")
    world.sounds["bullet_flesh"].set_volume(1)
            
def load_images(world):
    """ add images - this just loads them once"""
    """ we create a dictionary of image_data objects """
    world.images = {}
    
    world.images["survivor_image"] = image_data( './res/civilian.png')
    world.images["civilian_player_image"] = image_data( './res/civilian_player.png')
    world.images["remains_image"] = image_data('./res/remains.png')

    
    world.images["zombie_massive_image"] = image_data('./res/zombie_massive.png')
    world.images["zombie_massive_corpse_image"] = image_data('./res/zombie_massive_corpse.png')
            
    world.images["zombie_normal_image"] = image_data('./res/zombie_normal.png')
    world.images["zombie_normal_corpse_image"] = image_data('./res/zombie_normal_corpse.png')
    
    world.images["zombie_fast_image"] = image_data('./res/zombie_fast.png')
    world.images["zombie_fast_corpse_image"] = image_data('./res/zombie_fast_corpse.png')
    
    world.images["bullet_image"] = image_data('./res/bullet1.png')
    
    world.images["Pistol_bullet_image"] = world.images["bullet_image"]
    world.images["SMG_bullet_image"] = world.images["bullet_image"]
    world.images["AssaultR_bullet_image"] = world.images["bullet_image"]
    world.images["SniperR_bullet_image"] = world.images["bullet_image"]
    world.images["LaserR_bullet_image"] = world.images["bullet_image"]
    world.images["GaussR_bullet_image"] = world.images["bullet_image"]
    world.images["MiniGun_bullet_image"] = world.images["bullet_image"]
    
    world.images["concrete_block_image"] = image_data('./res/concrete_block.png')
    world.images["zombie_hive_image"] = image_data('./res/zombie_hive.png')
    world.images["shrub_image"] = image_data('./res/shrub.png')
    
    world.images["muzzle_flash_image"] = image_data('./res/muzzle_flash.png')
    world.images["blood_image"] = image_data('./res/blood2.png')
    world.images["fire_image"] = image_data('./res/fire.png')
    world.images["ricochet_image"] = image_data('./res/ricochet.png')
    world.images["rip_image"] = image_data('./res/rip.png')
    
    world.images["Pistol_muzzle_flash_image"] = image_data('./res/muzzle_flash_pistol.png')
    world.images["SMG_muzzle_flash_image"] = image_data('./res/muzzle_flash_SMG.png')
    world.images["AssaultR_muzzle_flash_image"] = image_data('./res/muzzle_flash_AR.png')
    world.images["SniperR_muzzle_flash_image"] = image_data('./res/muzzle_flash_SR.png')
    world.images["LaserR_muzzle_flash_image"] = image_data('./res/muzzle_flash_LR.png')
    world.images["GaussR_muzzle_flash_image"] = image_data('./res/muzzle_flash_GR.png')
    world.images["MiniGun_muzzle_flash_image"] = image_data('./res/muzzle_flash_MG.png')
    
    
    
    world.images["ammo_image"] = image_data('./res/ammo.png')
    world.images["medkit_image"] = image_data('./res/medkit.png')
    world.images["petrol_image"] = image_data('./res/petrol.png')
    world.images["grenade_concussion_image"] = image_data('./res/grenade_concussion.png')
    world.images["grenade_molotov_image"] = image_data('./res/grenade_molotov.png')
    world.images["grenade_incendiary_image"] = image_data('./res/grenade_incendiary.png')
    
    world.images["Pistol_image"] = image_data('./res/pistol.png')
    world.images["SMG_image"] = image_data('./res/smg.png')
    world.images["AssaultR_image"] = image_data('./res/assaultr.png')
    world.images["SniperR_image"] = image_data('./res/sniperr.png')
    world.images["LaserR_image"] = image_data('./res/laserr.png')
    world.images["GaussR_image"] = image_data('./res/GaussR.png')
    world.images["MiniGun_image"] = image_data('./res/MiniGun.png')
    
    
    world.images["pickup_Pistol_image"] = image_data('./res/pickup_pistol.png')
    world.images["pickup_SMG_image"] = image_data('./res/pickup_SMG.png')
    world.images["pickup_AssaultR_image"] = image_data('./res/pickup_AR.png')
    world.images["pickup_SniperR_image"] = image_data('./res/pickup_SR.png')
    world.images["pickup_LaserR_image"] = image_data('./res/pickup_LR.png')
    world.images["pickup_GaussR_image"] = image_data('./res/pickup_GR.png')
    world.images["pickup_MiniGun_image"] = image_data('./res/pickup_MG.png')
    
    world.images["Shotgun_image"] = image_data('./res/shotgun.png')
    world.images["pickup_Shotgun_image"] = image_data('./res/pickup_SG.png')
    world.images["Shotgun_muzzle_flash_image"] = image_data('./res/muzzle_flash_SMG.png')
    world.images["Shotgun_bullet_image"] = world.images["bullet_image"]
    world.images["mem_Shotgun_image"] = world.images["pickup_Shotgun_image"]
    
    
    world.images["shadow_image"] = image_data('./res/shadow.png')
    world.images["fog_image"] = image_data('./res/fog.png')
    world.images["cursor_image"] = image_data('./res/cursor.png')

    
    world.images["mem_concrete_block_image"] = world.images["concrete_block_image"] 
    world.images["mem_shrub_image"] = world.images["shrub_image"] 
    world.images["mem_remains_image"] = world.images["remains_image"] 
    world.images["mem_zombie_massive_corpse_image"] = world.images["zombie_massive_corpse_image"] 
    world.images["mem_zombie_normal_corpse_image"] = world.images["zombie_normal_corpse_image"] 
    world.images["mem_zombie_fast_corpse_image"] = world.images["zombie_fast_corpse_image"] 
        
    world.images["mem_zombie_hive_image"] = world.images["zombie_hive_image"]
    
    world.images["mem_medkit_image"] = world.images["medkit_image"] 
    world.images["mem_ammo_image"] = world.images["ammo_image"] 
    world.images["mem_petrol_image"] = world.images["petrol_image"]
    
    world.images["mem_Pistol_image"] = world.images["pickup_Pistol_image"] 
    world.images["mem_SMG_image"] = world.images["pickup_SMG_image"] 
    world.images["mem_AssaultR_image"] = world.images["pickup_AssaultR_image"] 
    world.images["mem_SniperR_image"] = world.images["pickup_SniperR_image"] 
    world.images["mem_LaserR_image"] = world.images["pickup_LaserR_image"] 
    world.images["mem_GaussR_image"] = world.images["pickup_GaussR_image"] 
    world.images["mem_MiniGun_image"] = world.images["pickup_MiniGun_image"] 


    
    """ we set up the background to fit the game area """
    world.images["background_image"] = image_data('./res/grass.png')
    world.images["background_image"].orig_image = pygame.transform.scale(world.images["background_image"].orig_image, (world.dimensions[0]*constants.WORLD_PPM,world.dimensions[1]*constants.WORLD_PPM))
    world.images["background_image"].orig_rect = world.images["background_image"].orig_image.get_rect()
