import pygame

class Bag:
    def __init__(self, player, ultimo):
        pygame.init()
        self.window = pygame.display.set_mode((720, 480))
        pygame.display.set_caption('Pokémon')

        self.assets = {
            "fonte":  pygame.font.Font("assets/batalha/fonte/fonte.ttf", 40),

            'background' : pygame.transform.scale(pygame.image.load('assets/bag/bag_background.png'), (720, 480)),
            'confirma' : pygame.transform.scale(pygame.image.load('assets/bag/caixa_confirma_bag.png'), (720, 144)),
            'seta' : pygame.transform.scale(pygame.image.load('assets/loja/seta_loja.png'), (12, 20)),

            'POTION' : pygame.transform.scale(pygame.image.load('assets/loja/potion.png'), (60, 66)),
            'SUPER POTION' : pygame.transform.scale(pygame.image.load('assets/loja/super_potion.png'), (60, 66)),
            'HYPER POTION' : pygame.transform.scale(pygame.image.load('assets/loja/hyper_potion.png'), (60, 66)),
            'MAX POTION' : pygame.transform.scale(pygame.image.load('assets/loja/max_potion.png'), (60, 66)),
            'FULL RESTORE' : pygame.transform.scale(pygame.image.load('assets/loja/full_restore.png'), (60, 66)),
            'ANTIDOTE' : pygame.transform.scale(pygame.image.load('assets/loja/antidoto.png'), (60, 66)),
            'BURN HEAL' : pygame.transform.scale(pygame.image.load('assets/loja/burn_heal.png'), (60, 66)),
            'PARALYZ HEAL' : pygame.transform.scale(pygame.image.load('assets/loja/paralyze_heal.png'), (60, 66)),
            'FULL HEAL' : pygame.transform.scale(pygame.image.load('assets/loja/full_heal.png'), (60, 66)),
            'REVIVE' : pygame.transform.scale(pygame.image.load('assets/loja/revive.png'), (60, 66)),
            'MAX REVIVE' : pygame.transform.scale(pygame.image.load('assets/loja/max_revive.png'), (60, 66)),
            'CANCEL' : pygame.transform.scale(pygame.image.load('assets/loja/sair.png'), (60, 66)),

            'blip' : pygame.mixer.Sound('assets/sound/effect/blip.mp3'),
            }

        self.state = {
            'ultimo' : ultimo,
            'i' : 0,
            'seta' : 0,
            'caixa_confirma' : False,
            'player' : player,
            'bag' : [],
            'pos_confirma' : [(578, 380), (578, 410)],
            'seta_confirma' : 0,
        }

        self.produtos = {
            'POTION' : [self.assets['POTION'], ['A spray-type wound medicine.', 'It restores the HP of one', 'Pokémon by 20 points.']],
            'SUPER POTION' : [self.assets['SUPER POTION'], ['A spray-type wound medicine.',  'It restores the HP of one', 'Pokémon by 50 points.']],
            'HYPER POTION' : [self.assets['HYPER POTION'], ['A spray-type wound medicine.', 'It restores the HP of one', 'Pokémon by 200 points.']],
            'MAX POTION' : [self.assets['MAX POTION'], ['A spray-type wound medicine.', 'It fully restores the HP of one', 'Pokémon.']],
            'ANTIDOTE' : [self.assets['ANTIDOTE'], ['A spray-type medicine. It', 'heals one Pokémon from a', 'poisoning.']],
            'PARALYZ HEAL' : [self.assets['PARALYZ HEAL'], ['A spray-type medicine. It', 'heals one Pokémon from', 'paralysis.']],
            'BURN HEAL' : [self.assets['BURN HEAL'], ['A spray-type medicine. It', 'heals one Pokémon of a', 'burn.']],
            'FULL HEAL' : [self.assets['FULL HEAL'], ['A spray-type medicine. It', 'heals all the status problems', 'of one Pokémon.']],
            'FULL RESTORE' : [self.assets['FULL RESTORE'], ['A medicine that fully restores', 'the HP and heals any status', 'problems of one Pokémon.']],
            'REVIVE' : [self.assets['REVIVE'], ['A medicine that revives a', 'fainted Pokémon, restoring HP', 'by half the maximum amount.']],
            'MAX REVIVE' : [self.assets['MAX REVIVE'], ['A medicine that revives a', 'fainted Pokémon, restoring HP', 'fully.']],
            'CANCEL' : [self.assets['CANCEL'], ['Quit shopping.']],
        }

        for item in player.bag:
            add = True
            for obj in self.state['bag']:
                if item.nome in obj:
                    obj[2] += 1
                    add = False
            if add == True:
                self.state['bag'].append([item.nome, item, 1])

        self.state['bag'].append(['CANCEL', None, None])

        if len(self.state['bag']) > 5:
            self.state['limite'] = len(self.state['bag']) - 5
        else:
            self.state['limite'] = len(self.state['bag'])

        #posicao seta
        self.pos = [
            (272, 45), #0
            (272, 92), #1
            (272, 140), #2
            (272, 186), #3
            (272, 238), #4
            (272, 281), #5
        ]

        #posicao texto
        self.linhas = [
            (293, 35), #0
            (293, 83), #1
            (293, 132), #2
            (293, 179), #3
            (293, 228), #4
            (293, 272), #5
        ]

    def desenha(self):
        self.window.blit(self.assets['background'], (0,0))

        if self.state['caixa_confirma']:
            self.window.blit(self.assets['confirma'], (0, 336))

        self.window.blit(self.assets['seta'], self.pos[self.state['seta']])

        i = 0
        comeca = 0
        for item in self.state['bag']:
            if comeca >= self.state['i']:
                self.window.blit(pygame.font.Font.render(self.assets['fonte'], item[0], True, (46, 46, 46)), self.linhas[i])
                if not item[0] == 'CANCEL':
                    self.window.blit(pygame.font.Font.render(self.assets['fonte'], 'X', True, (46, 46, 46)), (610, self.linhas[i][1]))
                    quant = pygame.font.Font.render(self.assets['fonte'], str(item[2]), True, (46, 46, 46))
                    self.window.blit(quant, (670 - quant.get_width(), self.linhas[i][1]))
                if self.state['seta'] == i:
                    self.state['selecionado'] = item
                i += 1
            else:
                comeca += 1
            if i > 5:
                break

            
        self.window.blit(self.produtos[self.state['selecionado'][0]][0], (31, 380))
        add = 360
        if not self.state['caixa_confirma']:
            for linha in self.produtos[self.state['selecionado'][0]][1]:
                self.window.blit(pygame.font.Font.render(self.assets['fonte'], linha, True, (255, 255, 255)), (127, add))
                add += 30
        else:
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], self.state['selecionado'][0] + ' is', True, (46, 46, 46)), (140, 375))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], 'selected.', True, (46, 46, 46)), (140, 407))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], 'USE', True, (46, 46, 46)), (600, 370))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], 'CANCEL', True, (46, 46, 46)), (600, 400))
            self.window.blit(self.assets['seta'], self.state['pos_confirma'][self.state['seta_confirma']])

        pygame.display.update()

    def update(self):


        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                return 'sair'
            if evento.type == pygame.KEYDOWN and evento.key == pygame.K_SPACE:
                if self.state['selecionado'][0] == 'CANCEL':
                    if self.state['ultimo'] != "batalha":
                        self.window = pygame.display.set_mode((624, 672))
                    pygame.display.set_caption('Pokémon')
                    return self.state['ultimo']
                if not self.state['caixa_confirma']:
                    self.state['caixa_confirma'] = True
                elif self.state['seta_confirma'] == 1:
                    self.state['seta_confirma'] = 0
                    self.state['caixa_confirma'] = False
                elif self.state['seta_confirma'] == 0:
                    return "troca", [4,self.state['selecionado'][1] ,self.state["ultimo"]]
            if evento.type == pygame.KEYDOWN and evento.key == pygame.K_ESCAPE:
                if not self.state['caixa_confirma']:
                    if self.state['ultimo'] != "batalha":
                        self.window = pygame.display.set_mode((624, 672))
                    pygame.display.set_caption('Pokémon')
                    return self.state['ultimo']
                else:
                    self.state['seta_confirma'] = 0
                    self.state['caixa_confirma'] = False
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_DOWN or evento.key == pygame.K_s) and self.state['i'] <= (self.state['limite']) and not self.state['caixa_confirma']:
                if self.state['seta'] == 5 and self.state['i'] < self.state['limite'] - 1:
                    self.state['i'] += 1
                    self.assets['blip'].play()
                elif self.state['seta'] < 5 and self.state['seta'] < len(self.state['bag']) - 1:
                    self.state['seta'] += 1
                    self.assets['blip'].play()
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_UP or evento.key == pygame.K_w) and not self.state['caixa_confirma']:
                if self.state['seta'] == 0 and self.state['i'] > 0:
                    self.state['i'] -= 1
                    self.assets['blip'].play()
                elif self.state['seta'] > 0:
                    self.state['seta'] -= 1
                    self.assets['blip'].play()
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_DOWN or evento.key == pygame.K_s) and self.state['caixa_confirma'] and self.state['seta_confirma'] < 1:
                self.state['seta_confirma'] += 1
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_UP or evento.key == pygame.K_w) and self.state['caixa_confirma'] and self.state['seta_confirma'] > 0:
                self.state['seta_confirma'] -= 1