import os

from glob import glob

import eventlet
import socketio

from flask import send_from_directory

from pycs.database.Model import Model
from pycs.database.LabelProvider import LabelProvider
from pycs.frontend.endpoints.ListJobs import ListJobs
from pycs.frontend.endpoints.ListLabelProviders import ListLabelProviders
from pycs.frontend.endpoints.ListModels import ListModels
from pycs.frontend.endpoints.ListProjects import ListProjects
from pycs.frontend.endpoints.additional.FolderInformation import FolderInformation
from pycs.frontend.endpoints.data.GetFile import GetFile
from pycs.frontend.endpoints.data.GetPreviousAndNextFile import GetPreviousAndNextFile
from pycs.frontend.endpoints.data.GetResizedFile import GetResizedFile
from pycs.frontend.endpoints.data.RemoveFile import RemoveFile
from pycs.frontend.endpoints.data.UploadFile import UploadFile
from pycs.frontend.endpoints.jobs.RemoveJob import RemoveJob
from pycs.frontend.endpoints.labels.CreateLabel import CreateLabel
from pycs.frontend.endpoints.labels.EditLabelName import EditLabelName
from pycs.frontend.endpoints.labels.EditLabelParent import EditLabelParent
from pycs.frontend.endpoints.labels.ListLabelTree import ListLabelTree
from pycs.frontend.endpoints.labels.ListLabels import ListLabels
from pycs.frontend.endpoints.labels.RemoveLabel import RemoveLabel
from pycs.frontend.endpoints.pipelines.FitModel import FitModel
from pycs.frontend.endpoints.pipelines.PredictFile import PredictFile
from pycs.frontend.endpoints.pipelines.PredictModel import PredictModel
from pycs.frontend.endpoints.projects.CreateProject import CreateProject
from pycs.frontend.endpoints.projects.EditProjectDescription import EditProjectDescription
from pycs.frontend.endpoints.projects.EditProjectName import EditProjectName
from pycs.frontend.endpoints.projects.ExecuteExternalStorage import ExecuteExternalStorage
from pycs.frontend.endpoints.projects.ExecuteLabelProvider import ExecuteLabelProvider
from pycs.frontend.endpoints.projects.GetProjectModel import GetProjectModel
from pycs.frontend.endpoints.projects.ListProjectCollections import ListProjectCollections
from pycs.frontend.endpoints.projects.ListProjectFiles import ListProjectFiles
from pycs.frontend.endpoints.projects.RemoveProject import RemoveProject
from pycs.frontend.endpoints.results.ConfirmResult import ConfirmResult
from pycs.frontend.endpoints.results.CreateResult import CreateResult
from pycs.frontend.endpoints.results.ResultAsCrop import ResultAsCrop
from pycs.frontend.endpoints.results.EditResultData import EditResultData
from pycs.frontend.endpoints.results.EditResultLabel import EditResultLabel
from pycs.frontend.endpoints.results.GetProjectResults import GetProjectResults
from pycs.frontend.endpoints.results.GetResults import GetResults
from pycs.frontend.endpoints.results.RemoveResult import RemoveResult
from pycs.frontend.endpoints.results.ResetResults import ResetResults
from pycs.frontend.notifications.NotificationManager import NotificationManager
from pycs.frontend.util.JSONEncoder import JSONEncoder
from pycs.jobs.JobRunner import JobRunner
from pycs.util.PipelineCache import PipelineCache


class WebServer:
    """
    wrapper class for flask and socket.io which initializes most networking
    """

    def __init__(self, app, settings: dict, discovery: bool = True):

        is_production = os.path.exists('webui/index.html')

        # initialize web server
        if is_production:
            print('production build')

            # find static files and folders
            static_files = {}

            for file_path in glob('webui/*'):
                file_path = file_path.replace('\\', '/')
                static_files[file_path[5:]] = file_path

            # separately add svg files and set their correct mime type
            for svg_path in glob('webui/img/*.svg'):
                svg_path = svg_path.replace('\\', '/')
                static_files[svg_path[5:]] = {'content_type': 'image/svg+xml', 'filename': svg_path}

            # create service objects
            if len(settings['allowedOrigins']) > 0:
                origins = settings['allowedOrigins']
                self.__sio = socketio.Server(cors_allowed_origins=origins, async_mode='eventlet')
            else:
                self.__sio = socketio.Server(async_mode='eventlet')

            self.wsgi_app = socketio.WSGIApp(self.__sio, app, static_files=static_files)

            # overwrite root path to serve index.html
            @app.route('/', methods=['GET'])
            def index():
                # pylint: disable=unused-variable
                return send_from_directory(os.path.join(os.getcwd(), 'webui'), 'index.html')

        else:
            print('development build')

            # create service objects
            self.__sio = socketio.Server(cors_allowed_origins='*', async_mode='eventlet')
            self.app = app
            self.wsgi_app = socketio.WSGIApp(self.__sio, app)

            # set access control header to allow requests from Vue.js development server
            @self.app.after_request
            def after_request(response):
                # pylint: disable=unused-variable
                response.headers['Access-Control-Allow-Origin'] = '*'
                return response

        # set json encoder so database objects are serialized correctly
        self.app.json_encoder = JSONEncoder

        self.host = settings.host
        self.port = settings.port

        # create notification manager
        jobs = JobRunner()
        pipelines = PipelineCache(jobs)
        notifications = NotificationManager(self.__sio)

        jobs.on_create(notifications.create_job)
        jobs.on_start(notifications.edit_job)
        jobs.on_progress(notifications.edit_job)
        jobs.on_finish(notifications.edit_job)
        jobs.on_remove(notifications.remove_job)

        self.define_routes(jobs, notifications, pipelines)

        if discovery:
            Model.discover("models/")
            LabelProvider.discover("labels/")


    def define_routes(self, jobs, notifications, pipelines):
        """ defines app routes """

        # additional
        self.app.add_url_rule(
            '/folder',
            view_func=FolderInformation.as_view('folder_information')
        )

        # jobs
        self.app.add_url_rule(
            '/jobs',
            view_func=ListJobs.as_view('list_jobs', jobs)
        )
        self.app.add_url_rule(
            '/jobs/<int:job_id>/remove',
            view_func=RemoveJob.as_view('remove_job', jobs)
        )

        # models
        self.app.add_url_rule(
            '/models',
            view_func=ListModels.as_view('list_models')
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/model',
            view_func=GetProjectModel.as_view('get_project_model')
        )

        # labels
        self.app.add_url_rule(
            '/label_providers',
            view_func=ListLabelProviders.as_view('label_providers')
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/labels',
            view_func=ListLabels.as_view('list_labels')
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/labels/tree',
            view_func=ListLabelTree.as_view('list_label_tree')
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/labels',
            view_func=CreateLabel.as_view('create_label', notifications)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/labels/<int:label_id>/remove',
            view_func=RemoveLabel.as_view('remove_label', notifications)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/labels/<int:label_id>/name',
            view_func=EditLabelName.as_view('edit_label_name', notifications)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/labels/<int:label_id>/parent',
            view_func=EditLabelParent.as_view('edit_label_parent', notifications)
        )

        # collections
        self.app.add_url_rule(
            '/projects/<int:project_id>/collections',
            view_func=ListProjectCollections.as_view('list_collections')
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/data/<int:collection_id>/<int:start>/<int:length>',
            view_func=ListProjectFiles.as_view('list_collection_files')
        )

        # data
        self.app.add_url_rule(
            '/projects/<int:project_id>/data',
            view_func=UploadFile.as_view('upload_file', notifications)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/data/<int:start>/<int:length>',
            view_func=ListProjectFiles.as_view('list_files')
        )
        self.app.add_url_rule(
            '/data/<int:file_id>/remove',
            view_func=RemoveFile.as_view('remove_file', notifications)
        )
        self.app.add_url_rule(
            '/data/<int:file_id>',
            view_func=GetFile.as_view('get_file')
        )
        self.app.add_url_rule(
            '/data/<int:file_id>/<resolution>',
            view_func=GetResizedFile.as_view('get_resized_file')
        )
        self.app.add_url_rule(
            '/data/<int:file_id>/previous_next',
            view_func=GetPreviousAndNextFile.as_view('get_previous_and_next_file')
        )

        # results
        self.app.add_url_rule(
            '/projects/<int:project_id>/results',
            view_func=GetProjectResults.as_view('get_project_results')
        )
        self.app.add_url_rule(
            '/data/<int:file_id>/results',
            view_func=GetResults.as_view('get_results')
        )
        self.app.add_url_rule(
            '/data/<int:file_id>/results',
            view_func=CreateResult.as_view('create_result', notifications)
        )
        self.app.add_url_rule(
            '/data/<int:file_id>/reset',
            view_func=ResetResults.as_view('reset_results', notifications)
        )
        self.app.add_url_rule(
            '/results/<int:result_id>/remove',
            view_func=RemoveResult.as_view('remove_result', notifications)
        )
        self.app.add_url_rule(
            '/results/<int:result_id>/confirm',
            view_func=ConfirmResult.as_view('confirm_result', notifications)
        )
        self.app.add_url_rule(
            '/results/<int:result_id>/crop',
            view_func=ResultAsCrop.as_view('crop_result')
        )
        self.app.add_url_rule(
            '/results/<int:result_id>/crop/<int:max_width>',
            view_func=ResultAsCrop.as_view('crop_result_resized_by_width')
        )
        self.app.add_url_rule(
            '/results/<int:result_id>/crop/<int:max_width>x<int:max_height>',
            view_func=ResultAsCrop.as_view('crop_result_resized')
        )
        self.app.add_url_rule(
            '/results/<int:result_id>/label',
            view_func=EditResultLabel.as_view('edit_result_label', notifications)
        )
        self.app.add_url_rule(
            '/results/<int:result_id>/data',
            view_func=EditResultData.as_view('edit_result_data', notifications)
        )

        # projects
        self.app.add_url_rule(
            '/projects',
            view_func=ListProjects.as_view('list_projects')
        )
        self.app.add_url_rule(
            '/projects',
            view_func=CreateProject.as_view('create_project', notifications, jobs)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/label_provider',
            view_func=ExecuteLabelProvider.as_view('execute_label_provider', notifications, jobs)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/external_storage',
            view_func=ExecuteExternalStorage.as_view('execute_external_storage',
                                                     notifications, jobs)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/remove',
            view_func=RemoveProject.as_view('remove_project', notifications)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/name',
            view_func=EditProjectName.as_view('edit_project_name', notifications)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/description',
            view_func=EditProjectDescription.as_view('edit_project_description', notifications)
        )

        # pipelines
        self.app.add_url_rule(
            '/projects/<int:project_id>/pipelines/fit',
            view_func=FitModel.as_view('fit_model', jobs, pipelines)
        )
        self.app.add_url_rule(
            '/projects/<int:project_id>/pipelines/predict',
            view_func=PredictModel.as_view('predict_model', notifications, jobs,
                                           pipelines)
        )
        self.app.add_url_rule(
            '/data/<int:file_id>/predict',
            view_func=PredictFile.as_view('predict_file', notifications, jobs, pipelines)
        )

    def run(self):
        """ start web server """
        eventlet.wsgi.server(eventlet.listen((self.host, self.port)), self.wsgi_app)
