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)

        self.clock = pygame.time.Clock()

        # TODO: Add a random delay between the spin and fire

        # TODO: Utiles 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.
        # gun-cylinder-stop: Plays directly after the load
        #                    animation is complete.
        # gun-spin/blank/shot are all used in their original ways.

        #Sounds for the gun :3
        self.playSound = pygame.mixer.Sound.play
        self.gunBlank = pygame.mixer.Sound("sfx/gun-blank.mp3")
        self.gunShot = pygame.mixer.Sound("sfx/gun-shot.mp3")
        self.gunSpin = pygame.mixer.Sound("sfx/gun-spin.mp3")
        self.gunLoad = pygame.mixer.Sound("sfx/gun-load.mp3")
        self.gunCyclinder = pygame.mixer.Sound("sfx/gun-cylinder-stop.mp3")

        self.gun = pygame.image.load_animation("gun.webp")
        self.gunFlash = pygame.image.load("images/muzzleflash.png")

        self.shockScale = 0 #controls shock level sent to PDO (on a scale of 0 to 1)
        #gun variables
        self.startSpin = False
        self.spinInc = 0
    
        #render variables
        self.gunw=self.gun[0][0].size[0]    #calculates width of gun anim
        self.gunh=self.gun[0][0].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

        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

        self.gunMidx = self.screenMidx-(self.gunw*self.gunScale/2)          #gun midpoint on the x axis
        self.gunMidy = self.screenMidy-(self.gunh*self.gunScale/2)          #gun midpoint on the y axis

    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:
                self.startSpin = True
    
    def update(self):
        """
        Ran once per frame, put your drawing code and any
        game logic that should be ran once per frame in here.
        """
        
        #gun code WOO
        if self.startSpin == True:#check if space was pressed or if the gun is already spinning
            if self.spinInc == 0:
                self.playSound(self.gunSpin)
            self.spinInc += 1                             #next gun frame
            if self.spinInc == len(self.gun):             #if we've reached the end
                self.spinInc = 0                          #reset frame count    
                self.startSpin = False                    #what did i say bitch? stop spinning
                self.shockScale += self.fire()            #check if the user gets shot
                self.shockScale = min(self.shockScale, 1) #keeps shockScale in bounds

        #screen code
        self.surf.fill("black")
        self.surf.blit(pygame.transform.scale_by(self.gun[self.spinInc][0],self.gunScale),(self.gunMidx,self.gunMidy))#draw the gun
        #screen.blit(pygame.transform.scale_by(MainSurface,1),(0,yOffset))#renders the main surface to the screen, while also scaling it for the required display resolution

        # TODO:
        # I've removed the whole "main surface" thing for now,
        # because I'm not sure how we're going to deal with it
        # from now on right now. I'll fix this later when it's
        # a bit clearer on how NoPE-Lib will draw the players
        # and how the base game should draw that.
        #   - Brosef

        pygame.display.flip()

        self.clock.tick(60)

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