from contextlib import closing
from os import mkdir
from os import path
from shutil import copytree
from uuid import uuid1

from flask import make_response, request, abort
from flask.views import View

from pycs.database.Database import Database
from pycs.frontend.endpoints.projects.ExecuteExternalStorage import ExecuteExternalStorage
from pycs.frontend.endpoints.projects.ExecuteLabelProvider import ExecuteLabelProvider
from pycs.frontend.notifications.NotificationManager import NotificationManager
from pycs.jobs.JobRunner import JobRunner
from pycs.util.PipelineUtil import load_from_root_folder as load_pipeline


class CreateProject(View):
    """
    create a project, insert data into database and create directories
    """
    # pylint: disable=arguments-differ
    methods = ['POST']

    def __init__(self, db: Database, nm: NotificationManager, jobs: JobRunner):
        # pylint: disable=invalid-name
        self.db = db
        self.nm = nm
        self.jobs = jobs

    def dispatch_request(self):
        # extract request data
        data = request.get_json(force=True)

        if 'name' not in data or 'description' not in data:
            return abort(400)

        name = data['name']
        description = data['description']

        # start transaction
        with self.db:
            # find model
            model_id = int(data['model'])
            model = self.db.model(model_id)

            if model is None:
                return abort(404)

            # find label provider
            if data['label'] is None:
                label_provider = None
            else:
                label_provider_id = int(data['label'])
                label_provider = self.db.label_provider(label_provider_id)

                if label_provider is None:
                    return abort(404)

            # create project folder
            project_folder = path.join('projects', str(uuid1()))
            mkdir(project_folder)

            temp_folder = path.join(project_folder, 'temp')
            mkdir(temp_folder)

            # check project data directory
            if data['external'] is None:
                external_data = False
                data_folder = path.join(project_folder, 'data')

                mkdir(data_folder)
            else:
                external_data = True
                data_folder = data['external']

                # check if exists
                if not path.exists(data_folder):
                    return abort(400)

            # copy model to project folder
            model_folder = path.join(project_folder, 'model')
            copytree(model.root_folder, model_folder)

            model, _ = model.copy_to(f'{model.name} ({name})', model_folder)

            # create entry in database
            created = self.db.create_project(name, description, model, label_provider,
                                             project_folder, external_data, data_folder)

        # execute label provider and add labels to project
        if label_provider is not None:
            ExecuteLabelProvider.execute_label_provider(self.db, self.nm, self.jobs, created,
                                                        label_provider)

        # load model and add collections to the project
        def load_model_and_get_collections():
            with closing(load_pipeline(model.root_folder)) as pipeline:
                return pipeline.collections()

        def add_collections_to_project(provided_collections):
            with self.db:
                for position, collection in enumerate(provided_collections):
                    created.create_collection(collection['reference'],
                                              collection['name'],
                                              collection['description'],
                                              position + 1,
                                              collection['autoselect'])

        self.jobs.run(created,
                      'Media Collections',
                      f'{created.name}',
                      f'{created.identifier}/media-collections',
                      executable=load_model_and_get_collections,
                      result=add_collections_to_project)

        # find media files
        if external_data:
            ExecuteExternalStorage.find_media_files(self.db, self.nm, self.jobs, created)

        # fire event
        self.nm.create_model(model)
        self.nm.create_project(created)

        # return success response
        return make_response()
