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

export default function WechatWaitingToAdd({ navigation, route }) {
  const tailwind = useTailwind();
  const insets = useSafeAreaInsets();

  //tab组件相关
  const layout = useWindowDimensions();
  const [index, setIndex] = useState(route.params?.tab ? route.params?.tab : 0);
  const [routes] = useState([
    { key: "haveNotAddWechat", title: "待添加" },
    { key: "alreadyAddWechat", title: "已完成" },
  ]);

  const renderScene = useCallback(
    SceneMap({
      haveNotAddWechat: () => <HaveNotAddWechat />,
      alreadyAddWechat: () => <AlreadyAddWechat />,
    }),
    []
  );

  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) => (
      <View style={tailwind("flex flex-row items-center")}>
        <TabBar
          {...props}
          activeColor="#FFFFFF"
          inactiveColor="#FFFFFF80"
          style={tailwind("bg-transparent flex-1")}
          labelStyle={tailwind("text-lg font-medium")}
          renderIndicator={renderIndicator}
        />
      </View>
    ),
    []
  );

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