import { removeById } from '../../../../../../util/util';

angular.module('materialscommons').component('mcTaskTemplateOther', {
    templateUrl: 'app/project/experiments/experiment/components/tasks/components/mc-task-template-other.html',
    controller: MCTaskTemplateOtherComponentController,
    bindings: {
        task: '='
    }
});

/*@ngInject*/
function MCTaskTemplateOtherComponentController(processSelections, createProcess, toastr,
                                                previousStateService, $state, sampleLinker, processEdit,
                                                $stateParams) {
    var ctrl = this;
    ctrl.remove = removeById;
    ctrl.chooseSamples = _.partial(processSelections.selectSamples, ctrl.task.template.input_samples);
    ctrl.chooseInputFiles = _.partial(processSelections.selectFiles, ctrl.task.template.input_files);
    ctrl.chooseOutputFiles = _.partial(processSelections.selectFiles, ctrl.task.template.output_files);

    ctrl.linkFilesToSample = linkFilesToSample;

    ctrl.submit = submit;
    ctrl.submitAndAnother = submitAndAnother;
    //ctrl.cancel = _.partial(previousStateService.go, 'process_create_previous');

    // previousStateService.setMemo('process_create_previous', 'project.processes.create');

    function submitAndAnother() {
        var go = _.partial($state.go, 'project.processes.create', {
            template_id: ctrl.task.template.process_name,
            process_id: ''
        });
        performSubmit(go);
    }

    function submit() {
        var go = _.partial(previousStateService.go, 'process_create_previous');
        performSubmit(go);
    }

    function performSubmit(goFn) {
        createProcess($stateParams.project_id, ctrl.process)
            .then(
                function success() {
                    goFn();
                },

                function failure() {
                    toastr.error('Unable to create sample', 'Error', {closeButton: true});
                }
            );
    }

    function linkFilesToSample(sample, input_files, output_files) {
        sampleLinker.linkFilesToSample(sample, input_files, output_files).then(function(linkedFiles) {
            sample = processEdit.refreshSample(linkedFiles, sample);
        });
    }
}
angular.module('materialscommons').component('mcTaskTemplateOther', {
    templateUrl: 'app/project/experiments/experiment/components/tasks/components/mc-task-template-other.html',
    controller: MCTaskTemplateOtherComponentController,
    bindings: {
        task: '='
    }
});

/*@ngInject*/
function MCTaskTemplateOtherComponentController(sampleLinker, processEdit) {
    var ctrl = this;
    ctrl.linkFilesToSample = linkFilesToSample;

    function linkFilesToSample(sample, input_files, output_files) {
        sampleLinker.linkFilesToSample(sample, input_files, output_files).then(function(linkedFiles) {
            sample = processEdit.refreshSample(linkedFiles, sample);
        });
    }
}
Remove unused code.