import flask
from flask import request, jsonify, Flask, abort, g, url_for, send_file, send_from_directory
import json
import os

app = Flask(__name__, static_url_path='')
app.config["DEBUG"] = False
app.config['SECRET_KEY'] = 'put_flask_autogenerated_key_here' # https://stackoverflow.com/a/54433731

"""

"""
@app.route('/', methods=['GET'])
def home():
    """
    the index page
    """
    return "<a href=\"itms-services://?action=download-manifest&amp;url=https://192.168.100.16:5000/manifest.plist\">Install IPA<a>"


@app.route('/manifest.plist', methods=['GET'])
def getPlist():
    """
    url route for manifest plist
    """
    try:
        return send_file('app_bundle/manifest.plist', attachment_filename='manifest.plist')
    except Exception as e:
        return str(e)


@app.route('/app_bundle/<path:path>')
def send_files(path):
    """
    load all files from app_bundle directory
    """
    print(path)
    return send_from_directory('app_bundle', path)


@app.route('/app/app.ipa', methods=['GET'])
def getAppIPA():
    """
    the main app package's url
    """
    try:
        return send_file('app_bundle/app.ipa', attachment_filename='app.ipa')
    except Exception as e:
        return str(e)


@app.route('/favicon.ico', methods=['GET'])
def getFavIcon():
    """
    just dont want 404 error for favicon
    """
    try:
        return send_file('favicon.ico', attachment_filename='favicon.ico')
    except Exception as e:
        return str(e)


if __name__ == "__main__":
    # load the ssl private keys created earlier
    context_ssl = ('debug_cert.pem', 'debug_key.pem')
    # not on local host, it would serve on the local network and default iPv4 address
    # e.g. 192.168.100.16
    app.run(host='0.0.0.0', port=5000, debug=False, ssl_context=context_ssl)
