import pygame
from funcoes import *
import json

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

        self.assets = {
            'fundo' : pygame.transform.scale(pygame.image.load('assets/intro/intro_modelo.png'), (720, 480)),
            'title' : pygame.transform.scale(pygame.image.load('assets/intro/elitefour_titulo.png'), (210.75, 148)),
            'seta' : pygame.transform.scale(pygame.image.load('assets/loja/seta_loja.png'), (18, 30)),

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

            "fonte":  pygame.font.Font("assets/Univers Next Pro Heavy Condensed.ttf", 50),
            "fonte_nome":  pygame.font.Font("assets/Univers Next Pro Heavy Condensed.ttf", 30),
            "fonte_aviso":  pygame.font.Font("assets/Univers Next Pro Heavy Condensed.ttf", 25),
        }

        pygame.mixer.music.load('assets/sound/music/inicio.mp3')
        pygame.mixer.music.play(-1)
        pygame.mixer.music.set_volume(0.5)        

        self.state = {
            'pos' : [(95, 350), (95, 405)],
            'atual' : 0,
            'input' : False,
            'nome' : '',
            'caixa' : pygame.Rect(100, 380, 200, 40),
            'aviso' : '',
        }

    def desenha(self):
        self.window.blit(self.assets['fundo'], (0, 0))
        self.window.blit(self.assets['title'], (137, 175))

        if not self.state['input']:
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "START GAME", True, (255, 255, 255)), (130, 345))
            self.window.blit(pygame.font.Font.render(self.assets['fonte'], "TUTORIAL", True, (255, 255, 255)), (130, 400))

            self.window.blit(self.assets['seta'], self.state['pos'][self.state['atual']])
        else:
            self.window.blit(pygame.font.Font.render(self.assets['fonte_nome'], "Digite seu nome", True, (255, 255, 255)), (100, 345))
            pygame.draw.rect(self.window, (255, 255, 255), self.state['caixa'], 2)
            self.window.blit(pygame.font.Font.render(self.assets['fonte_nome'], self.state['nome'], True, (255, 255, 255)), (self.state['caixa'].x + 5, self.state['caixa'].y + 5))
            self.window.blit(pygame.font.Font.render(self.assets['fonte_aviso'], self.state['aviso'], True, (255, 0, 0)), (100, 423))

        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_w or evento.key == pygame.K_UP) and self.state['atual'] > 0 and not self.state['input']:
                self.state['atual'] -= 1
                self.assets['blip'].play()
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_s or evento.key == pygame.K_DOWN) and self.state['atual'] < 1 and not self.state['input']:
                self.state['atual'] += 1
                self.assets['blip'].play()
            if evento.type == pygame.KEYDOWN and self.state['input']:
                if evento.key == pygame.K_RETURN:
                    self.state['nome'] =self.state['nome'].strip()
                    if not verifica_input(self.state['nome']):
                        self.state['aviso'] = 'Só são aceitas letras e números'
                    elif self.state['nome'] == "clear":
                        
                        with open('deafault.json', 'r') as openfile:
                                blue = json.load(openfile)

                        with open("save.json", "w") as outfile:
                            json.dump(blue, outfile)
                            
                        self.state['aviso'] = "save cleared"
                    else:
                        return 'jogo'
                if evento.key == pygame.K_BACKSPACE:
                    self.state['nome'] = self.state['nome'][:-1]
                elif len(self.state['nome']) < 10:
                    self.state['nome'] += evento.unicode
            if evento.type == pygame.KEYDOWN and (evento.key == pygame.K_SPACE or evento.key == pygame.K_RETURN):
                if self.state['atual'] == 0:
                    self.state['input'] = True
                if self.state['atual'] == 1:
                    return 'tutorial'

    def nome(self):
        return self.state['nome']
