import {
  View,
  useWindowDimensions,
  Animated,
  Image as NativeImage,
  Text,
} from "react-native";
import React, { useState, useCallback } from "react";
import { useTailwind } from "tailwind-rn";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { TabView, SceneMap, TabBar } from "react-native-tab-view";
import PendingReview from "./PendingReview";
import Reviewed from "./Reviewed";

export default function RefundList({ navigation }) {
  const tailwind = useTailwind();
  const insets = useSafeAreaInsets();
  //tab组件相关
  const layout = useWindowDimensions();
  const [index, setIndex] = useState(0);
  const [routes] = useState([
    { key: "doing", title: "待处理" },
    { key: "done", title: "已处理" },
  ]);

  const renderScene = useCallback(
    SceneMap({
      doing: () => <PendingReview navigation={navigation} />,
      done: () => <Reviewed navigation={navigation} />,
    }),
    []
  );

  const renderIndicator = useCallback((props) => {
    const { position, navigationState, getTabWidth } = props;
    const inputRange = [0, 1];
    const translateX = position.interpolate({
      inputRange: inputRange,
      outputRange: inputRange.map((x) => {
        return x * getTabWidth(navigationState.index);
      }),
    });
    return (
      <Animated.View
        style={{
          width: `${100 / navigationState.routes.length}%`,
          transform: [
            {
              translateX,
            },
          ],
          paddingBottom: 12,
          ...tailwind("flex flex-1 items-center justify-end"),
        }}
      >
        <NativeImage
          source={require("../../../assets/icon/others/pinkline.png")}
        />
      </Animated.View>
    );
  }, []);

  const renderTabBar = useCallback(
    (props) => (
      <TabBar
        {...props}
        style={tailwind("bg-transparent")}
        labelStyle={tailwind("text-base font-medium")}
        renderIndicator={renderIndicator}
      />
    ),
    []
  );

  return (
    <View
      style={{
        paddingBottom: insets.bottom,
        paddingLeft: insets.left,
        paddingRight: insets.right,
        ...tailwind("flex-1"),
      }}
    >
      <TabView
        navigationState={{ index, routes }}
        swipeEnabled={true}
        renderScene={renderScene}
        renderTabBar={renderTabBar}
        onIndexChange={setIndex}
        initialLayout={{ width: layout.width }}
      />
    </View>
  );
}