#NeoPeewee Pygame Engine (January 4, 2020)

import pygame

#Import Global Memory
from Scripts import Global

class Audio:
    def __init__(this):
        #Main Variables
        this.doControls = 1
        this.alarm = 0
        #Class Variables
        pygame.mixer.init()
        this.music = 'Sounds/Music/Town_Theme_Busy.mp3'
        this.savedMusic = 0
        this.savedTime = 0
        #Sound Effects (use Global.audio.test.play() to call)
        this.mermaid1l = pygame.mixer.Sound('Sounds/Effects/mermaid1left.wav')
        this.mermaid1c = pygame.mixer.Sound('Sounds/Effects/mermaid1center.wav')
        this.mermaid1r = pygame.mixer.Sound('Sounds/Effects/mermaid1right.wav')
        this.mermaid2l = pygame.mixer.Sound('Sounds/Effects/mermaid2left.wav')
        this.mermaid2c = pygame.mixer.Sound('Sounds/Effects/mermaid2center.wav')
        this.mermaid2r = pygame.mixer.Sound('Sounds/Effects/mermaid2right.wav')

    #Main Control Method
    def Control(this):
        if this.doControls == 1:
            if this.alarm > 0:
                this.alarm -= 1
                if this.alarm == 0:
                    this.StartMusic(this.music)

    #Takes a new music and plays it
    def StartMusic(this, newMusic):
        if this.doControls > 0:
            this.music = newMusic
            pygame.mixer.music.load(this.music)
            pygame.mixer.music.play(-1)

    #Takes a new music and ends the current music if the two musics don't match
    def EndMusic(this, newMusic):
        if this.doControls > 0:
            if newMusic != this.music:
                pygame.mixer.music.fadeout(300)

    #Takes a new music and plays it if it is not the current music
    def ReplaceMusic(this, newMusic):
        if this.doControls > 0:
            if newMusic != this.music:
                this.EndMusic(newMusic)
                this.music = newMusic
                this.alarm = 20

    #Takes a provided sound and plays it
    def PlaySound(this, sound):
        if this.doControls > 0:
            sound.play()
