import { useState } from 'react'
import {
    View,
    Text,
    StyleSheet,
    StatusBar as RNStatusBar,
    Pressable,
} from 'react-native'
import { StatusBar } from 'expo-status-bar'
import { SafeAreaView } from 'react-native-safe-area-context'
import { useRouter } from 'expo-router'
import { useTranslation } from 'react-i18next'

import { LeftArrowIcon, SearchIcon } from '@/components/icon'
import WorksGallery, { type Category, type WorkItem } from '@/components/WorksGallery'

const worksData: WorkItem[] = [
    { id: 1, date: new Date(2025, 10, 28), duration: '00:05', category: '萌宠' },
    { id: 2, date: new Date(2025, 10, 28), duration: '00:05', category: '写真' },
    { id: 3, date: new Date(2025, 10, 27), duration: '00:05', category: '萌宠' },
    { id: 4, date: new Date(2025, 10, 27), duration: '00:05', category: '合拍' },
    { id: 5, date: new Date(2025, 10, 27), duration: '00:05', category: '写真' },
    { id: 6, date: new Date(2025, 10, 27), duration: '00:05', category: '萌宠' },
    { id: 7, date: new Date(2025, 10, 26), duration: '00:05', category: '合拍' },
    { id: 8, date: new Date(2025, 10, 26), duration: '00:05', category: '写真' },
]

export default function WorksListScreen() {
    const { t } = useTranslation()
    const router = useRouter()
    
    const categories: Category[] = [
        t('worksList.all') as Category,
        t('worksList.pets') as Category,
        t('worksList.portrait') as Category,
        t('worksList.together') as Category,
    ]
    
    const [selectedCategory, setSelectedCategory] = useState<Category>(categories[0])

    const filteredWorks = selectedCategory === categories[0]
        ? worksData
        : worksData.filter(work => work.category === selectedCategory)

    const filteredGroupedWorks = filteredWorks.reduce((acc, work) => {
        // 使用日期作为分组键，格式化为 YYYY-MM-DD 以便正确分组
        const dateKey = work.date instanceof Date 
            ? work.date.toISOString().split('T')[0]
            : new Date(work.date).toISOString().split('T')[0]
        
        if (!acc[dateKey]) {
            acc[dateKey] = []
        }
        acc[dateKey].push(work)
        return acc
    }, {} as Record<string, WorkItem[]>)

    return (
        <SafeAreaView style={styles.container} edges={['top']}>
            <StatusBar style="light" />
            <RNStatusBar barStyle="light-content" />

            {/* 顶部导航栏 */}
            <View style={styles.header}>
                <Pressable
                    style={styles.backButton}
                    onPress={() => router.push('/(tabs)/my')} 
                >
                    <LeftArrowIcon />
                </Pressable>
                <Text style={styles.headerTitle}>{t('worksList.title')}</Text>
                <Pressable
                    style={styles.searchButton}
                    onPress={() => {
                        router.push('/searchWorks')
                    }}
                >
                    <SearchIcon />
                </Pressable>
            </View>

            <WorksGallery
                categories={categories}
                selectedCategory={selectedCategory}
                onCategoryChange={setSelectedCategory}
                groupedWorks={filteredGroupedWorks}
                onWorkPress={(id) => {
                    router.push({
                        pathname: '/generationRecord' as any,
                        params: { id: id.toString() },
                    })
                }}
            />
        </SafeAreaView>
    )
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#090A0B',
    },
    header: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        paddingHorizontal: 12,
        paddingTop: 13,
    },
    backButton: {
        width: 22,
        height: 22,
        alignItems: 'center',
        justifyContent: 'center',
    },
    headerTitle: {
        color: '#F5F5F5',
        fontSize: 14,
        fontWeight: '500',
        flex: 1,
        textAlign: 'center',
    },
    searchButton: {
        width: 20,
        height: 20,
        alignItems: 'center',
        justifyContent: 'center',
    },
})

