
var inherits = require('inherits');
var getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject;
import BpmnRenderer from 'bpmn-js/lib/draw/BpmnRenderer';
//import BaseRenderer from 'diagram-js/lib/draw/BaseRenderer';
var svgAttr = require('tiny-svg/lib/attr');


export default function ColorRenderer(injector, eventBus) {
  // set higher priority then a default 1000 for an existing BpmnRenderer
  var callPriority = 2000;

  //
  //BaseRenderer.call(this, eventBus, 2000);
  injector.invoke(BpmnRenderer, this);
  var self = this;

  eventBus.on([ 'render.shape' ], callPriority, function(evt, context) {
    var element = context.element,
        visuals = context.gfx;

    // call default implementation
    var shape = self.drawShape(visuals, element);
    // 2D shape with default white color
    var businessObject = getBusinessObject(element);
    if(businessObject.$type == "bpmn:SequenceFlow"){
      svgAttr(shape, {
        fill: getBackgroundColor(element) || '#000000'
      });
    }else{
      svgAttr(shape, {
        fill: getBackgroundColor(element) || '#ffffff'
      });
    }
    // make sure default renderer is not called anymore
    return shape;
  });

  eventBus.on([ 'render.connection' ], callPriority, function(evt, context) {
    var element = context.element,
        visuals = context.gfx;

    // call default implementation
    var shape = self.drawConnection(visuals, element);

    // line shape with default black color
    svgAttr(shape, {
      stroke: getBackgroundColor(element) || '#000000'
    });

    // make sure default renderer is not called anymore
    return shape;
  });
}

inherits(ColorRenderer, BpmnRenderer);
ColorRenderer.$inject = [ 'injector', 'eventBus' ];
//module.exports = ColorRenderer;




function getBackgroundColor(element) {
  var bo = getBusinessObject(element);
  return bo.di.get('color:background-color');
}
