import base64
import datetime
import http.client
import json
import sys
from pyblake2 import blake2b

from flask import Flask, request
from flask import render_template

from util.config import host_config

app = Flask(__name__)

AUTH_SIZE = 16

API_KEY = '<KEY>'  # use the provided one
SECRET_KEY = '2a4309a8a2c54e539e5cb57e3a4816d9'  # use the provided one; keep this one well protected


def generate(msg):
    h = blake2b(digest_size=AUTH_SIZE, key=SECRET_KEY.encode('utf-8'))
    h.update(msg.encode('utf-8'))
    return h.hexdigest()




def send_response(data):
    host = '0.0.0.0'
    port = '12892'
    path = '/api/external/v1/feedback/flair'

    headers = {"Content-type": "application/json", "charset": "utf-8"}
    conn = http.client.HTTPConnection(host, port)
    conn.request("POST", path, json.JSONEncoder().encode(data), headers)
    response = conn.getresponse()
    print(response.status, response.reason)

    tag_result = response.read()
    print(tag_result)
    return tag_result




def encode_request_json(text):
    data = {
        'text': base64.b64encode(text.encode('utf-8')).decode('utf-8'),
        'api-key': API_KEY,
        'user': '2IFQKT0_1_1',
        'case': '1',
        'proof': generate(text)
    }
    print(data)
    return data

import json
def encode_response_json(response):
    response=json.loads(response)
    data={}
    content_len=0
    reasoning_len=0
    if len(response["content"])>0:
        content_len=len(response["content"][0])
    if len(response["reasoning"])>0:
        reasoning_len=len(response["reasoning"][0])
    keys_content=["index_c","position_c","word_c","token_string_c","comment_c"]
    keys_reasoning=["index_r","position_r","word_r","token_string_c","comment_r"]

    for i in range(content_len):
        data[keys_content[i]]=" ".join(str(response["content"][0][i])) if type(response["content"][0][i]) is list else response["content"][0][i]
    for i in range(reasoning_len):
        data[keys_reasoning[i]]=" ".join(str(response["reasoning"][0][i])) if type(response["reasoning"][0][i]) is list else response["reasoning"][0][i]

    print(data)
    return data



@app.route('/predict', methods=['GET'])
def api_feedback_with_model():
    mytext = request.args.get('text', '')
    data=encode_request_json(mytext)
    tag_result=send_response(data)
    mydata=encode_response_json(tag_result)

    return render_template('main.html', **mydata)



# ### Host Server ####
if __name__ == '__main__':
    env = sys.argv[1] if len(sys.argv) == 2 else 'prod'

    print('{0} App will be served in port:{1}....'.format(datetime.datetime.now(), host_config[env]['port']))

    print('{0} Loading models...'.format(datetime.datetime.now()))
    #preload_models()
    print('{0} Models loaded....'.format(datetime.datetime.now()))
    print('{0} Serving in port:{1}....'.format(datetime.datetime.now(), host_config[env]['port']))
    #app.run(host=host_config[env]['host'], port=host_config[env]['port'], threaded=True)
    app.run(debug=True, host='0.0.0.0')
    print('started')

import base64
import datetime
import http.client
import json
import sys
from pyblake2 import blake2b

from flask import Flask, request
from flask import render_template

from util.config import host_config

app = Flask(__name__)

AUTH_SIZE = 16

API_KEY = '<KEY>'  # use the provided one
SECRET_KEY = '2a4309a8a2c54e539e5cb57e3a4816d9'  # use the provided one; keep this one well protected


def generate(msg):
    h = blake2b(digest_size=AUTH_SIZE, key=SECRET_KEY.encode('utf-8'))
    h.update(msg.encode('utf-8'))
    return h.hexdigest()




def send_response(data):
    host = '0.0.0.0'
    port = '12892'
    path = '/api/external/v1/feedback/flair'

    headers = {"Content-type": "application/json", "charset": "utf-8"}
    conn = http.client.HTTPConnection(host, port)
    conn.request("POST", path, json.JSONEncoder().encode(data), headers)
    response = conn.getresponse()
    print(response.status, response.reason)

    tag_result = response.read()
    print(tag_result)
    return tag_result




def encode_request_json(text):
    data = {
        'text': base64.b64encode(text.encode('utf-8')).decode('utf-8'),
        'api-key': API_KEY,
        'user': '2IFQKT0_1_1',
        'case': '1',
        'proof': generate(text)
    }
    print(data)
    return data

import json
def encode_response_json(response):
    response=json.loads(response)
    data={}
    content_len=0
    reasoning_len=0
    if len(response["content"])>0:
        content_len=len(response["content"][0])
    if len(response["reasoning"])>0:
        reasoning_len=len(response["reasoning"][0])
    keys_content=["index_c","position_c","word_c","token_string_c","comment_c"]
    keys_reasoning=["index_r","position_r","word_r","token_string_c","comment_r"]

    for i in range(content_len):
        data[keys_content[i]]=" ".join(str(response["content"][0][i])) if type(response["content"][0][i]) is list else response["content"][0][i]
    for i in range(reasoning_len):
        data[keys_reasoning[i]]=" ".join(str(response["reasoning"][0][i])) if type(response["reasoning"][0][i]) is list else response["reasoning"][0][i]

    print(data)
    return data



@app.route('/predict', methods=['GET'])
def api_feedback_with_model():
    mytext = request.args.get('text', '')
    data=encode_request_json(mytext)
    tag_result=send_response(data)
    mydata=encode_response_json(tag_result)

    return render_template('main.html', **mydata)



# ### Host Server ####
if __name__ == '__main__':
    env = sys.argv[1] if len(sys.argv) == 2 else 'prod'

    print('{0} App will be served in port:{1}....'.format(datetime.datetime.now(), host_config[env]['port']))

    print('{0} Loading models...'.format(datetime.datetime.now()))
    #preload_models()
    print('{0} Models loaded....'.format(datetime.datetime.now()))
    print('{0} Serving in port:{1}....'.format(datetime.datetime.now(), host_config[env]['port']))
    #app.run(host=host_config[env]['host'], port=host_config[env]['port'], threaded=True)
    app.run(debug=True, host='0.0.0.0')
    print('started')

# use the provided one # use the provided one; keep this one well protected # ### Host Server #### #preload_models() #app.run(host=host_config[env]['host'], port=host_config[env]['port'], threaded=True)