#NeoPeewee Pygame Engine (January 4, 2020)

import pygame

#Import Global Memory
from Scripts import Global

class Spineflowerberry:
    def __init__(this):
        #Main Variables
        this.state = 1
        this.doControls = 1
        this.doGraphics = 1
        this.alarm = [0, 0, 0, 0, 0]
        #Class Variables
        this.text = ['There are plenty of Spine Flower Berries in this field!',
                     'Unfortunately, the branches they grow on are full of thorns...',
                     'Complete the string of inputs and you get berries, Simple!',
                     'Press inputs too slow, and the game ends with the berries you have.',
                     'Press the wrong arrow key, and you get a face full of thorns,#dropping all your berries!']
        this.direction = [Global.Random(0, 3), Global.Random(0, 3), Global.Random(0, 3)]
        this.combo = 0
        this.time = 90
        this.berries = 0
        this.falling = []
        Global.audio.ReplaceMusic('Sounds/Music/The_Animal_Den.mp3')

    #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
                    if i == 1:
                        this.state = 2
                        this.alarm[2] = this.time
                    #Input Time
                    if i == 2:
                        this.state = 3
                        this.alarm[0] = 90
                    #Return to map
                    if i == 3:
                        this.state = 0
                        Global.map.state = 1
                        Global.inventory.spineflowerberry += this.berries
                        Global.audio.ReplaceMusic('Sounds/Music/Town_Theme_Busy.mp3')
                    #Falling
                    if i == 4:
                        this.falling = []

        #Do Controls
        if this.doControls == 1:
            for i in range(len(this.falling)):
                this.falling[i][1] += 4
            if leftpress == 1 and this.state == 2 and this.alarm[1] == 0:
                if this.direction[0] == 3:
                    this.CheckString()
                else:
                    this.MisInput()
            if rightpress == 1 and this.state == 2 and this.alarm[1] == 0:
                if this.direction[0] == 1:
                    this.CheckString()
                else:
                    this.MisInput()
            if uppress == 1 and this.state == 2 and this.alarm[1] == 0:
                if this.direction[0] == 0:
                    this.CheckString()
                else:
                    this.MisInput()
            if downpress == 1 and this.state == 2 and this.alarm[1] == 0:
                if this.direction[0] == 2:
                    this.CheckString()
                else:
                    this.MisInput()
            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
                if this.state > 2:
                    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

        #Do Graphics
        if this.doGraphics == 1:
            Global.screen.blit(Global.graphics.berries, (0, 0))
            if this.state == 1:
                Global.screen.blit(Global.graphics.thorns, (0, 0))
                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 == 2:
                if this.alarm[1] > 0:
                    Global.TextDisplay(Global, 2, (0, 0, 0), 3, 206, 'Plucked '+str(this.combo)+' Spine Flower Berries!')
                else:
                    Global.screen.blit(Global.graphics.thorns, (0, 0))
                    Global.screen.blit(Global.graphics.arrow_view, (24, 24))
                    for i in range(len(this.direction)):
                        if this.direction[i] == 0:
                            Global.screen.blit(Global.graphics.arrow_up, (32+(i*48), 32))
                        if this.direction[i] == 1:
                            Global.screen.blit(Global.graphics.arrow_right, (32+(i*48), 32))
                        if this.direction[i] == 2:
                            Global.screen.blit(Global.graphics.arrow_down, (32+(i*48), 32))
                        if this.direction[i] == 3:
                            Global.screen.blit(Global.graphics.arrow_left, (32+(i*48), 32))
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Spine Flower Berries: '+str(this.berries))
                for i in range(len(this.falling)):
                    if this.falling[i][1] < 170:
                        Global.screen.blit(Global.graphics.spineflowerberry, (this.falling[i][0], this.falling[i][1]))
            if this.state == 3:
                Global.screen.blit(Global.graphics.thorns, (0, 0))
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Maybe now is a good time to take the berries you have back...')
                if this.alarm[0] == 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to return to the map...')
            if this.state == 4:
                Global.screen.blit(Global.graphics.thorns, (0, 0))
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Ouch! Thorns hit your face, and you drop all your berries!')
                if this.alarm[0] == 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to return to the map...')

    def NextInput(this):
        this.combo += 1
        this.berries += this.combo
        for i in range(this.combo):
            this.falling.append([Global.Random(80, 320), Global.Random(64, 144)])
            this.alarm[4] = 60
        this.direction = [0]*(this.combo+3)
        for i in range(len(this.direction)):
            this.direction[i] = Global.Random(0, 3)
        this.time += 7
        this.alarm[1] = 30
        this.alarm[2] = 0

    def MisInput(this):
        this.state = 4
        this.berries = 0
        this.alarm[0] = 90
        this.alarm[2] = 0

    def CheckString(this):
        del(this.direction[0])
        if len(this.direction) == 0:
            this.NextInput()

    def Debugging(this, i):
        if i == 0:
            return 'up'
        if i == 1:
            return 'right'
        if i == 2:
            return 'down'
        if i == 3:
            return 'left'
