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

import Draw, { DrawEvent, Options as DrawOptions } from 'ol/interaction/Draw';
import { Observable, Subject } from 'rxjs';
import { InteractionMode } from '../interaction-mode';

export class DrawInteractionMode extends InteractionMode {
  readonly drawInteraction: Draw;

  private _creation = new Subject<DrawEvent>();
  readonly created: Observable<DrawEvent> = this._creation.asObservable();

  constructor(drawOptions: DrawOptions) {
    super();
    this.drawInteraction = new Draw({
      condition: () => true,
      ...drawOptions,
    });
  }

  get isEnabled(): boolean {
    return this.drawInteraction.getActive();
  }

  set isEnabled(enabled: boolean) {
    this.drawInteraction.setActive(enabled);
  }

  activate() {
    this.activateInteraction(this.drawInteraction);
    this.registerListener(
      this.drawInteraction.on('drawend', (event: DrawEvent) => {
        this._creation.next(event);
      })
    );
    super.activate();
  }
}
