from app import db, logger
from app.models import Corpus, CorpusFile
from flask import (abort, current_app, flash, redirect, request,
                   render_template, url_for, send_from_directory)
from flask_login import current_user, login_required
from threading import Thread
from werkzeug.utils import secure_filename
from . import corpora
from .background_functions import (delete_corpus_, delete_corpus_file_,
                                   edit_corpus_file_)
from .forms import (AddCorpusFileForm, AddCorpusForm, EditCorpusFileForm,
                    QueryDownloadForm, QueryForm)
import os


@corpora.route('/add', methods=['GET', 'POST'])
@login_required
def add_corpus():
    add_corpus_form = AddCorpusForm()
    if add_corpus_form.validate_on_submit():
        corpus = Corpus(creator=current_user,
                        description=add_corpus_form.description.data,
                        status='unprepared', title=add_corpus_form.title.data)
        db.session.add(corpus)
        db.session.commit()
        dir = os.path.join(current_app.config['NOPAQUE_STORAGE'],
                           str(corpus.user_id), 'corpora', str(corpus.id))
        try:
            os.makedirs(dir)
        except OSError:
            flash('[ERROR]: Could not add corpus!')
            corpus.delete()
        else:
            flash('Corpus added!')
            return redirect(url_for('corpora.corpus', corpus_id=corpus.id))
    return render_template('corpora/add_corpus.html.j2',
                           add_corpus_form=add_corpus_form,
                           title='Add corpus')


@corpora.route('/<int:corpus_id>')
@login_required
def corpus(corpus_id):
    corpus = Corpus.query.get_or_404(corpus_id)
    if not (corpus.creator == current_user or current_user.is_administrator()):
        abort(403)
    return render_template('corpora/corpus.html.j2', corpus=corpus,
                           title='Corpus')


@corpora.route('/<int:corpus_id>/analyse')
@login_required
def analyse_corpus(corpus_id):
    corpus = Corpus.query.get_or_404(corpus_id)
    if corpus.status == 'prepared':
        corpus.status = 'start analysis'
        db.session.commit()
    query_download_form = QueryDownloadForm()
    query_form = QueryForm(context=request.args.get('context', 10),
                           hits_per_page=request.args.get('hits_per_page', 30),
                           query=request.args.get('query'))
    return render_template('corpora/analyse_corpus.html.j2',
                           corpus_id=corpus_id,
                           query_download_form=query_download_form,
                           query_form=query_form, title='Corpus analysis')


@corpora.route('/<int:corpus_id>/delete')
@login_required
def delete_corpus(corpus_id):
    corpus = Corpus.query.get_or_404(corpus_id)
    if not (corpus.creator == current_user or current_user.is_administrator()):
        abort(403)
    thread = Thread(target=delete_corpus_,
                    args=(current_app._get_current_object(), corpus.id))
    thread.start()
    flash('Corpus deleted!')
    return redirect(url_for('main.dashboard'))


@corpora.route('/<int:corpus_id>/files/add', methods=['GET', 'POST'])
@login_required
def add_corpus_file(corpus_id):
    corpus = Corpus.query.get_or_404(corpus_id)
    if not (corpus.creator == current_user or current_user.is_administrator()):
        abort(403)
    add_corpus_file_form = AddCorpusFileForm()
    if add_corpus_file_form.validate_on_submit():
        file = add_corpus_file_form.file.data
        filename = secure_filename(file.filename)
        for corpus_file in corpus.files:
            if filename == corpus_file.filename:
                flash('File already registered to this corpus.')
                return redirect(url_for('corpora.add_corpus_file',
                                        corpus_id=corpus_id))
        # Save the file
        dir = os.path.join(str(corpus.user_id), 'corpora', str(corpus.id))
        file.save(os.path.join(current_app.config['NOPAQUE_STORAGE'],
                               dir, filename))
        ids = [field.id for field in add_corpus_file_form if not
               (field.id == 'submit'
                or field.id == "csrf_token"
                or field.id == "file")]
        data = [field.data for field in add_corpus_file_form if not
                (field.id == 'submit'
                 or field.id == "csrf_token"
                 or field.id == "file")]
        field_dict = dict(zip(ids, data))
        corpus_file = CorpusFile(**field_dict,
                                 corpus=corpus,
                                 dir=dir,
                                 filename=filename)
        db.session.add(corpus_file)
        db.session.commit()
        thread = Thread(target=edit_corpus_file_,
                        args=(current_app._get_current_object(),
                              corpus_file.id))
        thread.start()
        flash('Corpus file added!')
        return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
    return render_template('corpora/add_corpus_file.html.j2',
                           add_corpus_file_form=add_corpus_file_form,
                           corpus=corpus, title='Add corpus file')


@corpora.route('/<int:corpus_id>/files/<int:corpus_file_id>/delete')
@login_required
def delete_corpus_file(corpus_id, corpus_file_id):
    corpus_file = CorpusFile.query.get_or_404(corpus_file_id)
    if not corpus_file.corpus_id == corpus_id:
        abort(404)
    if not (corpus_file.corpus.creator == current_user
            or current_user.is_administrator()):
        abort(403)
    thread = Thread(target=delete_corpus_file_,
                    args=(current_app._get_current_object(), corpus_file.id))
    thread.start()
    flash('Corpus file deleted!')
    return redirect(url_for('corpora.corpus', corpus_id=corpus_id))


@corpora.route('/<int:corpus_id>/files/<int:corpus_file_id>/download')
@login_required
def download_corpus_file(corpus_id, corpus_file_id):
    corpus_file = CorpusFile.query.get_or_404(corpus_file_id)
    if not corpus_file.corpus_id == corpus_id:
        abort(404)
    if not (corpus_file.corpus.creator == current_user
            or current_user.is_administrator()):
        abort(403)
    dir = os.path.join(current_app.config['NOPAQUE_STORAGE'],
                       corpus_file.dir)
    return send_from_directory(as_attachment=True, directory=dir,
                               filename=corpus_file.filename)


@corpora.route('/<int:corpus_id>/files/<int:corpus_file_id>/edit',
               methods=['GET', 'POST'])
@login_required
def edit_corpus_file(corpus_id, corpus_file_id):
    corpus_file = CorpusFile.query.get_or_404(corpus_file_id)
    if not corpus_file.corpus_id == corpus_id:
        abort(404)
    if not (corpus_file.corpus.creator == current_user
            or current_user.is_administrator()):
        abort(403)
    edit_corpus_file_form = EditCorpusFileForm()
    if edit_corpus_file_form.validate_on_submit():
        ids = [field.id for field in edit_corpus_file_form if not
               (field.id == 'submit'
                or field.id == "csrf_token"
                or field.id == "file")]
        data = [field.data for field in edit_corpus_file_form if not
                (field.id == 'submit'
                 or field.id == "csrf_token"
                 or field.id == "file")]
        field_dict = dict(zip(ids, data))
        stmt = db.update(CorpusFile).where(CorpusFile.id==corpus_file_id).values(**field_dict)
        db.session.execute(stmt)
        db.session.commit()
        thread = Thread(target=edit_corpus_file_,
                        args=(current_app._get_current_object(),
                              corpus_file.id))
        thread.start()
        flash('Corpus file edited!')
        return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
    return render_template('corpora/edit_corpus_file.html.j2',
                           edit_corpus_file_form=edit_corpus_file_form,
                           corpus_file=corpus_file, title='Edit corpus file')


@corpora.route('/<int:corpus_id>/prepare')
@login_required
def prepare_corpus(corpus_id):
    corpus = Corpus.query.get_or_404(corpus_id)
    if not (corpus.creator == current_user or current_user.is_administrator()):
        abort(403)
    if len(corpus.files.all()) > 0:
        corpus.status = 'submitted'
        db.session.commit()
        flash('Corpus marked for preparation!')
    else:
        flash('Can not prepare corpus, please add corpus file(s).')
    return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
