#NeoPeewee Pygame Engine (January 4, 2020)

import pygame

#Import Global Memory
from Scripts import Global

class Bonemeal:
    def __init__(this):
        #Main Variables
        this.state = 1
        this.doControls = 1
        this.doGraphics = 1
        this.alarm = [0, 0, 0]
        #Class Variables
        this.text = ['These woods are very well known to be home to many fossils.',
                     'Try digging around to see if you can find some for bonemeal!',
                     'Use the arrow keys and Z to move and dig.',
                     'After a while of digging, the holes will cave back in.',
                     'So make sure you get as much bonemeal as you can before then!']
        this.holes = [1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1]
        this.CreateTreasure()
        this.CreateTreasure()
        this.CreateTreasure()
        this.location = 0
        this.bones = 0
        this.stable = 0
        Global.audio.ReplaceMusic('Sounds/Music/Through_The_Forest.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/Check for Game End
                    if i == 1:
                        this.state = 2
                        if this.stable >= 20:
                            this.state = 3
                            this.alarm[0] = 90
                    #Return to map
                    if i == 2:
                        this.state = 0
                        Global.map.state = 1
                        Global.inventory.bonemeal += this.bones
                        Global.audio.ReplaceMusic('Sounds/Music/Town_Theme_Busy.mp3')

        #Do Controls
        if this.doControls == 1:
            if leftpress == 1 and this.state == 2 and this.alarm[1] == 0:
                if this.location != 0 and this.location != 6 and this.location != 12 and this.location != 18 and this.location != 24 and this.location != 30:
                    this.location -= 1
            if rightpress == 1 and this.state == 2 and this.alarm[1] == 0:
                if this.location != 5 and this.location != 11 and this.location != 17 and this.location != 23 and this.location != 29 and this.location != 35:
                    this.location += 1
            if uppress == 1 and this.state == 2 and this.alarm[1] == 0:
                if this.location > 5:
                    this.location -= 6
            if downpress == 1 and this.state == 2 and this.alarm[1] == 0:
                if this.location < 30:
                    this.location += 6
            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 and this.holes[this.location] > 0 and this.alarm[1] == 0:
                    this.holes[this.location] *= -1
                    if this.holes[this.location] == -1:
                        this.stable += 1
                    if this.holes[this.location] == -2:
                        this.stable += 3
                        this.bones += 1
                    if this.holes[this.location] == -3:
                        this.stable += 1
                        this.bones += 3
                    this.alarm[1] = 30
                if this.state == 3 and this.alarm[0] == 0:
                    Global.DoTransition(Global, 10)
                    this.doControls = 0
                    this.alarm[2] = 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.woods, (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 == 2:
                for i in range(len(this.holes)):
                    if this.holes[i] > 0:
                        Global.screen.blit(Global.graphics.tile_dig, (152+((i%6)*16), 52+((i//6)*16)))
                    if this.holes[i] == -1:
                        Global.screen.blit(Global.graphics.tile_hole, (152+((i%6)*16), 52+((i//6)*16)))
                    if this.holes[i] == -2:
                        Global.screen.blit(Global.graphics.tile_bone, (152+((i%6)*16), 52+((i//6)*16)))
                    if this.holes[i] == -3:
                        Global.screen.blit(Global.graphics.tile_meal, (152+((i%6)*16), 52+((i//6)*16)))
                if this.alarm[1] > 0:
                    if this.holes[this.location] == -1:
                        Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Nothing buried here...')
                    if this.holes[this.location] == -2:
                        Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'You found part of a fossil, there might be Bonemeal nearby!')
                    if this.holes[this.location] == -3:
                        Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'You struck Bonemeal!')
                else:
                    Global.screen.blit(Global.graphics.select, (152+((this.location%6)*16), 52+((this.location//6)*16)))
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Bonemeal: '+str(this.bones))
            if this.state == 3:
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Woah! The area around you caved in!')
                if this.alarm[0] == 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to return to the map...')

    def CreateTreasure(this):
        treasure = Global.Random(0, 35)
        if this.holes[treasure] == 1:
            this.holes[treasure] = 3
            this.CreateBones(treasure-7)
            this.CreateBones(treasure-6)
            this.CreateBones(treasure-5)
            this.CreateBones(treasure-1)
            this.CreateBones(treasure+1)
            this.CreateBones(treasure+5)
            this.CreateBones(treasure+6)
            this.CreateBones(treasure+7)
        else:
            this.CreateTreasure()

    def CreateBones(this, i):
        if i >= 0 and i <= 35:
            if this.holes[i] == 1:
                this.holes[i] = 2
