/*
 * Copyright (c) 2019 - 2020 Arnaud Vergnet.
 *
 * This file is part of Campus INSAT.
 *
 * Campus INSAT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Campus INSAT is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Campus INSAT.  If not, see <https://www.gnu.org/licenses/>.
 */

import { NativeScrollEvent, NativeSyntheticEvent } from 'react-native';

const speedOffset = 5;

type ListenerFunctionType = (shouldHide: boolean) => void;

/**
 * Class used to detect when to show or hide a component based on scrolling
 */
export default class AutoHideHandler {
  lastOffset: number;

  isHidden: boolean;

  listeners: Array<ListenerFunctionType>;

  constructor(startHidden: boolean) {
    this.listeners = [];
    this.isHidden = startHidden;
    this.lastOffset = 0;
  }

  /**
   * Adds a listener to the hide event
   *
   * @param listener
   */
  addListener(listener: (shouldHide: boolean) => void) {
    this.listeners.push(listener);
  }

  /**
   * Notifies every listener whether they should hide or show.
   *
   * @param shouldHide
   */
  notifyListeners(shouldHide: boolean) {
    this.listeners.forEach((func: ListenerFunctionType) => {
      func(shouldHide);
    });
  }

  /**
   * Callback to be used on the onScroll animated component event.
   *
   * Detects if the current speed exceeds a threshold and notifies listeners to hide or show.
   *
   * The hide even is triggered when the user scrolls down, and the show event on scroll up.
   * This does not take into account the speed when the y coordinate is negative, to prevent hiding on over scroll.
   * (When scrolling up and hitting the top on ios for example)
   *
   * //TODO Known issue:
   * When refreshing a list with the pull down gesture on ios,
   * this can trigger the hide event as it scrolls down the list to show the refresh indicator.
   * Android shows the refresh indicator on top of the list so this is not an issue.
   *
   * @param event The scroll event generated by the animated component onScroll prop
   */
  onScroll(event: NativeSyntheticEvent<NativeScrollEvent>) {
    const { nativeEvent } = event;
    const speed =
      nativeEvent.contentOffset.y < 0
        ? 0
        : this.lastOffset - nativeEvent.contentOffset.y;
    if (speed < -speedOffset && !this.isHidden) {
      // Go down
      this.notifyListeners(true);
      this.isHidden = true;
    } else if (speed > speedOffset && this.isHidden) {
      // Go up
      this.notifyListeners(false);
      this.isHidden = false;
    }
    this.lastOffset = nativeEvent.contentOffset.y;
  }
}
