from FlaskApp.app import app
#import time
import sys
import threading
from flask import request, jsonify
import FlaskApp.app.common as common
import FlaskApp.app.uphance_webhook_info as uphance_webhook
import FlaskApp.app.cross_docks_polling as cross_docks_polling
import FlaskApp.app.data_store as data_store
import urllib.parse


@app.route("/")
def homepage():
    common.logger.debug('root of domain reached displaying : Nothing to see here')
    return "Nothing to see here"

@app.route('/test',methods=['POST','GET'])
def test():
    #args = None
    content = request.get_json(silent=True)
    #common.logger.info(str(request.url))
    if content:
        common.send_email(0,'Test Message',str(content),'gary@mclarenwilliams.com.au')
    else:
        common.send_email(0,'Test Message','No content','gary@mclarenwilliams.com.au')

    return 'Test Processed - check email'

@app.route('/uphance',methods=['POST','GET'])
def uphance():
    #args = None
    if common.uphance_initiate('aemery'):
        #common.send_email(0,'Uphance initiated successfully',str(content),'gary@mclarenwilliams.com.au')
        return 'Uphance initiated successfully'
    else:
        #common.send_email(0,'Uphance not initiated','No content','gary@mclarenwilliams.com.au')
        return 'Uphance not initiated'


@app.route('/aemery',methods=['POST','GET'])
def process_aemery_webhook():
    content = request.get_json(silent=True)
    #common.logger.info(str(request.url))
    if content:
        status_code = uphance_webhook.uphance_prod_webhook('aemery',content)
        return 'Processed', status_code
    else :
        return 'amery - No content'

@app.route('/aemery_cross-docks-polling',methods=['POST','GET'])
def process_aemery_cross_docks_polling():

    status_code = cross_docks_polling.cross_docks_poll_request('aemery')
    
    return 'Processed', status_code

@app.route('/two-ts',methods=['POST','GET'])
def process_two_ts_webhook():
    '''
    #active code
    content = request.get_json(silent=True)
    if content:
        status_code = uphance_webhook.uphance_prod_webhook('two-ts',content)
        return 'Processed', status_code
    else :
        return 'two-ts No content'
    #end active code
    '''
    return 'in sleep mode'

@app.route('/two-ts_cross-docks-polling',methods=['POST','GET'])
def process_two_ts_cross_docks_polling():
    '''#active code
    
    status_code = cross_docks_polling.cross_docks_poll_request('two-ts')
    
    return 'Processed', status_code
    #end active code
    '''
    return 'in sleep mode'

@app.route('/aemery_get_product_data',methods=['GET','POST'])
def process_aemery_get_data_store_info():
    #use threading so that no timeout occurs on POST
    x = threading.Thread(target = data_store.get_data_store_info, args=('aemery',))
    x.start()
    return 'Processed using thread'


@app.route('/aemery_get_master_IT_file',methods=['GET','POST'])
def process_aemery_get_master_IT_file():
    #use threading so that no timeout occurs on POST
    x = threading.Thread(target = data_store.get_master_IT_file, args=('aemery',))
    x.start()
    return 'Processed using thread'

@app.route('/two-ts_get_master_IT_file',methods=['GET','POST'])
def process_two_ts_get_master_IT_file():
    #use threading so that no timeout occurs on POST
    #active code
    
    x = threading.Thread(target = data_store.get_master_IT_file, args=('two-ts',))
    x.start()
    return 'Processed using thread'
    #end active code
    #return 'in sleep mode'

@app.route('/vpn-log',methods=['GET','POST'])
def process_vpn_info():
    common.logger.debug(str(request))
    content = request.query_string.decode('utf-8',errors='replace')
    content_json = request.get_json(silent=True)
    raw_body = request.data.decode("utf-8")
    all_headers = dict(request.headers)
    encoded_data = request.args.get('data')
    if encoded_data:
        encoded_data = urllib.parse.unquote(encoded_data)
    common.logger.debug('Raw Body: ' + raw_body)
    common.logger.debug('All Headers: ' + str(all_headers))
    common.logger.debug('JSON: ' + str(content_json))
    common.logger.debug('Query String: ' + str(content))
    common.logger.debug('Decoded Data: ' + str(encoded_data))
    if content:
        common.send_email(0,'VPN Info','VPN Query String :\n' + str(content) + '\n\nVPN JSON: \n' + str(content_json) + '\n\nHeaders:\n' + str(all_headers) + '\n\nDecoded Data:\n' + str(encoded_data),'gary@mclarenwilliams.com.au')
        return 'VPN Info Processed - Email sent'
    else :
        return 'VPN Info - No content'
