var lgnd = require('../scene/legend');

function parseLegends(model, spec, legends, group) {
  (spec || []).forEach(function(def, index) {
    legends[index] = legends[index] || lgnd(model);
    parseLegend(def, index, legends[index], group);
  });
}

function parseLegend(def, index, legend, group) {
  // legend scales
  legend.size  (def.size   ? group.scale(def.size)   : null);
  legend.shape (def.shape  ? group.scale(def.shape)  : null);
  legend.fill  (def.fill   ? group.scale(def.fill)   : null);
  legend.stroke(def.stroke ? group.scale(def.stroke) : null);

  // legend orientation
  if (def.orient) legend.orient(def.orient);

  // legend offset
  if (def.offset != null) legend.offset(def.offset);

  // legend title
  legend.title(def.title || null);

  // legend values
  legend.values(def.values || null);

  // legend label formatting
  legend.format(def.format !== undefined ? def.format : null);

  // style properties
  var p = def.properties;
  legend.titleProperties(p && p.title || {});
  legend.labelProperties(p && p.labels || {});
  legend.legendProperties(p && p.legend || {});
  legend.symbolProperties(p && p.symbols || {});
  legend.gradientProperties(p && p.gradient || {});
}

module.exports = parseLegends;

parseLegends.schema = {
  "defs": {
    "legend": {
      "type": "object",
      "properties": {
        "size": {"type": "string"},
        "shape": {"type": "string"},
        "fill": {"type": "string"},
        "stroke": {"type": "string"},
        "orient": {"enum": ["left", "right"], "default": "right"},
        "offset": {"type": "number"},
        "title": {"type": "string"},
        "values": {"type": "array"},
        "format": {"type": "string"},
        "properties": {
          "type": "object",
          "properties": {
            "title": {"$ref": "#/defs/propset"},
            "labels": {"$ref": "#/defs/propset"},
            "legend": {"$ref": "#/defs/propset"},
            "symbols": {"$ref": "#/defs/propset"},
            "gradient": {"$ref": "#/defs/propset"}
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false,
      "anyOf": [
        {"required": "size"},
        {"required": "shape"},
        {"required": "fill"},
        {"required": "stroke"}
      ]
    }
  }
};

@@ -2173,22 +2173,24 @@
 uired%22: 
+%5B
 %22size%22
+%5D
 %7D,%0A     
@@ -2205,23 +2205,25 @@
 uired%22: 
+%5B
 %22shape%22
+%5D
 %7D,%0A     
@@ -2238,22 +2238,24 @@
 uired%22: 
+%5B
 %22fill%22
+%5D
 %7D,%0A     
@@ -2270,24 +2270,25 @@
 uired%22: 
+%5B
 %22stroke%22
 %7D%0A      
@@ -2279,16 +2279,17 @@
 %22stroke%22
+%5D
 %7D%0A      

Fix "required' schema to take an array