import logging
import pygame
from random import randint
import gameUtils

#Oh god here we go. I'm gonna screw up this entire thing

class Game(gameUtils.Game):
    def __init__(self, *args, **kwargs):
        """
        Ran when the game starts.

        You have access to the following variables:
            self.surf (pygame.Surface): This is the surface you draw onto.
            self.pm (NoPELib.PlayerManager): This is where your players are stored.
            self.cfg (dict): Everything from the `game.toml` file. You can access it like this: self.details['title']
        """
        
        # Don't remove this. It does important things. :3
        super().__init__(*args, **kwargs)

        # TODO: Utilise self.pm (the player manager)
        # TODO: Add background music
        # TODO: Add muzzle flash
        # TODO: Add animations for blank firing and real firing

        # TODO: Utilise the following new sounds:
        # gun-load: Should be played at the beginning of the game,
        #           and should have an animation accompanying it.
        #           see Brosef for the animation.

        #Sounds for the gun :3
        self.playSound = pygame.mixer.Sound.play
        self.gunBlank = pygame.mixer.Sound("./assets/gun-blank.mp3")
        self.gunShot = pygame.mixer.Sound("./assets/gun-shot.mp3")
        self.gunSpin = pygame.mixer.Sound("./assets/gun-spin.mp3")
        self.gunLoad = pygame.mixer.Sound("./assets/gun-load.mp3")
        self.gunCyclinderStop = pygame.mixer.Sound("./assets/gun-cylinder-stop.mp3")
        
        #self.gun = pygame.load_animation('./assets/gun-spin.webp')
        self.gunFlash = pygame.image.load("./assets/muzzleflash.png")

        self.gun = self.createAnimObj('gun', './assets/gun-base.png')
        self.gun.addAnimation(gameUtils.AnimationHandler('spin', './assets/gun-spin.webp', 60))

        self.shockScale = 0 #controls shock level sent to PDO (on a scale of 0 to 1)
    
        #render variables
        self.gunw=self.gun.size[0]    #calculates width of gun anim
        self.gunh=self.gun.size[1]    #calculates height of gun anim
                                            #NOTE: Brosef, istg you better not make the gun webm
                                            #       change sizes mid file, or so help me
                                            #
                                            #      :3
                                            #         - Brosef

        self.screenMidx = self.size[0]/2    #calculates horizontal midpoint of screen
        self.screenMidy = self.size[1]/2    #calculates verticle midpoint of screen

        self.gunScale = min(self.size[1]/self.gunh,self.size[0]/self.gunw)  #calculates how to scale the gun so that it stays on the screen

    def fire(self):#LETS GO GAMBLING
        if randint(1,6) == 6:
            self.playSound(self.gunShot)
            return 0.04
            #remember to add shock
        else:
            self.playSound(self.gunBlank)
            return 0

    def onEvent(self, event):
        """
        Ran when an event is fired.

        Args:
            event (pygame.Event): The event that was fired.
        """

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1:
                self.playSound(self.gunBlank)
            elif event.key == pygame.K_2:
                self.playSound(self.gunShot)
            elif event.key == pygame.K_3:
                self.playSound(self.gunSpin)
            elif event.key == pygame.K_SPACE and not self.awaitingTimeout("FireDelay"):
                self.gun.playAnim('spin')
        elif event.type == gameUtils.AnimStart:
            if event.objectID == self.gun.objectID and event.animationID == 'spin':
                self.playSound(self.gunSpin)
        elif event.type == gameUtils.AnimFinish:
            if event.objectID == self.gun.objectID and event.animationID == 'spin':
                self.timeout("FireDelay",randint(750,2000)/1000)
                self.playSound(self.gunCyclinderStop)
                
        elif event.type == gameUtils.Timeout:
            self.shockScale += self.fire()            #check if the user gets shot
            self.shockScale = min(self.shockScale, 1) #keeps shockScale in bounds
    
    def update(self):
        """
        Ran once per frame, put your drawing code and any
        game logic that should be ran once per frame in here.
        """
        
        # Don't remove this. It does important things. :3
        super().update()

        #screen code
        self.surf.fill("black")
        self.surf.blit(gameUtils.centre(pygame.transform.scale_by(self.gun.getFrame(), self.gunScale), self.size))

    def close(self):
        """
        Ran when the game closes
        """
        pass