'use strict';

angular.module('app')
	.service('jobManager', function(jobApi, $state, notify) {

    var jobManager = this;

    jobManager.currentWorkingJob = null;

    jobManager.currentProductId = null;

    jobManager.jobs = [
    {
        label: "import",
        title: "Import",
        icon: "fa-file-o",
        status: "not-ready",
        type: 'project',
        info: [],
        actions: [
            {
                name: "Configure",
                action: importConfigure
            }
        ]
    },
    {
        label: "sampling",
        title: "Sampling",
        icon: "fa-cogs",
        status: "done",
        type: 'product',
        info: [],
        actions: [

        ]
    },
    {
        label: "download",
        title: "Download",
        icon: "fa-cloud-download",
        status: "working",
        type: 'product',
        info: [],
        actions: [
            {
                name: "Start"
            }
        ]
    },
    {
        label: "translation-price",
        title: "Translation price",
        icon: "fa-eur",
        status: "ready",
        type: 'product',
        info: [],
        actions: [
            {
                name: "Start",
                action: translationPriceStart
            }
        ]
    },
    {
        label: "cleaning",
        title: "Cleaning",
        icon: "fa-compass",
        status: "error",
        type: 'product',
        info: [],
        actions: [

        ]
    },
    {
        label: "translation",
        title: "Translation",
        icon: "fa-language",
        status: "warning",
        type: 'product',
        info: [],
        actions: [

        ]
    }];

    function importConfigure() {
        $state.go('file-import');
    }

    function translationPriceStart() {
        let job = jobManager.jobs.find( (job) => job.label === 'translation-price' );
        jobManager.start(() => {
            job.status = "working";
            jobApi.submitTranslationJob(jobManager.currentProductId)
                .then((price) => {
                    job.status = "done";
                    job.info[0] = {
                        label: 'Price',
                        content: price
                    };
                    notify.show("The price is: " + price, '/img/notify.png');
                })
                .catch((err) => {
                    job.status = "error";
                })
                .finally(() => {
                    jobManager.stop();
                });
        }, job.label);
    }

    jobManager.getProjectJobs = function() {
         return jobManager.jobs.filter( (job) => job.type === 'project' );
    }

    jobManager.getProductJobs = function() {
         return jobManager.jobs.filter( (job) => job.type === 'product' );
    }

    jobManager.start = function(startFunction, label) {
        if (!jobManager.isRunning()){
            jobManager.currentWorkingJob = label;
            startFunction();
        }
    }

    jobManager.stop = function() {
        jobManager.currentWorkingJob = null;
    }

    jobManager.setCurrentProductId = function(id) {
        jobManager.currentProductId = id;
    }

    jobManager.isRunning = function() {
        if (jobManager.currentWorkingJob) {
            return true;
        } else {
            return false;
        }
    }


});



