#NeoPeewee Pygame Engine (January 4, 2020)

import pygame

#Import Global Memory
from Scripts import Global

class Shop:
    def __init__(this):
        #Main Variables
        this.state = 0
        this.doControls = 1
        this.doGraphics = 1
        this.alarm = [0, 0, 0, 0, 0]
        #Class Variables
        this.select = 0
        this.orders = 0
        this.complete = 0
        this.text = []

    #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:
                    #Disable the state and activate the map
                    if i == 0:
                        this.state = 0
                        this.doControls = 1
                        Global.map.state = 1
                        Global.audio.ReplaceMusic('Sounds/Music/Town_Theme_Busy.mp3')
                    #Enable state 1
                    if i == 1:
                        this.state = 1
                        this.doControls = 1
                    #Manage Text
                    if i == 2:
                        pass
                    #Cutscene Transition
                    if i == 3:
                        this.alarm[2] = 4
                    #Start Cutscene
                    if i == 4:
                        this.state = 3
                        this.doControls = 1

        #Do Controls
        if this.doControls == 1:
            if uppress == 1 and this.state == 1:
                this.select = 0
            if downpress == 1 and this.state == 1:
                this.select = 1
            if leftpress == 1 and this.state == 2 and this.select > 0:
                this.select -= 1
            if rightpress == 1 and this.state == 2 and this.select < len(Global.inventory.orders)-1:
                this.select += 1
            if zpress == 1:
                if this.state == 1:
                    if this.select == 0 and len(Global.inventory.orders) > 0:
                        this.state = 2
                    else:
                        this.doControls = 0
                        this.alarm[0] = 10
                        Global.DoTransition(Global, 10)
                elif this.state == 2 and this.CheckComplete():
                    Global.DoTransition(Global, 10)
                    this.alarm[4] = 10
                    this.text = Global.inventory.orders[this.select].complete+this.NextOrder()
                    this.doControls = 0
                elif this.state == 3 and this.alarm[2] == 0 and this.alarm[3] == 0:
                    this.CheckText()

            if xpress == 1 and this.state == 2:
                this.state = 1
                this.select = 0
            if xpress > 0 and this.state == 3 and this.alarm[2] == 0 and this.alarm[3] == 0:
                this.CheckText()

        #Do Graphics
        if this.doGraphics == 1:
            Global.screen.blit(Global.graphics.shop, (0, 0))
            if this.state < 3:
                Global.screen.blit(Global.graphics.lizardstail, (6, 206))
                Global.TextDisplay(Global, 1, this.GetColour(this.CheckLizardstail()), 44, 216, 'x'+str(Global.inventory.lizardstail))
                Global.screen.blit(Global.graphics.gryphonegg, (70, 206))
                Global.TextDisplay(Global, 1, this.GetColour(this.CheckGryphonegg()), 108, 216, 'x'+str(Global.inventory.gryphonegg))
                Global.screen.blit(Global.graphics.spineflowerberry, (134, 206))
                Global.TextDisplay(Global, 1, this.GetColour(this.CheckSpineflowerberry()), 172, 216, 'x'+str(Global.inventory.spineflowerberry))
                Global.screen.blit(Global.graphics.bonemeal, (198, 206))
                Global.TextDisplay(Global, 1, this.GetColour(this.CheckBonemeal()), 236, 216, 'x'+str(Global.inventory.bonemeal))
                Global.screen.blit(Global.graphics.makishroom, (262, 206))
                Global.TextDisplay(Global, 1, this.GetColour(this.CheckMakishroom()), 300, 216, 'x'+str(Global.inventory.makishroom))
                Global.screen.blit(Global.graphics.mermaidscale, (326, 206))
                Global.TextDisplay(Global, 1, this.GetColour(this.CheckMermaidscale()), 364, 216, 'x'+str(Global.inventory.mermaidscale))
            if this.state == 1:
                Global.screen.blit(Global.graphics.tag, (0, 16))
                Global.screen.blit(Global.graphics.tag, (0, 64))
                Global.TextDisplay(Global, 2, (0, 0, 0), 32, 24, 'View Orders')
                Global.TextDisplay(Global, 2, (0, 0, 0), 32, 72, 'Go to Map')
                Global.screen.blit(Global.graphics.select, (8, 24+(this.select*48)))
            if this.state == 2:
                Global.screen.blit(Global.graphics.largescroll, (104, 4))
                Global.TextDisplay(Global, 2, (0, 0, 0), 112, 32, Global.inventory.orders[this.select].name)
                Global.screen.blit(Global.graphics.lizardstail, (112, 56))
                Global.TextDisplay(Global, 1, (0, 0, 0), 150, 66, 'x'+str(Global.inventory.orders[this.select].lizardstail))
                Global.screen.blit(Global.graphics.gryphonegg, (112, 104))
                Global.TextDisplay(Global, 1, (0, 0, 0), 150, 114, 'x'+str(Global.inventory.orders[this.select].gryphonegg))
                Global.screen.blit(Global.graphics.spineflowerberry, (112, 152))
                Global.TextDisplay(Global, 1, (0, 0, 0), 150, 162, 'x'+str(Global.inventory.orders[this.select].spineflowerberry))
                Global.screen.blit(Global.graphics.bonemeal, (202, 56))
                Global.TextDisplay(Global, 1, (0, 0, 0), 240, 66, 'x'+str(Global.inventory.orders[this.select].bonemeal))
                Global.screen.blit(Global.graphics.makishroom, (202, 104))
                Global.TextDisplay(Global, 1, (0, 0, 0), 240, 114, 'x'+str(Global.inventory.orders[this.select].makishroom))
                Global.screen.blit(Global.graphics.mermaidscale, (202, 152))
                Global.TextDisplay(Global, 1, (0, 0, 0), 240, 162, 'x'+str(Global.inventory.orders[this.select].mermaidscale))
                if this.select > 0:
                    Global.screen.blit(Global.graphics.smallscroll, (60, 80))
                if this.select < len(Global.inventory.orders)-1:
                    Global.screen.blit(Global.graphics.smallscroll, (328, 80))
            if this.state == 3 and this.alarm[3] == 0 and len(this.text) > 0:
                Global.TextDisplay(Global, 1, (0+(this.alarm[2]*20), 0+(this.alarm[2]*20), 0+(this.alarm[2]*20)), 3, 206-this.alarm[2], this.text[0].split('#')[0])
                if len(this.text[0].split('#')) > 1:
                    Global.TextDisplay(Global, 1, (0+(this.alarm[2]*50), 0+(this.alarm[2]*50), 0+(this.alarm[2]*50)), 3, 222-this.alarm[2], this.text[0].split('#')[1])

    def GetColour(this, i):
        if this.state == 1:
            return (0, 0, 0)
        elif i:
            return (0, 155, 0)
        else:
            return (155, 0, 0)

    def CheckLizardstail(this):
        if this.state == 2:
            if Global.inventory.lizardstail >= Global.inventory.orders[this.select].lizardstail:
                return True
            else:
                return False
        else:
            return True

    def CheckGryphonegg(this):
        if this.state == 2:
            if Global.inventory.gryphonegg >= Global.inventory.orders[this.select].gryphonegg:
                return True
            else:
                return False
        else:
            return True

    def CheckSpineflowerberry(this):
        if this.state == 2:
            if Global.inventory.spineflowerberry >= Global.inventory.orders[this.select].spineflowerberry:
                return True
            else:
                return False
        else:
            return True

    def CheckBonemeal(this):
        if this.state == 2:
            if Global.inventory.bonemeal >= Global.inventory.orders[this.select].bonemeal:
                return True
            else:
                return False
        else:
            return True

    def CheckMakishroom(this):
        if this.state == 2:
            if Global.inventory.makishroom >= Global.inventory.orders[this.select].makishroom:
                return True
            else:
                return False
        else:
            return True

    def CheckMermaidscale(this):
        if this.state == 2:
            if Global.inventory.mermaidscale >= Global.inventory.orders[this.select].mermaidscale:
                return True
            else:
                return False
        else:
            return True

    def CheckComplete(this):
        if this.CheckLizardstail() and this.CheckGryphonegg() and this.CheckSpineflowerberry() and this.CheckBonemeal() and this.CheckMakishroom() and this.CheckMermaidscale():
            return True
        else:
            return False

    def CheckText(this):
        del(this.text[0])
        if len(this.text) == 0:
            Global.DoTransition(Global, 10)
            this.doControls = 0
            this.alarm[1] = 10
        else:
            if this.text[0] == '(TRANSITION)':
                del(this.text[0])
                this.alarm[3] = 40
                Global.DoTransition(Global, 30)
            elif this.text[0] == '(ORDER)':
                this.orders += 1
                Global.inventory.NewOrder(this.orders)
                this.CheckText()
            elif this.text[0] == '(COMPLETE)':
                Global.inventory.lizardstail -= Global.inventory.orders[this.select].lizardstail
                Global.inventory.gryphonegg -= Global.inventory.orders[this.select].gryphonegg
                Global.inventory.spineflowerberry -= Global.inventory.orders[this.select].spineflowerberry
                Global.inventory.bonemeal -= Global.inventory.orders[this.select].bonemeal
                Global.inventory.makishroom -= Global.inventory.orders[this.select].makishroom
                Global.inventory.mermaidscale -= Global.inventory.orders[this.select].mermaidscale
                del(Global.inventory.orders[this.select])
                del(this.text[0])
                this.alarm[3] = 40
                Global.DoTransition(Global, 30)
            else:
                this.alarm[2] = 4

    def NextOrder(this):
        this.complete += 1
        if this.complete == 1:
            return ['*A woman in a cloack creeps into the shop.*',
                    'Welcome to ALCHEMY! How can I be of service today?',
                    '*The woman removed her cloak, revealing a fragile old lady. She#drops a broken photo frame with a worn out photo inside of it.*',
                    'Aldith: Please listen to my woes!',
                    'Aldith: This frame of my late husband was all I had left of him! Alas it#was ruined by a fire...',
                    'Aldith: If there is anything your magic can do, I beg that you help me!',
                    '*Hmmm... This is a little beyond me, but I think a mixture of Bonemeal#and Maki Shroom might just do the trick here...*',
                    'Please leave your frame with me, and I will try my best to revitalise#the photo.',
                    'Aldith: Thank you alchemist, please take good care of whats left of#my husband...',
                    '(ORDER)']
        if this.complete == 2:
            return ['*A young boy frantically comes into the shop.*',
                    'Elnaril: Please help me! My poor brother is now a frog!',
                    'A frog? How did that happen?',
                    'Elnaril: He was licking frogs in the garden, and -',
                    'Why was he licking frogs?',
                    'Elnaril: You have to help me!',
                    '....',
                    '...Okay.',
                    'Turning into a frog is a symptom of consuming the oils of the#Laevigata strain. I can have a potion prepared soon.',
                    'For the meantime, keep your brother in a warm, damp environment#with a bowl of water and crickets on hand.',
                    'Elnaril: Thank you, Alchemist!',
                    '(ORDER)',
                    '(TRANSITION)',
                    '*An uneasy aura fills the shop. The change of energy is palpable.#Suddenly, a woman in magician’s robes appears in a puff of smoke.*',
                    'Phynara: Greetings, Alchemist.',
                    'H-How can I help you, Magician?',
                    '*Phynara looks up and down at the magical textbooks on display*',
                    'Phynara: You seem competent enough. After all, only one in five#alchemical apprentices continue to become full fledged Alchemists.',
                    'Ah, yes, I do not doubt that, most apprenticeships are grueling... Is#there anything I can help you with?',
                    'Phynara: No, I doubt it – I’m rather certain that my alchemical#knowledge surpasses yours.',
                    '...Okay.',
                    'Phynara: However, I require a specific ingredient for my own…#experiments, that I cannot procure on my own.',
                    'Bonemeal of an ancient familiar? That’s a potent material.',
                    'Phynara: Quite so. Do you have it or not?',
                    'No, not at the moment, but it’s not out of the question that I could get#some. The forests surrounding this village are old and magical.',
                    'Phynara: All Alchemists are competent spellcasters too, or at least#should be – summon me when you have some bonemeal in.',
                    '(ORDER)']
        if this.complete == 3:
            return ['*A man walks into the store, browsing the shelves with curiosity.*',
                    'Tanithil: I’ve passed through this village many times during my#travels.',
                    'Tanithil: It’s been quite some time since I’ve seen this storefront#occupied by an Alchemist.',
                    'I have only set up shop recently. Still cleaning this old place#up.',
                    'Is there anything I can help you with?',
                    'Tanithil: Ah, perhaps, now that I think of it...',
                    'Tanithil: I’m getting to be an old man, and as a natural nomad, my#lifestyle is beginning to get harder to maintain in my old age.',
                    'Tanithil: My joints ache as I walk.',
                    'I could prepare a soothing balm for you, should you be in town for#awhile more.',
                    'Tanithil: I shall be resting at the village inn for a few more days#before continuing on.',
                    'Tanithil: Send a message to the inn once it’s ready.',
                    'That I can do.',
                    '(ORDER)']
        if this.complete == 4:
            return ['*The door swings open as an archer walks in.*',
                    'Welcome to ALCHEMY, may I be of business?',
                    'Rowan: Yves the Kind spoke very highly of you.',
                    '*Sounds like the work I did for one of the knights is spreading#through the town...*',
                    'Yes, I was able to repair the enchantment lost from his sword.',
                    'Rowan: Well I have fallen to a similar circumstance...',
                    '*Rowan pulls out a broken Bow from his backpack.*',
                    'Rowan: This was my finest bow, well aged however and snapped just#yesterday.',
                    'Rowan: I simply cannot afford another bow of that quality, and#wondered if you could repair it cheaper?',
                    '*This will be a touch more difficult than simply repairing the#enchantment...*',
                    '*But the same blend of Mermaid Scales and Spine Flower Berries#should return the enchantment.*',
                    '*Bonemeal and Maki Shrooms should repair the bow itself.*',
                    'I think I know something that will repair your bow. Give me some time#and I will summon you.',
                    '(ORDER)',
                    '(TRANSITION)',
                    '*A woman swiftly enters the store, closing the door behind her.*',
                    '*Hmph, business seems to be picking up swiftly...*',
                    'Elora: Alchemist, do you by chance have any Spine Flower#Berries?',
                    'I need to check my stock, but I know I do have plenty of similar#alternatives I can spare.',
                    'Elora: No, no, only pure Spine Flower Berries will do.',
                    'Elora: Anything less, and I’m not sure if the compound will alter#during the baking process, ruining my efforts.',
                    'Baking process? What are you trying to make?',
                    'Elora: A pie for my meeting with Councilor Syndra when she visits#the village next week!',
                    'Elora: If I understand the attributes of the Spine Flower Berry#correctly, it will make her most agreeable.',
                    'Then she’ll agree to providing the funding for my theater project!',
                    'Well, as an Alchemist, I do support the arts...',
                    'And I’m not in the position to make moral judgements on how ethical#your plan is.',
                    'If you can provide the coin, I can provide the Spine Flower Berries.',
                    'Elora: Thank you, Alchemist!',
                    '(ORDER)']
        if this.complete > 4:
            return ['Another order complete!',
                    'There seems to be no more orders right now...',
                    'Hopefully this gives me time to focus on the orders I already have...']
