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

import { Injectable } from '@angular/core';
import { BOSCH_DDS_COLORS } from '@bci-web-core/core';
import { LocationStyleCustomized, LocationStyleService } from '@bci-web-core/maps';
import { Style } from 'ol/style';
import { DEVICE_PIN_STYLE, FACILITY_STYLE, FACILITY_TYPE } from './location-layers/demo-location-layers.mocks';

@Injectable()
export class CustomLocationStylesService extends LocationStyleService {
  constructor() {
    super();

    this.locationStyles.push(
      this.createStyle('plant', this.createPlainLocationStyle(BOSCH_DDS_COLORS.boschGray80, 10, 0.01), 50),
      this.createStyle('building', this.createPlainLocationStyle(BOSCH_DDS_COLORS.boschBlue75, 15, 0.02), 1),
      this.createStyle('floor', this.createPlainLocationStyle(BOSCH_DDS_COLORS.boschBlue35, 20, 0.005), 0.5),
      this.createStyle('room', this.createPlainLocationStyle(BOSCH_DDS_COLORS.boschGreen50, 50, 0.02), 0.2),

      this.createStyle(FACILITY_TYPE.enterprise, FACILITY_STYLE, 50),
      this.createStyle(FACILITY_TYPE.site, FACILITY_STYLE, 1),
      this.createStyle(FACILITY_TYPE.area, FACILITY_STYLE, 0.2),
      this.createStyle(FACILITY_TYPE.device, DEVICE_PIN_STYLE)
    );
  }

  private createStyle(type: string, styleInstance: Style, resolutionLimitInKm = 9999999999) {
    return new LocationStyleCustomized(styleInstance, {
      applies: (location, metadata) => location.type === type && !metadata.isSelected,
      isMarker: () => type === FACILITY_TYPE.device,
      showLabel: (feature, resolution) => {
        return resolution < resolutionLimitInKm;
      },
    });
  }
}
