import { View, Text, ScrollView, Pressable, Image, useWindowDimensions, ActivityIndicator } from 'react-native';
import React, { useContext, useEffect, useState } from 'react';
import { SafeAreaView } from 'react-native-safe-area-context';
import { router } from 'expo-router';
import { CrossLogoSvg, RightArrowIconSvg } from '../global/SVG';
import { walletService } from '../../service/walletService';
import { useTranslation } from '../../util/hooks/useTranslation';

const VipQrPageComponent = () => {
    const { t } = useTranslation(); // 使用翻译钩子
    const { width } = useWindowDimensions();
    const qrCodeSize = width * 0.8; // 80% of screen width
    const [loading, setLoading] = useState(true);
    const [transactions, setTransactions] = useState<any[]>([]);

    useEffect(() => {
        const fetchTransactionRecords = async () => {
            try {
                setLoading(true);
                const response = await walletService.getTransactionRecord();
                if (response && Array.isArray(response)) {
                    setTransactions(response);
                } else {
                    setTransactions([]);
                }
            } catch (error) {
                console.error(t('vipQr.error_fetching_transactions'), error);
                setTransactions([]);
            } finally {
                setLoading(false);
            }
        };

        fetchTransactionRecords();
    }, []);

    return (
        <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
            <ScrollView className="flex-1 mx-[5%]" showsVerticalScrollIndicator={false}>
                <View style={{ marginTop: 25 }}>
                    <Pressable
                        onPress={() => {
                            router.replace('/mainPage');
                        }}
                    >
                        <CrossLogoSvg />
                    </Pressable>
                    <Text style={{ fontSize: 45, marginVertical: 25 }}>{t('vipQr.title')}</Text>
                </View>
                {loading ? (
                    <View className="items-center justify-center mt-10">
                        <ActivityIndicator size="large" color="#02677D" />
                        <Text className="text-center mt-4">{t('vipQr.loading')}</Text>
                    </View>
                ) : transactions.length > 0 ? (
                    <View className="items-center">
                        <Text className="text-lg self-start mb-4">{t('vipQr.scan_instructions')}</Text>
                        <Image
                            source={require('../../assets/vipqrcode.png')}
                            className=""
                            style={{ width: qrCodeSize, height: qrCodeSize }}
                        />
                    </View>
                ) : (
                    <View className="items-center mt-10">
                        <Text className="text-xl text-center">{t('vipQr.no_transactions_message')}</Text>
                    </View>
                )}
            </ScrollView>
        </SafeAreaView>
    );
};

export default VipQrPageComponent;