import HandersSvg from "@/assets/icons/svg/handers.svg";
import UserinfoTotalSvg from "@/assets/icons/svg/userinfoTotal.svg";
import { Counter, UserCountData } from "@/types/user";
import * as React from "react";
import { Dimensions, StyleSheet, View, ViewStyle } from "react-native";
import { useSharedValue } from "react-native-reanimated";
import Carousel, {
    ICarouselInstance
} from "react-native-reanimated-carousel";
import { ThemedText } from "../ThemedText";
import { formatDuration } from "../utils/time";
import CategoryComponent from "./category";
interface Props {
    data: Counter
}

interface CarouselData {
    key: string,
    value: UserCountData

}[]
const width = Dimensions.get("window").width;

function CarouselComponent(props: Props) {
    const { data } = props;

    const ref = React.useRef<ICarouselInstance>(null);
    const progress = useSharedValue<number>(0);
    const [carouselDataValue, setCarouselDataValue] = React.useState<CarouselData[]>([]);
    const dataHandle = () => {
        const carouselData = { ...data?.category_count, total_count: data?.total_count }
        // 1. 转换为数组并过滤掉 'total'
        const entries = Object?.entries(carouselData)
            ?.filter(([key]) => key !== 'total_count')
            ?.map(([key, value]) => ({ key, value }));

        // 2. 找到 total 数据
        const totalEntry = {
            key: 'total_count',
            value: carouselData?.total_count
        };

        // 3. 插入到中间位置
        const middleIndex = Math.floor((entries || [])?.length / 2);
        entries?.splice(middleIndex, 0, totalEntry);
        setCarouselDataValue(entries)
        return entries;
    }

    const totleItem = (data: UserCountData) => {
        return <View style={styles.container}>
            {Object?.entries(data)?.filter(([key]) => key !== 'cover_url')?.map((item, index) => (
                <View style={index === Object?.entries(data)?.length - 2 ? { ...styles.item, borderBottomWidth: 0 } : styles.item} key={index}>
                    <ThemedText style={styles.title}>{item[0]}</ThemedText>
                    <ThemedText style={styles.number}>{item[1]}</ThemedText>
                </View>
            ))}
            <View style={styles.image}>
                <UserinfoTotalSvg />
                <View style={{ position: 'absolute', bottom: -5, right: 0, left: 0, justifyContent: 'center', alignItems: 'center' }}><HandersSvg /></View>
            </View>
        </View>
    }

    React.useEffect(() => {
        if (data) {
            dataHandle()
        }
    }, [data]);

    return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
            <Carousel
                ref={ref}
                width={width * 0.8}
                height={width * 0.8}
                data={carouselDataValue || []}
                mode="parallax"
                onProgressChange={progress}
                defaultIndex={carouselDataValue?.findIndex((item) => item?.key === 'total_count') - 1 || 0}
                renderItem={({ item, index }) => {
                    const style: ViewStyle = {
                        marginHorizontal: 10,
                        width: '92%',
                        height: '92%',
                    };
                    return (
                        <View key={index} style={style}>
                            {item?.key === 'total_count' ? (
                                totleItem(item.value)
                            ) : (
                                <View style={{ flex: 1 }}>
                                    {CategoryComponent({
                                        title: item?.key,
                                        data: [
                                            { title: 'Video', number: item?.value?.video_count },
                                            { title: 'Photo', number: item?.value?.photo_count },
                                            { title: 'Length', number: formatDuration(item?.value?.video_length || 0) }
                                        ],
                                        bgSvg: item?.value?.cover_url,
                                    })}
                                </View>
                            )}
                        </View>
                    )
                }}
            />
        </View>
    );
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: "#FFB645",
        padding: 16,
        borderRadius: 16,
        display: "flex",
        flexDirection: "column",
        height: '100%',
        position: 'relative',
    },
    image: {
        position: 'absolute',
        bottom: 0,
        right: 0,
        left: 0,
        width: '100%',
        height: '100%',
        alignItems: 'center',
        justifyContent: 'flex-end',
    },
    item: {
        flexDirection: "row",
        alignItems: "center",
        justifyContent: "space-between",
        paddingVertical: 12,
        borderBottomColor: '#fff',
        borderBottomWidth: 1,
    },
    title: {
        color: "#4C320C",
        fontWeight: "700",
        fontSize: 14,
    },
    number: {
        color: "#fff",
        fontWeight: "700",
        fontSize: 32,
        textAlign: 'right',
        flex: 1,
    }
})

export default CarouselComponent;