/* @flow */

import * as React from 'react';
import { View, ViewPagerAndroid, StyleSheet, I18nManager } from 'react-native';
import { PagerRendererPropType } from './TabViewPropTypes';
import type { PagerRendererProps } from './TabViewTypeDefinitions';

type PageScrollEvent = {
  nativeEvent: {
    position: number,
    offset: number,
  },
};

type PageScrollState = 'dragging' | 'settling' | 'idle';

type Props<T> = PagerRendererProps<T>;

export default class TabViewPagerAndroid<T: *> extends React.Component<
  Props<T>
> {
  static propTypes = PagerRendererPropType;

  static defaultProps = {
    canJumpToTab: () => true,
  };

  constructor(props: Props<T>) {
    super(props);
    this._currentIndex = this.props.navigationState.index;
  }

  componentDidUpdate(prevProps: Props<T>) {
    if (
      this.props.layout !== prevProps.layout ||
      this.props.navigationState.routes.length !==
        prevProps.navigationState.routes.length ||
      this.props.navigationState.index !== prevProps.navigationState.index
    ) {
      this._handlePageChange(this.props.navigationState.index);
    }
  }

  _pageChangeCallabck: any;
  _viewPager: ?ViewPagerAndroid;
  _isIdle: boolean = true;
  _currentIndex = 0;

  _getPageIndex = (index: number) =>
    I18nManager.isRTL
      ? this.props.navigationState.routes.length - (index + 1)
      : index;

  _setPage = (index: number, animated = true) => {
    const pager = this._viewPager;

    if (pager) {
      const page = this._getPageIndex(index);

      if (this.props.animationEnabled === false || animated === false) {
        pager.setPageWithoutAnimation(page);
      } else {
        pager.setPage(page);
      }
    }
  };

  _handlePageChange = (index: number) => {
    if (this._isIdle && this._currentIndex !== index) {
      this._setPage(index);
      this._currentIndex = index;
    }
  };

  _handlePageScroll = (e: PageScrollEvent) => {
    this.props.offsetX.setValue(
      e.nativeEvent.position *
        this.props.layout.width *
        (I18nManager.isRTL ? 1 : -1)
    );
    this.props.panX.setValue(
      e.nativeEvent.offset *
        this.props.layout.width *
        (I18nManager.isRTL ? 1 : -1)
    );
  };

  _handlePageScrollStateChanged = (e: PageScrollState) => {
    this._isIdle = e === 'idle';

    let nextIndex = this._currentIndex;

    const nextRoute = this.props.navigationState.routes[nextIndex];

    if (this.props.canJumpToTab(nextRoute)) {
      this.props.jumpTo(nextRoute.key);
    } else {
      this._setPage(this.props.navigationState.index);
      this._currentIndex = this.props.navigationState.index;
    }
  };

  _handlePageSelected = (e: PageScrollEvent) => {
    const index = this._getPageIndex(e.nativeEvent.position);
    this._currentIndex = index;
  };

  _setRef = (el: ?ViewPagerAndroid) => (this._viewPager = el);

  render() {
    const { children, navigationState, swipeEnabled } = this.props;
    const content = React.Children.map(children, (child, i) => (
      <View
        key={navigationState.routes[i].key}
        testID={navigationState.routes[i].testID}
        style={styles.page}
      >
        {child}
      </View>
    ));

    if (I18nManager.isRTL) {
      content.reverse();
    }

    const initialPage = this._getPageIndex(navigationState.index);

    return (
      <ViewPagerAndroid
        key={navigationState.routes.length}
        keyboardDismissMode="on-drag"
        initialPage={initialPage}
        scrollEnabled={swipeEnabled !== false}
        onPageScroll={this._handlePageScroll}
        onPageScrollStateChanged={this._handlePageScrollStateChanged}
        onPageSelected={this._handlePageSelected}
        style={styles.container}
        ref={this._setRef}
      >
        {content}
      </ViewPagerAndroid>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flexGrow: 1,
  },

  page: {
    overflow: 'hidden',
  },
});

@@ -435,16 +435,65 @@
 Props%3CT%3E
+ & %7B%0A  keyboardDismissMode: 'none' %7C 'on-drag',%0A%7D
 ;%0A%0Aexpor
@@ -675,16 +675,52 @@
 %3E true,%0A
+    keyboardDismissMode: 'on-drag',%0A
   %7D;%0A%0A  
@@ -2949,16 +2949,22 @@
  const %7B
+%0A     
  childre
@@ -2965,16 +2965,22 @@
 hildren,
+%0A     
  navigat
@@ -2988,16 +2988,22 @@
 onState,
+%0A     
  swipeEn
@@ -3007,16 +3007,48 @@
 eEnabled
+,%0A      keyboardDismissMode,%0A   
  %7D = thi
@@ -3544,17 +3544,29 @@
 ode=
-%22on-drag%22
+%7BkeyboardDismissMode%7D
 %0A   

add keyboardDismissMode prop to TabViewPagerAndroid (#453)