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

import {
  ComponentFactoryResolver,
  Inject,
  Injector,
  NgZone,
  Renderer2,
  RendererFactory2,
  Injectable,
} from '@angular/core';
import { DOCUMENT, Location } from '@angular/common';
import {
  Overlay,
  OverlayKeyboardDispatcher,
  OverlayOutsideClickDispatcher,
  OverlayPositionBuilder,
  ScrollStrategyOptions,
} from '@angular/cdk/overlay';
import { Directionality } from '@angular/cdk/bidi';

import { CustomOverlayContainer } from './custom-overlay-container';

@Injectable({
  providedIn: 'root',
})
export class CustomOverlay extends Overlay {
  private readonly _customOverlayContainer: CustomOverlayContainer;
  private renderer: Renderer2;

  constructor(
    private scrollStrategyOptions: ScrollStrategyOptions,
    private customOverlayContainer: CustomOverlayContainer,
    private componentFactoryResolver: ComponentFactoryResolver,
    private overlayPositionBuilder: OverlayPositionBuilder,
    private overlayKeyboardDispatcher: OverlayKeyboardDispatcher,
    private overlayOutsideClickDispatcher: OverlayOutsideClickDispatcher,
    private injector: Injector,
    private ngZone: NgZone,
    private directionality: Directionality,
    private rendererFactory: RendererFactory2,
    private location: Location,
    @Inject(DOCUMENT) private document: any
  ) {
    super(
      scrollStrategyOptions,
      customOverlayContainer,
      componentFactoryResolver,
      overlayPositionBuilder,
      overlayKeyboardDispatcher,
      injector,
      ngZone,
      document,
      directionality,
      location,
      overlayOutsideClickDispatcher
    );

    this.renderer = rendererFactory.createRenderer(null, null);
    this._customOverlayContainer = customOverlayContainer;
  }

  public setContainerElement(containerElement: HTMLElement): void {
    this.renderer.setStyle(containerElement, 'transform', 'translateZ(0)');
    this._customOverlayContainer.setContainerElement(containerElement);
  }
}
