#NeoPeewee Pygame Engine (January 4, 2020)

import pygame

#Import Global Memory
from Scripts import Global

class Mermaidscale:
    def __init__(this):
        #Main Variables
        this.state = 1
        this.doControls = 1
        this.doGraphics = 1
        this.alarm = [0, 0, 0, 0]
        #Class Variables
        this.text = ['The Mermaids here want to play a game!',
                     'Listen closely, as the 3 of them will sing a song together...',
                     'One Mermaid will be singing a different tune, so pay close attention#to which direction the different song is coming from.',
                     'For each consecutive correct guess, they will provide you with more#and more Mermaid Scales.',
                     'You can leave whenever, but guess incorrectly once and you lose#every scale you earned!']
        this.select = 0
        this.correcct = 0
        this.scales = 0
        Global.audio.EndMusic('easter egg!')

    #Main Control Method
    def Control(this, leftpress, rightpress, uppress, downpress, zpress, xpress):
        #Manage Alarms
        for i in range(len(this.alarm)):
            if this.alarm[i] > 0:
                this.alarm[i] -= 1
                if this.alarm[i] == 0:
                    #Text Control
                    if i == 0:
                        pass
                    #Start Game/Check for Game End
                    if i == 1:
                        this.state = 2
                        this.alarm[2] = 240
                        this.PlaySong()
                    #Start Choice
                    if i == 2:
                        this.state = 3
                        this.select = 1
                    #Continue
                    if i == 3:
                        this.state = 0
                        Global.map.state = 1
                        Global.inventory.mermaidscale += this.scales
                        Global.audio.ReplaceMusic('Sounds/Music/Town_Theme_Busy.mp3')

        #Do Controls
        if this.doControls == 1:
            if leftpress == 1 and this.state == 3 and this.alarm[2] == 0 and this.select > 0:
                this.select -= 1
            if rightpress == 1 and this.state == 3 and this.alarm[2] == 0 and this.select < 2:
                this.select += 1
            if zpress == 1:
                if this.state == 1 and this.alarm[0] == 0 and len(this.text) > 0:
                    del(this.text[0])
                    if len(this.text) == 0:
                        this.alarm[1] = 50
                    else:
                        this.alarm[0] = 4
                elif this.state == 3 and this.alarm[2] == 0:
                    if this.select == this.correct:
                        this.state = 4
                        if this.scales == 0:
                            this.scales = 1
                        else:
                            this.scales *= 2
                    else:
                        this.state = 5
                        this.scales = 0
                        this.alarm[0] = 90
                elif this.state == 4:
                    this.state = 1
                    this.alarm[1] = 50
                elif this.state == 5 and this.alarm[0] == 0:
                    Global.DoTransition(Global, 10)
                    this.doControls = 0
                    this.alarm[3] = 10
            if xpress > 0 and this.state == 1 and this.alarm[0] == 0 and len(this.text) > 0:
                del(this.text[0])
                if len(this.text) == 0:
                    this.alarm[1] = 50
                else:
                    this.alarm[0] = 4
            if xpress == 1 and this.state == 4:
                Global.DoTransition(Global, 10)
                this.doControls = 0
                this.alarm[3] = 10

        #Do Graphics
        if this.doGraphics == 1:
            Global.screen.blit(Global.graphics.mermaids, (0, 0))
            if this.state == 1:
                if len(this.text) > 0:
                    Global.TextDisplay(Global, 1, (0+(this.alarm[0]*20), 0+(this.alarm[0]*20), 0+(this.alarm[0]*20)), 3, 206-this.alarm[0], this.text[0].split('#')[0])
                    if len(this.text[0].split('#')) > 1:
                        Global.TextDisplay(Global, 1, (0+(this.alarm[0]*50), 0+(this.alarm[0]*50), 0+(this.alarm[0]*50)), 3, 222-this.alarm[0], this.text[0].split('#')[1])
                if this.alarm[1] > 20:
                    Global.TextDisplay(Global, 2, (0, 0, 0), 3, 206, 'Ready?')
                elif this.alarm[1] > 0:
                    Global.TextDisplay(Global, 2, (0, 0, 0), 3, 206, 'Go!')
            if this.state == 3:
                Global.screen.blit(Global.graphics.select, (145+(this.select*55), 130))
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Make your guess!')
            if this.state == 4:
                Global.screen.blit(Global.graphics.select, (145+(this.select*55), 130))
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Correct! Mermaid Scales: '+str(this.scales))
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to play again and double scales, Press X to return to Map.')
            if this.state == 5:
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Nope! Wrong Mermaid! You lost all your won Mermaid Scales.')
                if this.alarm[0] == 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to return to the map...')
    def PlaySong(this):
        this.correct = Global.Random(0, 2)
        if Global.Random(0, 1) == 0:
            if this.correct == 0:
                Global.audio.PlaySound(Global.audio.mermaid2l)
            else:
                Global.audio.PlaySound(Global.audio.mermaid1l)
            if this.correct == 1:
                Global.audio.PlaySound(Global.audio.mermaid2c)
            else:
                Global.audio.PlaySound(Global.audio.mermaid1c)
            if this.correct == 2:
                Global.audio.PlaySound(Global.audio.mermaid2r)
            else:
                Global.audio.PlaySound(Global.audio.mermaid1r)
        else:
            if this.correct == 0:
                Global.audio.PlaySound(Global.audio.mermaid1l)
            else:
                Global.audio.PlaySound(Global.audio.mermaid2l)
            if this.correct == 1:
                Global.audio.PlaySound(Global.audio.mermaid1c)
            else:
                Global.audio.PlaySound(Global.audio.mermaid2c)
            if this.correct == 2:
                Global.audio.PlaySound(Global.audio.mermaid1r)
            else:
                Global.audio.PlaySound(Global.audio.mermaid2r)
