import Style from "ol/style/Style";
import Fill from "ol/style/Fill";
import Stroke from "ol/style/Stroke";
import Circle from "ol/style/Circle";
import Txt from "ol/style/Text";
import RegularShape from "ol/style/RegularShape";

window.olStyle = {
  Style: Style,
  Fill: Fill,
  Stroke: Stroke,
  Circle: Circle,
  Text: Txt,
  RegularShape: RegularShape,
};

const defaultVectorStyle = [
  new Style({
    stroke: new Stroke({
      color: "rgba(255, 255, 255, 0.9)",
      width: 4
    }),
    fill: new Fill({
      color: "rgba(255, 255, 255, 0.2)"
    })
  }),
  new Style({
    stroke: new Stroke({
      color: "rgba(0, 0, 0, 0.8)",
      lineDash: [4, 4],
      width: 2
    })
  }),
  new Style({
    image: new Circle({
      radius: 8,
      stroke: new Stroke({
        color: "rgba(0, 0, 0, 0.8)",
        width: 2
      }),
      fill: new Fill({
        color: "rgba(255, 255, 255, 1)"
      })
    })
  })
];

export default options => {
  if (options.styleFn) {
    return eval(options.styleFn);
  }

  if (options.style) {
    return options.style;
  }

  if (options.styleAttributes) {
    return (feature, resolution) => {
      const fillColor = feature.get("fillColor");
      const strokeColor = feature.get("strokeColor");
      const strokeWidth = feature.get("strokeWidth");
      const strokeDash = feature.get("strokeDash");
      const markerRadius = feature.get("markerRadius");
      const markerColor = feature.get("markerColor");
      const markerStrokeColor = feature.get("markerStrokeColor");
      const markerStrokeWidth = feature.get("markerStrokeWidth");
      const markerFillColor = feature.get("markerFillColor");

      const style = new Style();
      if (fillColor) {
        style.setFill(
          new Fill({
            color: fillColor
          })
        );
      }
      if (strokeColor || strokeWidth || strokeDash) {
        style.setStroke(
          new Stroke({
            color: strokeColor || "rgba(0, 0, 0, 0.5)",
            lineDash: strokeDash ? strokeDash.split(",") : [10, 10],
            width: strokeWidth || 2
          })
        );
      }
      if (
        markerColor ||
        markerRadius ||
        markerStrokeColor ||
        markerStrokeWidth ||
        markerFillColor
      ) {
        style.setImage(
          new Circle({
            radius: markerRadius || 3,
            stroke: new Stroke({
              color: markerStrokeColor || "rgba(0, 0, 0, 0.7)",
              width: markerStrokeWidth || 0
            }),
            fill: new Fill({
              color: markerFillColor || "rgba(255, 255, 255, 0.2)"
            })
          })
        );
      }
      return style;
    };
  }

  return defaultVectorStyle;
};

// (
//   function(f){
//     const b = feature.get('briefit');
//     const h = b === 'Y';
//     const style = new Style({
//       image: new Circle({
//         radius: h ? 7 : 5,
//         stroke: new Stroke({
//           color: '#FFFFFF',
//           width: 2
//         }),
//         fill: new Fill({
//           color: h ? '#DEA500' : '#B85B2B'
//         })
//       })
//     });
//     return style;
//   }
// )
