import sys
from datetime import datetime
import logging
from logging.handlers import RotatingFileHandler
from flask import Flask
from flask import request
from os import popen
from os import system

app = Flask(__name__)


@app.route('/')
def main_form():
    app.logger.info('main_form')
    page = '<br><blockquote><tt><h1><a href="/dcc">0 level characters</a>\
           <p><a href="/familiar">Familiars</a>\
           <p><a href="/demon">Demons</a>\
           <p><a href="/undead">Un-dead</a>\
           <p><a href="/beastman">Beastman</a>\
           <p><a href="/scroll">Scroll</a>\
           <p><a href="/magicweapon">Magic weapons</a>\
           <form action="runes" id="textform" method="post">\
           <textarea name="runic"></textarea><br>\
           <input type="submit" value="ᚱᚢᚾᛖᛊ"></form></h1></tt><p><blockquote>\
           <font color="white">{}'.format(popen('Netinfo').read().replace('\n', '<br>'))
    return page


@app.route('/dcc')
def dcct():
    app.logger.info('0Level')
    data = popen('0Level').read()
    return ('<blockquote><tt><h2>{}'.format(data.replace('\n', '<br>').replace(' ', '&nbsp;')))


@app.route('/demon')
def demon():
    app.logger.info('Demon')
    data = popen('Demon').read()
    #return ("<blockquote><input id='reload-button' type='button' value='&#127922;' style='border:1;' onClick='window.location.reload()'><tt><h2>{}".format(data.replace('\n', '<br>')))
    return ("<blockquote><tt><h2>{}".format(data.replace('\n', '<br>')))


@app.route('/undead')
def undead():
    app.logger.info('Undead')
    data = popen('Undead').read()
    #return ("<blockquote><input id='reload-button' type='button' value='&#127922;' style='border:1;' onClick='window.location.reload()'><tt><h2>{}".format(data.replace('\n', '<br>')))
    return ("<blockquote><tt><h2>{}".format(data.replace('\n', '<br>')))


@app.route('/magicweapon')
def magicweapon():
    app.logger.info('Magic Weapon')
    data = popen('MagicWeapon').read()
    #return ("<blockquote><input id='reload-button' type='button' value='&#127922;' style='border:1;' onClick='window.location.reload()'><tt><h2>{}".format(data.replace('\n', '<br>')))
    return ("<blockquote><tt><h2>{}".format(data.replace('\n', '<br>')))


@app.route('/familiar')
def familiar():
    app.logger.info('Familiar')
    data = popen('Familiar').read()
    #return ("<blockquote><input id='reload-button' type='button' value='&#127922;' style='border:1;' onClick='window.location.reload()'><tt><h2>{}".format(data.replace('\n', '<br>')))
    return ("<blockquote><tt><h2>{}".format(data.replace('\n', '<br>')))


@app.route('/scroll')
def scroll():
    app.logger.info('Scroll')
    data = popen('Scroll').read()
    # return ("<blockquote><input id='reload-button' type='button' value='&#127922;' style='border:1;' onClick='window.location.reload()'><tt><h2>{}".format(data.replace('\n', '<br>')))
    return ("<blockquote><tt><h2>{}".format(data.replace('\n', '<br>')))

@app.route('/beastman')
def beastman():
    app.logger.info('Beastman')
    data = popen('Beastman').read()
    # return ("<blockquote><input id='reload-button' type='button' value='&#127922;' style='border:1;' onClick='window.location.reload()'><tt><h2>{}".format(data.replace('\n', '<br>')))
    return ("<blockquote><tt><h2>{}".format(data.replace('\n', '<br>')))

@app.route('/dcc_html')  #, methods=['GET', 'POST'])
def dcc_html():
    app.logger.info('{} dcc 0level'.format(datetime.now()))
    with open ('0level.html', 'r') as npc:
        return npc.read()

@app.route('/dnd')  #, methods=['GET', 'POST'])
def dnd():
    app.logger.info('{} dnd 0level'.format(datetime.now()))
    with open ('npc.html', 'r') as npc:
        return npc.read()

@app.route('/rotas')
def rotas():
    app.logger.info('rotas')
    data = popen('Rotas').read()
    return '<blockquote><tt><h1>{}'.format(data.replace('\n', '<br>'))

@app.route('/bones/<input>')
def bones(input):
    app.logger.info('RollBones')
    data = popen('RollBones {}'.format(input)).read()
    return '<blockquote><h1><tt>{}</tt>'.format(data.replace('\n', '<br>').replace('|', ''))


@app.route('/runic/<phrase>')
def runic(phrase):
    app.logger.info('Runic')
    data = popen('Runic {}'.format(phrase)).read()
    return '<blockquote>{}<br>{}'.format(phrase, data.replace(' ', '&nbsp;&nbsp;'))


@app.route('/runes', methods=['POST'])
def runes():
    app.logger.info('{} Submitted: {}'.format(datetime.now(), request.form["runic"]))
    data = popen('Runic {}'.format(request.form["runic"])).read()
    return '<blockquote>{}<br>{}'.format(request.form["runic"], data.replace(' ', '&nbsp;&nbsp;'))


if __name__ == '__main__':
    handler = RotatingFileHandler('foo.log', maxBytes=10000, backupCount=1)
    handler.setLevel(logging.INFO)
    app.logger.addHandler(handler)
    app.run(debug=True)