'use strict';

var workflows = [
  { id: 0, title: 'Static forms', steps: [
    {code: 'risk-assessment', name: 'Risk Assessment', templates: {
      form: '<risk-assessment-form></risk-assessment-form>',
      view: '<risk-assessment value="workorder.steps[\'risk-assessment\'].submission"></risk-assessment>',
      portal: {
        view: '<risk-assessment-portal-view value="workorder.steps[\'risk-assessment\'].submission"></risk-assessment-portal-view>'
      }
    }},
    {code: 'vehicle-inspection', name: 'Vehicle Inspection', templates: {
      form: '<vehicle-inspection-form></vehicle-inspection-form>',
      view: '<vehicle-inspection value="workorder.steps[\'vehicle-inspection\'].submission"></vehicle-inspection>',
      portal: {
        view: '<vehicle-inspection-portal-view value="workorder.steps[\'vehicle-inspection\'].submission"></vehicle-inspection-portal-view>'
      }
    }}
  ]},

  { id: 1, title: 'App forms', steps: [
    {code: 'identification', name: 'Identification', formId: '561582e5e375d65e34dd5d8e'},
    {code: 'signoff', name: 'Signoff', formId: '5602dcbf212b52816d2ab9d8'}
  ]},

  { id: 2, title: 'Mixed forms', steps: [
    {code: 'signoff', name: 'Sign-off Workorder', formId: '5602dcbf212b52816d2ab9d8'},
    {code: 'risk-assessment', name: 'Risk Assessment', templates: {
      form: '<risk-assessment-form></risk-assessment-form>',
      view: '<risk-assessment value="workorder.steps[\'risk-assessment\'].submission"></risk-assessment>',
      portal: {
        view: '<risk-assessment-portal-view value="workorder.steps[\'risk-assessment\'].submission"></risk-assessment-portal-view>'
      }
    }},
    {code: 'vehicle-inspection', name: 'Vehicle Inspection', templates: {
      form: '<vehicle-inspection-form></vehicle-inspection-form>',
      view: '<vehicle-inspection value="workorder.steps[\'vehicle-inspection\'].submission"></vehicle-inspection>',
      portal: {
        view: '<vehicle-inspection-portal-view value="workorder.steps[\'vehicle-inspection\'].submission"></vehicle-inspection-portal-view>'
      }
    }}
  ]},
];

var steps = [

];

module.exports = function(mediator) {
  // Deprecated
  mediator.subscribe('workflow:steps:load', function() {
    setTimeout(function() {
      mediator.publish('workflow:steps:loaded', workflows[2].steps);
    }, 0);
  });

  mediator.subscribe('workflows:load', function() {
    setTimeout(function() {
      mediator.publish('done:workflows:load', workflows);
    }, 0);
  });

  mediator.subscribe('workflow:save', function(workflow) {
    setTimeout(function() {
      var index = _.findIndex(workflows, function(_workflow) {
        return _workflow.id == workflow.id;
      });
      workflows[index] = workflow;
      console.log('Saved workflow:', workflow);
      mediator.publish('workflow:saved:' + workflow.id, workflow);
    }, 0);
  });
}

@@ -249,149 +249,14 @@
 der.
-steps%5B%5C'risk-assessment%5C'%5D.submission%22%3E%3C/risk-assessment%3E',%0A      portal: %7B%0A        view: '%3Crisk-assessment-portal-view value=%22workorder.step
+result
 s%5B%5C'
@@ -303,38 +303,18 @@
 sessment
--portal-view%3E'%0A      %7D
+%3E'
 %0A    %7D%7D,
@@ -509,158 +509,14 @@
 der.
-steps%5B%5C'vehicle-inspection%5C'%5D.submission%22%3E%3C/vehicle-inspection%3E',%0A      portal: %7B%0A        view: '%3Cvehicle-inspection-portal-view value=%22workorder.step
+result
 s%5B%5C'
@@ -569,38 +569,18 @@
 spection
--portal-view%3E'%0A      %7D
+%3E'
 %0A    %7D%7D%0A
@@ -1107,149 +1107,14 @@
 der.
-steps%5B%5C'risk-assessment%5C'%5D.submission%22%3E%3C/risk-assessment%3E',%0A      portal: %7B%0A        view: '%3Crisk-assessment-portal-view value=%22workorder.step
+result
 s%5B%5C'
@@ -1161,38 +1161,18 @@
 sessment
--portal-view%3E'%0A      %7D
+%3E'
 %0A    %7D%7D,
@@ -1367,158 +1367,14 @@
 der.
-steps%5B%5C'vehicle-inspection%5C'%5D.submission%22%3E%3C/vehicle-inspection%3E',%0A      portal: %7B%0A        view: '%3Cvehicle-inspection-portal-view value=%22workorder.step
+result
 s%5B%5C'
@@ -1431,30 +1431,10 @@
 tion
--portal-view%3E'%0A      %7D
+%3E'
 %0A   

Renamed workorder.steps to workorder.results