/*
 * Copyright (C) 2018 Robert Bosch GmbH Copyright (C) 2018 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Injectable } from '@angular/core';
import CircleStyle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style, { Options as StyleOptions } from 'ol/style/Style';
import Text, { Options as TextOptions } from 'ol/style/Text';

@Injectable({
  providedIn: 'root',
})
export class OpenLayersStyleFactory {
  //// STYLES

  static textStyle(options?: TextOptions) {
    return new Text(options);
  }

  static featureStyle(strokeColor, pointColor, fillColor) {
    return new Style({
      stroke: new Stroke({
        color: strokeColor,
        width: 3,
      }),
      fill: new Fill({
        color: fillColor,
      }),
      image: new CircleStyle({
        radius: 7,
        stroke: new Stroke({
          color: pointColor,
        }),
      }),
      text: OpenLayersStyleFactory.textStyle({
        font: 'normal 15px "Bosch-Sans", "Arial", "sans-serif"',
        placement: 'point',
        textAlign: 'left',
        offsetY: 0,
        offsetX: 12,
        overflow: true,
        text: '',
        fill: new Fill({
          color: strokeColor,
        }),
        stroke: new Stroke({
          color: '#ffffff',
          width: 2,
        }),
      }),
    });
  }

  //// MARKERS

  static markerOptions(backgroundColor, markerTextOptions, sizeInPx = 40): StyleOptions {
    return {
      image: new CircleStyle({
        fill: new Fill({ color: backgroundColor }),
        radius: sizeInPx / 2,
      }),
      text: new Text(markerTextOptions),
    };
  }

  //// TEXT OPTIONS

  static boschFontIconOptions(
    fontCharacter,
    color,
    rotateWithView = false,
    sizeInPx = 24,
    fontName = 'Bosch-Ic'
  ): TextOptions {
    return OpenLayersStyleFactory.fontIconOptions(fontCharacter, color, rotateWithView, sizeInPx, fontName);
  }

  static fontIconOptions(text, color, rotateWithView = false, sizeInPx = 24, fontName = 'Bosch-Sans'): TextOptions {
    return {
      ...OpenLayersStyleFactory.textOptions(text, color, sizeInPx, fontName),
      placement: 'point',
      rotateWithView: rotateWithView,
    };
  }

  static textOptions(text, color, sizeInPx = 16, fontName = 'Bosch-Sans'): TextOptions {
    return {
      text: text,
      font: 'normal ' + sizeInPx + 'px "' + fontName + '"',
      fill: new Fill({
        color: color,
      }),
      placement: 'line',
    };
  }
}
