from flask import Flask, request, redirect, render_template
import cgi
import os

app = Flask(__name__)
app.config['DEBUG'] = True


@app.route('/validate-time')
def display_time_form():
    return render_template('time_form.html')

def is_integer(num):
    try:
        int(num)
        return True
    except ValueError:
        return False

@app.route('/validate-time', methods=['POST'])
def validate_time():
    hours=request.form['hours']
    minutes=request.form['minutes']

    hours_error=''
    minutes_error=''

    if not is_integer(hours):
        hours_error='Not a Valid Integer'
        hours=''
    else:
        hours=int(hours)
        if hours>23 or hours<0:
            hours_error='Hour value out of range (0-23)'
            hours=''

    if not is_integer(minutes):
        minutes_error='Not a Valid Integer'
        minutes=''
    else:
        minutes=int(minutes)
        if minutes>59 or minutes<0:
            minutes_error='Minute value out of range (0-59)'
            minutes=''

    if not hours_error and not minutes_error:
        time=str(hours) + ':' + str(minutes)
        return redirect ('/valid-time?time={0}'.format(time))
    else:
        return render_template('time_form.html', hours_error=hours_error, minutes_error=minutes_error, hours=hours, minutes=minutes)
    
@app.route("/valid-time")
def valid_time():
    time=request.args.get('time')
    return '<h1>You submitted {0}. Thanks for submitting a valid time!</h1>'.format(time)


@app.route('/')
def index():
    return render_template('hello_form.html')


@app.route("/hello", methods=['POST'])
def hello():
    first_name=request.form['first_name']
    return render_template('hello_greeting.html', name=first_name)

@app.route("/form-inputs")
def display_form_inputs():
    return """
    <style>
    br {margin-bottom: 20px;}
    </style>
    <form method='POST'>
        <label>type=text
            <input name="user-name" type="text" />
        </label>
        <br>
        <label>type=password
            <input name="user-password" type="password" />
        </label>
        <br>
        <label>type=email
            <input name="user-email" type="email" />
        </label>
        <br>
        <input name="shopping-cart-id" value="0129384" type="hidden" />
        <br>
        <label>Ketchup
            <input type="checkbox" name="cb1" value="first-cb" />
        </label>
        <br>
        <label>Mustard
            <input type="checkbox" name="cb2" value="second-cb" />
        </label>
        <br>
        <label>Small
            <input type="radio" name="coffee-size" value="sm" />
        </label>
        <label>Medium
            <input type="radio" name="coffee-size" value="med" />
        </label>
        <label>Large
            <input type="radio" name="coffee-size" value="lg" />
        </label>
        <br>
        <label>Your life story
            <textarea name="life-story"></textarea>
        </label>
        <br>
        <label>LaunchCode Hub
            <select name="lc-hub">
                <option value="kc">Kansas City</option>
                <option value="mia">Miami</option>
                <option value="ri">Providence</option>
                <option value="sea">Seattle</option>
                <option value="pdx">Portland</option>
            </select>
        </label>
        <br>
        <input type="submit" />
    </form>
    """


@app.route("/form-inputs", methods=['POST'])
def print_form_values():
    resp = ""
    for field in request.form.keys():
        resp += "<b>{key}</b>: {value}<br>".format(key=field, value=request.form[field])
    return resp

tasks=[]

@app.route("/todos", methods=['POST', 'GET'])
def todos():
    if request.method == 'POST':
        task = request.form['task']
        tasks.append(task)

    return render_template('todos.html', title='TODOs', tasks=tasks)

app.run()