import React from "react";
import { View, Text, Image, FlatList, StyleSheet } from "react-native";
import { Header, HeaderContainer } from "@src/commonComponent";
import {
  appColors,
  appFonts,
  fontSizes,
  windowHeight,
  windowWidth,
} from "@src/themes";
import { useValues } from "@src/utils/context";
import { useSelector } from "react-redux";
import Images from "@src/utils/images";

export function ReferralList() {
  const { taxidoSettingData, translateData } = useSelector(
    (state: any) => state.setting
  );
  const { isDark, viewRTLStyle } = useValues();
  const { referralList } = useSelector((state: any) => state.refer);
  const referralData1 = referralList?.data?.data || [];
  const renderItem = ({ item, index }: any) => {
    const isLastItem = index === item.length - 1;

    return (
      <View>
        <View
          style={{
            flexDirection: viewRTLStyle,
            alignItems: "center",
            marginHorizontal: windowWidth(15),
          }}
        >
          {item?.referred?.profile_image_url ? (
            <Image
              style={styles.profileImage}
              source={item?.referred?.profile_image_url}
            />
          ) : (
            <View
              style={[
                styles.profileImage1,
                {
                  backgroundColor: isDark
                    ? appColors.whiteColor
                    : appColors.blackColor,
                },
              ]}
            >
              <Text
                style={{
                  fontFamily: appFonts.bold,
                  fontSize: fontSizes.FONT22,
                  color: isDark ? appColors.blackColor : appColors.whiteColor,
                }}
              >
                {item.referred.name.charAt(0).toUpperCase()}
              </Text>
            </View>
          )}

          <View style={{ flex: 1, marginHorizontal: windowWidth(10) }}>
            <View
              style={{
                flexDirection: viewRTLStyle,
                justifyContent: "space-between",
                alignItems: "center",
              }}
            >
              <Text
                style={[
                  styles.title,
                  {
                    color: isDark ? appColors.whiteColor : appColors.blackColor,
                    marginTop: windowHeight(5),
                  },
                ]}
              >
                {item.referred?.name}
              </Text>
              <View
                style={[styles.pendingView, { marginTop: windowHeight(5) }]}
              >
                <Text style={styles.pendingText}>{item.status}</Text>
              </View>
            </View>
            <Text style={styles.email}>{item.referred?.email}</Text>
          </View>
        </View>
      </View>
    );
  };

  return (
    <View
      style={{
        flex: 1,
        backgroundColor: isDark ? appColors.solidBorder : appColors.lightGray,
        paddingBottom: windowHeight(100),
      }}
    >
      <HeaderContainer value="Referrals List" />

      <View
        style={[
          styles.box,
          {
            borderColor: isDark ? appColors.darkBorder : appColors.border,
          },
          {
            backgroundColor: isDark
              ? appColors.primaryText
              : appColors.whiteColor,
          },
          { marginVertical: windowHeight(15), marginBottom: 0 },
        ]}
      >
        <FlatList
          data={referralData1}
          keyExtractor={(item) => item.id}
          renderItem={renderItem}
          showsVerticalScrollIndicator={false}
          contentContainerStyle={{ paddingBottom: windowHeight(0) }}
          ListEmptyComponent={
            <View
              style={{
                alignItems: "center",
                flex: 1,
                justifyContent: "center",
                marginTop: "40%",
                marginBottom: "20%",
              }}
            >
              <Image
                source={isDark ? Images.noVehicleDark : Images.noVehicle}
                style={{
                  height: windowHeight(250),
                  width: windowWidth(400),
                  resizeMode: "contain",
                  marginTop: windowHeight(-50),
                }}
              />
              <Text
                style={{
                  fontFamily: appFonts.bold,
                  fontSize: fontSizes.FONT22,
                  color: isDark ? appColors.whiteColor : appColors.primaryText,
                }}
              >
                {translateData.noReferralList}
              </Text>
              <Text
                style={{
                  fontFamily: appFonts.regular,
                  color: isDark ? appColors.darkText : appColors.regularText,
                  textAlign: "center",
                  marginTop: windowHeight(5),
                }}
              >
                {translateData.noReferralDetail}
              </Text>
            </View>
          }
          ItemSeparatorComponent={() => (
            <View
              style={[
                styles.line,
                {
                  backgroundColor: isDark
                    ? appColors.solidBorder
                    : appColors.lightGray,
                },
              ]}
            />
          )}
        />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  itemContainer: {
    flexDirection: "row",
    alignItems: "center",
    backgroundColor: appColors.whiteColor,
    padding: windowHeight(15),
    borderRadius: windowHeight(8),
    borderWidth: windowHeight(1),
    borderColor: appColors.border,
  },
  userImage: {
    height: windowHeight(40),
    width: windowHeight(40),
    borderRadius: windowHeight(5),
  },
  textContainer: {
    flex: 1,
    marginLeft: windowWidth(4),
  },
  nameText: {
    fontSize: fontSizes.FONT18,
    color: appColors.primaryText,
    fontFamily: appFonts.medium,
    marginHorizontal: windowWidth(5),
  },
  statusContainer: {
    paddingVertical: windowHeight(1),
    borderRadius: windowHeight(2),
    justifyContent: "center",
    alignItems: "center",
  },
  statusText: {
    fontSize: fontSizes.FONT16,
    fontFamily: appFonts.medium,
  },
  box: {
    width: "92%",
    paddingVertical: windowHeight(13),
    borderWidth: 1,
    alignSelf: "center",
    borderRadius: windowWidth(20),
    marginVertical: windowHeight(8),
  },
  profileImage: {
    height: windowHeight(43),
    width: windowHeight(43),
    borderRadius: windowHeight(43),
  },
  profileImage1: {
    height: windowHeight(43),
    width: windowHeight(43),
    borderRadius: windowHeight(43),
    alignItems: "center",
    justifyContent: "center",
  },
  title: {
    fontFamily: appFonts.medium,
    fontSize: fontSizes.FONT19,
    width: "70%",
  },
  pendingView: {
    paddingHorizontal: windowWidth(19),
    height: windowHeight(25.5),
    borderRadius: windowWidth(35),
    backgroundColor: appColors.status,
    alignItems: "center",
    justifyContent: "center",
    alignSelf: "flex-end",
  },
  pendingText: {
    fontFamily: appFonts.medium,
    fontSize: fontSizes.FONT19,
    color: appColors.statusText,
  },
  email: {
    color: appColors.gray,
    fontFamily: appFonts.regular,
    fontSize: fontSizes.FONT18,
    marginTop: windowHeight(2),
  },
  line: {
    height: windowHeight(4),
    width: "100%",
    marginVertical: windowHeight(18),
  },
});
