#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from libstormgames.libstormgames import *

# Initial variable settings
mode = "menu"
sounds = initialize_gui("Monkey Spank")
pygame.event.set_allowed(pygame.KEYDOWN)
pygame.mixer.set_reserved(1)
pygame.mixer.set_reserved(2)


def butthead_taunt(sounds):
    global bonus
    global points
    play_random(sounds, "butt-head")
    bonus = True

def monkey_taunt():
    t = pygame.mixer.Channel(1)
    left = 1.0
    right = 1.0
    taunt = random.randrange(1, 4)
    if taunt == 1: right = 0.0
    if taunt == 3: left = 0.0
    t.play(sounds['monkey-taunt'])
    t.set_volume(left, right)
    return taunt

def player_action(monkey, reaction):
    if monkey != reaction: return True
    global points
    global spanks
    left = 0.7
    right = 0.7
    if reaction == 1: right = 0.0
    if reaction == 3: left = 0.0
    p = pygame.mixer.Channel(2)
    p.play(sounds['spank-hit'])
    p.set_volume(left, right)
    points += 10
    spanks += 1
    return False
    
    
def game(mode):
    pygame.mixer.music.pause()
    global bonus
    bonusTime = int(time.time())
    encourage = int(time.time())
    lose = False
    cut_scene(sounds, 'start-game')
    startTime = time.time()
    totalTime = startTime
    global points
    global spanks
    taunt = monkey_taunt()
    timeOut = time.time()
    while lose == False:
        if spanks <= 10 and time.time() - timeOut > 3:
            lose = True
            continue
        elif spanks > 10 and spanks <= 20 and time.time() - timeOut > 2:
            lose = True
            continue
        elif spanks > 20 and time.time() - timeOut > 1:
            lose = True
            continue
        event = pygame.event.poll()
        if event.type == pygame.KEYDOWN:
            # Escape is the back/exit key, close the game if not playing, or return to menu if playing.
            if event.key == pygame.K_ESCAPE:
                if mode != "menu":
                    mode = "menu"
                    return mode
                elif mode == "menu": exit_game()
            elif event.key == pygame.K_LEFT:
                lose = player_action(taunt, 1)
                if lose == True: continue
                taunt = monkey_taunt()
                timeOut = time.time()
            elif event.key == pygame.K_DOWN:
                lose = player_action(taunt, 2)
                if lose == True: continue
                taunt = monkey_taunt()
                timeOut = time.time()
            elif event.key == pygame.K_UP:
                lose = player_action(taunt, 2)
                if lose == True: continue
                taunt = monkey_taunt()
                timeOut = time.time()
            elif event.key == pygame.K_RIGHT:
                lose = player_action(taunt, 3)
                if lose == True: continue
                taunt = monkey_taunt()
                timeOut = time.time()
            elif (event.key == [pygame.K_SPACE) and (bonus == True):
                bonus == False
                points += 100
                k = pygame.mixer.Channel(0)
                k.play(sounds['kick-hit'])
            pygame.event.clear()
        if bonusTime - time.time() > 0.75:
            bonus = False
        bonusTime = time.time()
        totalTime = time.time()
        if int(time.time()) - encourage  == 8:
            if random.randrange(0, 5) < 2:
                play_random(sounds, "encourage")
            else:
                butthead_taunt(sounds)
            encourage = int(time.time())
        pygame.event.pump()
        time.sleep(0.001)
    pygame.mixer.stop()
    time.sleep(0.1)
    pygame.event.clear()
    play_random(sounds, "lose", interrupt = True)
    message = ["You spanked the monkey " + str(spanks) + " "]
    if spanks == 1: message[0] += "time."
    if spanks != 1: message[0] += "times."
    message.append("Your total time was " + str(round(totalTime - startTime, 2)) + " seconds.")
    try:
        message.append("You averaged " + str(round(spanks / (totalTime - startTime), 2)) + " monkey spanks per second.")
    except:
        message.append("You averaged 0.00 monkey spanks per second.")
    message.append("You scored " + str(points) + " points.")
    time.sleep(0.1)
    display_text(message)
    return "menu"

# Game starts at main menu
mode = game_menu("start game", "instructions", "donate", "credits", "learn sounds", "exit_game")
while mode != "exit_game":
    bonus = False
    spanks = 0
    points = 0
    if mode == "menu": mode = game_menu("start game", "instructions", "donate", "credits", "learn sounds", "exit_game")
    if mode == "start game": mode = game(mode)
    if mode == "learn sounds": mode = learn_sounds(sounds)
    time.sleep(0.001)

