from flask import Flask
from flask import render_template
from flask import request
import os
from flask import session
import json
import logging
from controllers.received_money_controller import ReceivedMoneyController

FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
_logger = logging.getLogger(__file__)

from add_product_bot import handle_update

app = Flask(__name__)


@app.route('/botserver_recycled', methods=['POST', 'GET'])
def hello():
    update = request.data.decode('utf8')
    update = json.loads(update)

    with open(os.path.abspath('/var/www/jspanda_bot/session_file'), 'r') as fh:
        what_to_do = fh.readline()
        logging.info("what_to_do before : {}".format(what_to_do))
        what_to_do = handle_update(update, what_to_do)
        logging.info("what_to_do after: {}".format(what_to_do))
    with open(os.path.abspath('/var/www/jspanda_bot/session_file'), 'w') as fh:
        if what_to_do:
            fh.write(what_to_do)
        else:
            fh.write('nothing')
    return ""  # it is important as this return 200 success response to telegram


@app.route('/show_received_money')
def show_received_money():
    received_money_obj = ReceivedMoneyController()
    return received_money_obj.show_all_received_money()


@app.route('/show_session')
def show_session():
    return "what_to_do session : {}".format(session.get('what_to_do'))


@app.route('/fx')
def fx():
    return render_template("fxcalc.html")


if __name__ == '__main__':
    app.run(host='0.0.0.0')
