export default class WsCarouselController {
  activeSlideNum = 0;
  totalSlideCount = 0;

  static $inject = ['$timeout'];
  constructor($timeout) {
    this.$timeout = $timeout;
  }

  $postLink() {
    this.$timeout(() => {
      this.totalSlideCount = this._getSlideDOMElements().length;
      this._showSlide(this.activeSlideNum);
    }, 0);
  }

  showNextSlide = () => {
    this._showSlide(++this.activeSlideNum);
  }

  showPreviousSlide = () => {
    this._showSlide(--this.activeSlideNum);
  }

  canGoForward = () => this.activeSlideNum < this.totalSlideCount - 1;
  canGoBack = () => this.activeSlideNum > 0;

  getSelectorRange = () => new Array(this.totalSlideCount).map((v, i) => i);

  isActiveSlide = slideNum => slideNum === this.activeSlideNum;
  setActiveSlide = (slideNum) => {
    if (slideNum !== this.activeSlideNum) {
      this.activeSlideNum = slideNum;
      this._showSlide(slideNum);
    }
  }

  _getSlideDOMElements = () => angular
    .element('.slide-container')
    .children();

  _showSlide = (slideNum) => {
    const allSlides = this._getSlideDOMElements();
    allSlides.removeClass('next-slide previous-slide current-slide');

    if (slideNum > 0) {
      angular.element(allSlides[slideNum - 1]).addClass('previous-slide');
    }

    if (slideNum < allSlides.length - 1) {
      angular.element(allSlides[slideNum + 1]).addClass('next-slide');
    }

    angular.element(allSlides[slideNum]).addClass('current-slide');
    if (this.onSlideActivated) {
      this.onSlideActivated({ slideIndex: slideNum });
    }
  }
}
