from flask import Flask, render_template, request, jsonify, send_from_directory, Response, stream_with_context, redirect
from flask_compress import Compress
from queue import Queue
from datetime import datetime
from pprint import pprint
import threading
import random
import string
import dataHandlerPTT as ptt
import dataHandlerPTTPush as pttPush
import generalText as gen
import json

app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
Compress(app)

eventQueue = Queue()


@app.route('/stream')
def stream():
    return Response(stream_with_context(eventStream(eventQueue)), mimetype='text/event-stream')


def eventStream(eventQueue):
    while (True):
        eventNode = eventQueue.get(True)
        data = json.dumps(eventNode['data'], indent=4, ensure_ascii=False)
        data = data.splitlines()
        data = ['data:' + i for i in data]
        data = '\n'.join(data)
        yield "event:{event}\n{data}\n\n".format(event=eventNode['event'], data=data)


@app.route('/data/<path:path>')
def send_data(path):
    return send_from_directory('data', path)


@app.route('/generalTxt')
def generalTxt():
    return render_template('generalTxt.html', title="泛用文字視覺化工具")


@app.route('/post/generalTxt/addText', methods=['POST'])
def generalText_addText():
    text = request.json['text']
    stopwords = request.json['stopwords']
    randId = ''.join(random.choices(
        string.ascii_uppercase + string.ascii_lowercase, k=15))
    result = gen.processText(randId, text, stopwords)
    return jsonify(Result={
        'path': result
    })


@app.route('/')
def index():
    return redirect('/ptt')


@app.route('/ptt_push')
def ptt_push():
    return render_template('ptt_push.html', title='推文 Sententree')


@app.route('/ptt_push/init', methods=['POST'])
def pttPushInit():
    author = next(pttPush.getMostFrequentAuthor())
    pushes = pttPush.findAuthorPush(
        author=[author], stopwords=pttPush.defaultStopWords)
    result: dict = {
        'author': author,
        'stopwords': pttPush.defaultStopWords,
        'tsv': pushes[0],
        'json': pushes[1]
    }
    return jsonify(Result=result)


@app.route('/ptt_push/addRequest', methods=['POST'])
def pttPushAddRequest():
    pushes = pttPush.findAuthorPush(author=request.json['author'].split(
        ' '), aid=request.json['aid'].split(' '), keyword=request.json['keyword'], stopwords=request.json['stopwords'])
    result = {
        'keyword': request.json['keyword'],
        'tsv': pushes[0],
        'json': pushes[1]
    }
    return jsonify(Result=result)


@app.route('/ptt')
def pttSententree():
    return render_template('ptt.html', title="PTT Sententree")


@app.route('/ptt/keywordFrequency', methods=['POST'])
def findKeywordFrequency():
    print(request.json)
    content = request.json
    result = ptt.findKeywordFrequency(content)
    return jsonify(Result=result)


@app.route('/dev/updateContent')
def updateContent():
    ptt.loadPostContents()
    return jsonify(Result={
        'done': True
    })


@app.route('/addRequest', methods=['POST'])
def addRequest():
    content = request.json
    ready = False
    info = None

    files = ptt.findResult(content)

    info = files
    ready = True

    key = ptt.calcKey(content['startDate'],
                      content["endDate"], content["keyword"], content["pos"])

    return jsonify(Result={
        'ready': ready,
        'info': info,
        'key': key
    })


@app.route("/init", methods=['POST'])
def initPage():
    startDate = datetime.today().replace(day=1).strftime('%Y-%m-%d')
    endDate = datetime.today().strftime('%Y-%m-%d')
    files = ptt.getDefault(startDate, endDate)
    return jsonify(Result={
        "startDate": startDate,
        'endDate': endDate,
        'keyword': '',
        'info': files
    })


@app.route('/resource/<path:path>')
def send_resource(path):
    return send_from_directory('resource', path)


@app.route("/dcard_dev")
def dcard_dev():
    return render_template('dcard.html', title='DCard Sentntree 測試版')


if __name__ == "__main__":
    app.run(debug=False, port=4980, host='0.0.0.0', threaded=False)
