from flask import Flask, flash, make_response, redirect, request, jsonify, render_template
from flask_cors import CORS
from string import ascii_lowercase
from hangman import init_game_data
import hangman.config.constants as constants
import hangman.utils as utils
#import hangman.validations as validations
from hangman.validations import isLetter, isValidLetter, check_letter
from config.db import DB
from config.api import WordApi

app = Flask(__name__)  # referencing current file
# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'

CORS(app)

db_instance = DB(app)

crud_methods = ['GET', 'POST']
Word = db_instance.models['word']
WORD = WordApi.get_word()
print(WORD)
#Word.add_word(word_here, redirect('/')) add word to db
#Tomar palabra
GAME_DATA = init_game_data(WORD)
#Descomponer palabra
Guess=GAME_DATA['GuessLetters']
print(Guess)
gametries=GAME_DATA['tries']
print(gametries)
letters=GAME_DATA['letters']
final="Ha perdido todos los intentos"
word = list(WORD)
guess_word = ['_' for x in word]
#url sin ningún parámetro
@app.route('/', methods=crud_methods)
def index():
    print('estoy en home')
    global WORD,gametries,Guess, letters,word, guess_word, final
    print(Guess) 
    print(gametries)
    if request.method == 'POST':
        req = request.form
        # TODO handle game logic functionality different
        # handle different scenarios
        # handle errors
        # handle tries  
        input = req['letter']
        print(input)
      #Valida si el input es una letra
        count=gametries
        while (count <= constants.MAX_TRIES): 
             if isLetter(input) == True:
                 if isValidLetter(input,Guess,gametries)==True:
                     check_letter(input,word,guess_word)
                     alerta=('Ha acertado una letra')
                     print(check_letter(input,word,guess_word))
                     return render_template('index.html', 
                         instruction1=constants.INSTRUCTION1,
                         instruction2=constants.INSTRUCTION2,
                         instruction3=constants.INSTRUCTION3, 
                         game=GAME_DATA, 
                         letters=letters,
                         guessLetters=Guess, 
                         alerta=alerta,
                         gametries=gametries,
                         guess_word= (check_letter(input,word,guess_word)),
                         letter=input,
                         final=final)
                 else:
                     alerta=('Letra incorrecta')
                     gametries = gametries + 1
                     return render_template('index.html',
                         instruction1=constants.INSTRUCTION1,
                         instruction2=constants.INSTRUCTION2,
                         instruction3=constants.INSTRUCTION3,
                         game=GAME_DATA, 
                         letters=letters,
                         guessLetters=Guess,
                         guess_word=guess_word,  
                         alerta=alerta,gametries=gametries
                         )
             else:
                 print('No es letra')
                 aviso=('No ha digitado una letra')
                 return render_template('index.html',
                     instruction1=constants.INSTRUCTION1,
                     instruction2=constants.INSTRUCTION2,
                     instruction3=constants.INSTRUCTION3,
                     game=GAME_DATA, 
                     letters=letters,
                     GuessLetters=Guess,gametries=gametries,guess_word=guess_word, aviso=aviso,final=final) 
       
             count=count + gametries
         
             print(GAME_DATA.utils.gameOver)
             final="Ha perdido todos los intentos"
             return render_template(
                 'index.html',
                 instruction1=constants.INSTRUCTION1,
                 instruction2=constants.INSTRUCTION2,
                 instruction3=constants.INSTRUCTION3,
                 game=GAME_DATA,
                 letters=letters,
                 guessLetters=Guess, 
                 gametries=gametries,
                 guess_word=guess_word,
                 final=final) 
    else:
         GAME_DATA['words'] = Word.get_played_words()
         return render_template(
             'index.html',
             instruction1=constants.INSTRUCTION1,
             instruction2=constants.INSTRUCTION2,
             instruction3=constants.INSTRUCTION3,
             game=GAME_DATA,
             guessLetters=Guess,
             gametries=gametries,
             guess_word=guess_word,
             final=final) 

@app.route('/restart', methods=['GET'])
def restart():
    # TODO make restart functionality
    global WORD,gametries,Guess
    Word = db_instance.models['word']
    WORD = WordApi.get_word()
    print(WORD)
    GAME_DATA = init_game_data(WORD)
    Guess = GAME_DATA['GuessLetters']
    print(Guess)
    gametries=GAME_DATA['tries']
    print(gametries)
    print('estoy en restart')
    return redirect('/')
    return render_template(
            'index.html',
            instruction1=constants.INSTRUCTION1,
            instruction2=constants.INSTRUCTION2,
            instruction3=constants.INSTRUCTION3,
            game=GAME_DATA,gametries=gametries,letters=letters,final=final) 

if __name__ == '__main__':
    db_instance.init_db()
    app.run(debug=True)

from flask import Flask, flash, make_response, redirect, request, jsonify, render_template
from flask_cors import CORS
from string import ascii_lowercase
from hangman import init_game_data
import hangman.config.constants as constants
import hangman.utils as utils
#import hangman.validations as validations
from hangman.validations import isLetter, isValidLetter, check_letter
from config.db import DB
from config.api import WordApi

app = Flask(__name__)  # referencing current file
# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'

CORS(app)

db_instance = DB(app)

crud_methods = ['GET', 'POST']
Word = db_instance.models['word']
WORD = WordApi.get_word()
print(WORD)
#Word.add_word(word_here, redirect('/')) add word to db
#Tomar palabra
GAME_DATA = init_game_data(WORD)
#Descomponer palabra
Guess=GAME_DATA['GuessLetters']
print(Guess)
gametries=GAME_DATA['tries']
print(gametries)
letters=GAME_DATA['letters']
final="Ha perdido todos los intentos"
word = list(WORD)
guess_word = ['_' for x in word]
#url sin ningún parámetro
@app.route('/', methods=crud_methods)
def index():
    print('estoy en home')
    global WORD,gametries,Guess, letters,word, guess_word, final
    print(Guess) 
    print(gametries)
    if request.method == 'POST':
        req = request.form
        # TODO handle game logic functionality different
        # handle different scenarios
        # handle errors
        # handle tries  
        input = req['letter']
        print(input)
      #Valida si el input es una letra
        count=gametries
        while (count <= constants.MAX_TRIES): 
             if isLetter(input) == True:
                 if isValidLetter(input,Guess,gametries)==True:
                     check_letter(input,word,guess_word)
                     alerta=('Ha acertado una letra')
                     print(check_letter(input,word,guess_word))
                     return render_template('index.html', 
                         instruction1=constants.INSTRUCTION1,
                         instruction2=constants.INSTRUCTION2,
                         instruction3=constants.INSTRUCTION3, 
                         game=GAME_DATA, 
                         letters=letters,
                         guessLetters=Guess, 
                         alerta=alerta,
                         gametries=gametries,
                         guess_word= (check_letter(input,word,guess_word)),
                         letter=input,
                         final=final)
                 else:
                     alerta=('Letra incorrecta')
                     gametries = gametries + 1
                     return render_template('index.html',
                         instruction1=constants.INSTRUCTION1,
                         instruction2=constants.INSTRUCTION2,
                         instruction3=constants.INSTRUCTION3,
                         game=GAME_DATA, 
                         letters=letters,
                         guessLetters=Guess,
                         guess_word=guess_word,  
                         alerta=alerta,gametries=gametries
                         )
             else:
                 print('No es letra')
                 aviso=('No ha digitado una letra')
                 return render_template('index.html',
                     instruction1=constants.INSTRUCTION1,
                     instruction2=constants.INSTRUCTION2,
                     instruction3=constants.INSTRUCTION3,
                     game=GAME_DATA, 
                     letters=letters,
                     GuessLetters=Guess,gametries=gametries,guess_word=guess_word, aviso=aviso,final=final) 
       
             count=count + gametries
         
             print(GAME_DATA.utils.gameOver)
             final="Ha perdido todos los intentos"
             return render_template(
                 'index.html',
                 instruction1=constants.INSTRUCTION1,
                 instruction2=constants.INSTRUCTION2,
                 instruction3=constants.INSTRUCTION3,
                 game=GAME_DATA,
                 letters=letters,
                 guessLetters=Guess, 
                 gametries=gametries,
                 guess_word=guess_word,
                 final=final) 
    else:
         GAME_DATA['words'] = Word.get_played_words()
         return render_template(
             'index.html',
             instruction1=constants.INSTRUCTION1,
             instruction2=constants.INSTRUCTION2,
             instruction3=constants.INSTRUCTION3,
             game=GAME_DATA,
             guessLetters=Guess,
             gametries=gametries,
             guess_word=guess_word,
             final=final) 

@app.route('/restart', methods=['GET'])
def restart():
    # TODO make restart functionality
    global WORD,gametries,Guess
    Word = db_instance.models['word']
    WORD = WordApi.get_word()
    print(WORD)
    GAME_DATA = init_game_data(WORD)
    Guess = GAME_DATA['GuessLetters']
    print(Guess)
    gametries=GAME_DATA['tries']
    print(gametries)
    print('estoy en restart')
    return redirect('/')
    return render_template(
            'index.html',
            instruction1=constants.INSTRUCTION1,
            instruction2=constants.INSTRUCTION2,
            instruction3=constants.INSTRUCTION3,
            game=GAME_DATA,gametries=gametries,letters=letters,final=final) 

if __name__ == '__main__':
    db_instance.init_db()
    app.run(debug=True)

#import hangman.validations as validations # referencing current file # Set the secret key to some random bytes. Keep this really secret! #y2L"F4Q8z\n\xec]/' #Word.add_word(word_here, redirect('/')) add word to db #Tomar palabra #Descomponer palabra #url sin ningún parámetro # TODO handle game logic functionality different # handle different scenarios # handle errors # handle tries #Valida si el input es una letra # TODO make restart functionality