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

import { Directive, ElementRef, Input } from '@angular/core';
import { ColumnItemDirective } from './column-item.directive';

/** Provides pagewise scrolling to {@link ColumnItemDirective column items}. */
@Directive({
  selector: '[bciColumn]',
})
export class ColumnDirective {
  @Input() columnIndex: number;

  constructor(public elem: ElementRef) {}

  scrollToItem(item: ColumnItemDirective, itemIndex: number): void {
    const pageHeight = this.elem.nativeElement.offsetHeight;
    const itemsPerPage = Math.floor(pageHeight / item.elem.nativeElement.offsetHeight);
    const pageIndex = Math.floor(itemIndex / itemsPerPage);

    this.elem.nativeElement.scrollTop = pageIndex * itemsPerPage * item.elem.nativeElement.offsetHeight;
  }
}
