import {
    View,
    Text,
    StyleSheet,
    Pressable,
    Alert,
    Dimensions,
    ActivityIndicator
} from 'react-native';
import NormalButton from '../../../global/normal_button';
import Logo from '../../../global/logo';
import NormalInput from '../../../global/normal_input';
import { useState } from 'react';
import { useAuth } from '../../../../context/AuthProvider';
type LoginPageProps = {
    goToNextPage: () => void;
    goToForgetPassWordPage: () => void;
};

const screenHeight = Dimensions.get('window').height;
const LoginPage: React.FC<LoginPageProps> = ({
    goToNextPage,
    goToForgetPassWordPage
}) => {
    const [loginEmail, setLoginEmail] = useState('');
    const [loginPassword, setLoginPassword] = useState('');
    const [isLoading, setIsLoading] = useState(false);
    const { login } = useAuth();

    const _login = async (username: string, password: string) => {
        setIsLoading(true);
        if (username === '' || password === '') {
            Alert.alert('Error', 'Please enter a username and password');
        } else {
            const lowerCaseUsername = username.toLowerCase();
            const success = await login(lowerCaseUsername, password);
            if (!success) {
                Alert.alert('Error', 'Invalid username or password');
            }
        }
        setIsLoading(false);
    };

    return (
        <View style={styles.container} className="">
            <View style={styles.topContainerForLogo}>
                <Logo />
            </View>
            <View style={styles.bottomContainer} className="">
                <NormalInput
                    placeholder="電子郵件"
                    onChangeText={(email) => setLoginEmail(email)}
                    extendedStyle={{ borderRadius: 12, padding: 20 }}
                    textContentType="username"
                    autoComplete="username"
                />
                <NormalInput
                    placeholder="密碼"
                    onChangeText={(password) => setLoginPassword(password)}
                    secureTextEntry={true}
                    extendedStyle={{ borderRadius: 12, padding: 20 }}
                    textContentType="password"
                    autoComplete="password"
                />

                <Pressable onPress={() => goToForgetPassWordPage()}>
                    <Text style={styles.text}>忘記密碼</Text>
                </Pressable>
                <NormalButton
                    extendedStyle={{ padding: 20 }}
                    onPress={() => _login(loginEmail, loginPassword)}
                    title={
                        isLoading ? (
                            <Text
                                style={{
                                    fontWeight: '700',
                                    fontSize: 20,
                                    color: '#fff'
                                }}
                            >
                                <ActivityIndicator />
                            </Text>
                        ) : (
                            <Text
                                style={{
                                    fontWeight: '700',
                                    fontSize: 20,
                                    color: '#fff'
                                }}
                            >
                                登入
                            </Text>
                        )
                    }
                />
                <Pressable onPress={goToNextPage}>
                    <Text style={styles.text}>註冊會員</Text>
                </Pressable>
            </View>
        </View>
    );
};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        height: Dimensions.get('window').height,
        justifyContent: 'center',
        marginHorizontal: 20
    },
    topContainerForLogo: { marginBottom: screenHeight < 700 ? '0%' : '20%' },
    bottomContainer: {
        marginBottom: '20%',
        justifyContent: 'flex-start',
        gap: 10
    },

    text: {
        color: '#02677D',
        fontSize: 16,
        paddingVertical: 5
    }
});
export default LoginPage;
