from Negocio import controller
import forms, functions
from flask import Flask, render_template, request, redirect, url_for, flash
   
def register(mysql, request):
   registerForm= forms.RegisterForm(request.form)
   if request.method == 'POST' and registerForm.validate():
      return controller.registraUsuario(mysql, request, registerForm)
   return render_template('register.html', form=registerForm)

def Index(mysql, request):
   if request.method=='GET':
      success= request.args.get('success')
      if success==None:
         if controller.usuarioIniciado():
               return redirect(url_for('home'))
         else:
            return render_template('Index.html')
      else:          
         return render_template('Index.html', success=success)
   return render_template('Index.html')
          

def home(mysql, request):
    if request.method== 'POST':
       controller.iniciarSesion(mysql, request)
    if controller.usuarioIniciado() and request.method== 'GET':
       return controller.mostrarRutinas(mysql, request)
    else:
        return redirect(url_for('Index'))

def historial_rutina(mysql, request):
    if controller.usuarioIniciado() and request.method== 'GET':
       return controller.mostrar_historial_rutina(mysql, request)
    else:
        return redirect(url_for('Index'))

def historial_usuario(mysql, request):
    if controller.usuarioIniciado() and request.method== 'GET':
       return controller.mostrar_historial_usuario(mysql, request)
    else:
        return redirect(url_for('Index'))

def perfil(mysql, request):
    if controller.usuarioIniciado and request.method=='GET':
        success= request.args.get('success')
        usuario=controller.datosUsuario(mysql, request)
        imc=functions.IMC(usuario[8], usuario[7])
        m_basal= controller.calcular_metabolismo_basal(mysql, usuario[7], usuario[8])
        return render_template('perfil.html', success=success, usuario=usuario, imc=imc, evaluacion=functions.evaluarIMC(imc), pg=functions.porcentajeGrasa(usuario[5], usuario[9], usuario[10], usuario[7], usuario[11]), m_basal=m_basal )
    else:
        return redirect(url_for('Index'))

def ActualizarPerfil(mysql, request):
   actualize_form= forms.PerfilForm(request.form)
   if request.method == 'POST' and controller.usuarioIniciado:
      if actualize_form.validate():
         return controller.actualizar_perfil(mysql, request)
      else:
         flash("Alguno de los datos es incorrecto")
         return redirect(url_for('actualizar_perfil', success=False))
   else:
      if request.method == 'GET' and controller.usuarioIniciado:
         datos=controller.formulario_perfil(mysql)
         return render_template('actualizar_perfil.html', form=actualize_form, datos=datos)
      return redirect(url_for('perfil'))

def administracionRutinas(mysql, request):
    if controller.usuarioIniciado():
        return render_template('administracion_rutinas.html')
    else:
        return redirect(url_for('Index'))

def crearRutina(mysql, request):
   if request.method =='POST' and controller.usuarioIniciado():
        return controller.agregarRutina(mysql, request)
   else:
      if controller.rutinaIniciada() and controller.usuarioIniciado():
         return controller.rutinaEnCurso(mysql, request)
      if controller.usuarioIniciado():
         return redirect(url_for('adm_rutinas'))
      else:
         return redirect(url_for('Index'))

def registrarEjerciciosRutina(mysql, request):
   if request.method == 'POST':
      return controller.registrarEjerciciosRutina(mysql, request)
   return redirect(url_for('adm_rutinas'))

def modificarRutina(mysql, request):
    if controller.usuarioIniciado():
      rutinas=controller.rutinasUsuario(mysql)
      rutinaEjercicios=controller.rutinaEjercicios(mysql)
      datosEjer=controller.datosEjercicios(mysql)
      return render_template('modify_rutina.html', rutinas=rutinas , ejercicios=datosEjer, rutinaEjer=rutinaEjercicios)
    else:
        return redirect(url_for('Index'))

def registrarModiciaciones(mysql, request):
   if request.method == 'POST':
      return controller.registrarModificaciones(mysql, request)
   return redirect(url_for('adm_rutinas'))

def eliminarRutina(mysql,request):
   if controller.usuarioIniciado():
      rutinas=controller.rutinasUsuario(mysql)
      rutinaEjercicios=controller.rutinaEjercicios(mysql)
      return render_template('delete_rutina.html', rutinas=rutinas , rutinaEjer=rutinaEjercicios)
   else:
      return redirect(url_for('Index'))

def registrarEliminacion(mysql, request):
    if request.method=='POST' and controller.usuarioIniciado():
        return controller.registrarEliminacion(mysql, request)
    else:
        return redirect(url_for('Index'))

def registrarEjercicios(mysql, request):
   if request.method == 'POST':
      return controller.registrarEjercicio(mysql, request)
   return redirect(url_for('ejercicios'))
[(6, 'forms.RegisterForm', 'forms.RegisterForm', False, 'import forms, functions\n'), (9, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (21, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (55, 'forms.PerfilForm', 'forms.PerfilForm', False, 'import forms, functions\n'), (69, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (91, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (105, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (8, 'Negocio.controller.registraUsuario', 'controller.registraUsuario', False, 'from Negocio import controller\n'), (13, 'flask.request.args.get', 'request.args.get', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (26, 'Negocio.controller.iniciarSesion', 'controller.iniciarSesion', False, 'from Negocio import controller\n'), (27, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (28, 'Negocio.controller.mostrarRutinas', 'controller.mostrarRutinas', False, 'from Negocio import controller\n'), (33, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (34, 'Negocio.controller.mostrar_historial_rutina', 'controller.mostrar_historial_rutina', False, 'from Negocio import controller\n'), (39, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (40, 'Negocio.controller.mostrar_historial_usuario', 'controller.mostrar_historial_usuario', False, 'from Negocio import controller\n'), (46, 'flask.request.args.get', 'request.args.get', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (47, 'Negocio.controller.datosUsuario', 'controller.datosUsuario', False, 'from Negocio import controller\n'), (48, 'functions.IMC', 'functions.IMC', False, 'import forms, functions\n'), (49, 'Negocio.controller.calcular_metabolismo_basal', 'controller.calcular_metabolismo_basal', False, 'from Negocio import controller\n'), (70, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (75, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (76, 'Negocio.controller.agregarRutina', 'controller.agregarRutina', False, 'from Negocio import controller\n'), (80, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (87, 'Negocio.controller.registrarEjerciciosRutina', 'controller.registrarEjerciciosRutina', False, 'from Negocio import controller\n'), (88, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (92, 'Negocio.controller.rutinasUsuario', 'controller.rutinasUsuario', False, 'from Negocio import controller\n'), (93, 'Negocio.controller.rutinaEjercicios', 'controller.rutinaEjercicios', False, 'from Negocio import controller\n'), (94, 'Negocio.controller.datosEjercicios', 'controller.datosEjercicios', False, 'from Negocio import controller\n'), (95, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (101, 'Negocio.controller.registrarModificaciones', 'controller.registrarModificaciones', False, 'from Negocio import controller\n'), (102, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (106, 'Negocio.controller.rutinasUsuario', 'controller.rutinasUsuario', False, 'from Negocio import controller\n'), (107, 'Negocio.controller.rutinaEjercicios', 'controller.rutinaEjercicios', False, 'from Negocio import controller\n'), (108, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (113, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (114, 'Negocio.controller.registrarEliminacion', 'controller.registrarEliminacion', False, 'from Negocio import controller\n'), (120, 'Negocio.controller.registrarEjercicio', 'controller.registrarEjercicio', False, 'from Negocio import controller\n'), (121, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (15, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (20, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (30, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (36, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (42, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (52, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (58, 'Negocio.controller.actualizar_perfil', 'controller.actualizar_perfil', False, 'from Negocio import controller\n'), (60, 'flask.flash', 'flash', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (64, 'Negocio.controller.formulario_perfil', 'controller.formulario_perfil', False, 'from Negocio import controller\n'), (65, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (66, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (72, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (78, 'Negocio.controller.rutinaIniciada', 'controller.rutinaIniciada', False, 'from Negocio import controller\n'), (78, 'Negocio.controller.usuarioIniciado', 'controller.usuarioIniciado', False, 'from Negocio import controller\n'), (79, 'Negocio.controller.rutinaEnCurso', 'controller.rutinaEnCurso', False, 'from Negocio import controller\n'), (97, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (110, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (116, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (18, 'flask.render_template', 'render_template', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (50, 'functions.evaluarIMC', 'functions.evaluarIMC', False, 'import forms, functions\n'), (50, 'functions.porcentajeGrasa', 'functions.porcentajeGrasa', False, 'import forms, functions\n'), (61, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (81, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (83, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n'), (16, 'flask.url_for', 'url_for', False, 'from flask import Flask, render_template, request, redirect, url_for, flash\n')]