from app import db, logger, socketio
from app.events import connected_sessions
from app.models import Corpus, User
from flask import current_app, request
from flask_login import current_user, login_required
from .CQiWrapper.CQiWrapper import CQiWrapper

'''
' A dictionary containing lists of, with corpus ids associated, Socket.IO
' session ids (sid). {<corpus_id>: [<sid>, ...], ...}
'''
corpus_analysis_sessions = {}
'''
' A dictionary containing Socket.IO session id - CQi client pairs.
' {<sid>: CQiClient, ...}
'''
corpus_analysis_clients = {}


@socketio.on('corpus_analysis_init')
@login_required
def init_corpus_analysis(corpus_id):
    logger.warning("Initiating corpus analysis.")
    socketio.start_background_task(corpus_analysis_session_handler,
                                   current_app._get_current_object(),
                                   corpus_id, current_user.id, request.sid)


@socketio.on('corpus_analysis')
@login_required
def corpus_analysis(message):
    client = corpus_analysis_clients.get(request.sid)
    if client is None:
        socketio.emit('query', '[424]: Failed Dependency',
                      room=request.sid)
        return
    # Prepare and execute a query
    corpus_name = 'CORPUS'
    query = str(message['query'])
    result_len = 200  # int(message['hits_per_page'])
    context_len = int(message['context'])
    result_offset = 0
    client.select_corpus(corpus_name)
    try:
        client.query_subcorpus(query)
    except Exception as e:
        logger.warning(e)
        response = str(e)
        if response == "CQI_CQP_ERROR_GENERAL":
            response = {'code': 1,
                        'result': {'matches': [],
                                   'match_count': 0,
                                   'cpos_lookup': {},
                                   'text_lookup': {}}
                            }
            socketio.emit('corpus_analysis_query', response, room=request.sid)
    else:
        logger.warning('====== Initial query {} ======'.format(query))
        response = client.show_query_results(result_len=result_len,
                                             context_len=context_len,
                                             result_offset=result_offset)
        result_offset += result_len  # initial offset is plus result len because client.show_query_results has already been executed once
        socketio.emit('corpus_analysis_query', response, room=request.sid)
        while result_offset < client.match_count:
            logger.warning('====== While loop start for {} ======'.format(query))
            logger.warning('result_offset: {}'.format(result_offset))
            response = client.show_query_results(result_len=result_len,
                                                 context_len=context_len,
                                                 result_offset=result_offset)
            result_offset += result_len
            result_offset = min(result_offset, client.match_count)
            logger.warning('result_offset end of while loop: {}'.format(result_offset))
            socketio.emit('corpus_analysis_query', response, room=request.sid)


@socketio.on('inspect_match')
@login_required
def inspect_match(message):
    client = corpus_analysis_clients.get(request.sid)
    if client is None:
        socketio.emit('query', '[424]: Failed Dependency',
                      room=request.sid)
        return
    # Get more context for given match CPOS
    match_context = client.get_sentences(message['cpos'],
                                         get_surrounding_s=True,
                                         l_r_s_context_additional_len=3)
    socketio.emit('match_context', match_context, room=request.sid)


def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
    with app.app_context():
        ''' Setup analysis session '''
        corpus = Corpus.query.get(corpus_id)
        user = User.query.get(user_id)
        if corpus is None:
            logger.warning("404")
            response = {'code': 404, 'msg': 'Not Found'}
            socketio.emit('corpus_analysis_init', response, room=session_id)
            return
        elif not (corpus.creator == user or user.is_administrator()):
            logger.warning("403")
            response = {'code': 403, 'msg': 'Forbidden'}
            socketio.emit('corpus_analysis_init', response, room=session_id)
            return
        while corpus.status != 'analysing':
            db.session.refresh(corpus)
            socketio.sleep(3)
        client = CQiWrapper(host='corpus_{}_analysis'.format(corpus_id))
        client.connect()
        corpus_analysis_clients[session_id] = client
        if corpus_id not in corpus_analysis_sessions:
            corpus_analysis_sessions[corpus_id] = [session_id]
        else:
            corpus_analysis_sessions[corpus_id].append(session_id)
        response = {'code': 201, 'msg': 'Created'}
        socketio.emit('corpus_analysis_init', response, room=session_id)
        ''' Observe analysis session '''
        while session_id in connected_sessions:
            socketio.sleep(3)
        ''' Teardown analysis session '''
        client.disconnect()
        corpus_analysis_clients.pop(session_id, None)
        corpus_analysis_sessions[corpus_id].remove(session_id)
        if not corpus_analysis_sessions[corpus_id]:
            corpus_analysis_sessions.pop(corpus_id, None)
            corpus.status = 'stop analysis'
            db.session.commit()
