import { useNavigation } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import AppText from 'components/AppText';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
  Image,
  Linking,
  Modal,
  ScrollView,
  StyleSheet,
  TouchableOpacity,
  View,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import SendIntentAndroid from 'react-native-send-intent';
import Logo from 'screens/../../assets/bootsplash/logo_512.png';
import InfoIcon from 'svg/Info';
import { RootStackParamList } from 'types/types';

type LoginScreenNavigationProp = NativeStackNavigationProp<
  RootStackParamList,
  'Login'
>;

const SelectAuth = () => {
  const { t } = useTranslation();
  const navigation = useNavigation<LoginScreenNavigationProp>();
  const [modalVisible, setModalVisible] = useState(false);
  const [newRegisterModalVisible, setNewRegisterModalVisible] = useState(false);

  const openTelegram = async () => {
    const telegramUri = 'tg://resolve?domain=cpostuz';
    try {
      const success = await SendIntentAndroid.openAppWithUri(telegramUri);
      if (!success) Linking.openURL('https://t.me/cpostuz');
    } catch (error) {
      Linking.openURL('https://t.me/cpostuz');
    }
  };

  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={styles.container}>
        <ScrollView
          style={{ flex: 1 }}
          contentContainerStyle={styles.scrollContent}
          showsVerticalScrollIndicator={false}
        >
          <View style={[styles.innerContainer, { maxWidth: 500 }]}>
            <View style={styles.logoWrapper}>
              <Image
                source={Logo}
                style={[
                  styles.logoImage,
                  {
                    width: 180,
                    height: 150,
                    marginLeft: 35,
                  },
                ]}
              />
              <AppText
                style={[styles.logoText, { fontSize: 52, fontWeight: '900' }]}
              >
                CPOST
              </AppText>
              {/* <AppText style={[styles.logoText, { fontSize: 32 }]}>CPOST</AppText> */}
            </View>

            <AppText style={[styles.title, { fontSize: 24 }]}>
              {t('Ro’yxatdan o’tganmisz')}
            </AppText>

            <View style={styles.btnContainer}>
              <View style={{ gap: 6 }}>
                <TouchableOpacity
                  onPress={() => setModalVisible(true)}
                  style={{
                    gap: 4,
                    flexDirection: 'row',
                    justifyContent: 'center',
                    alignItems: 'center',
                  }}
                >
                  <InfoIcon color="#000" height={18} width={18} />
                  <AppText style={styles.helperText}>
                    {t("Botdan ro'yxatdan o’tganmisiz")}?
                  </AppText>
                </TouchableOpacity>
                <Modal
                  animationType="slide"
                  transparent={true}
                  visible={modalVisible}
                  onRequestClose={() => setModalVisible(false)}
                >
                  <View style={styles.modalOverlay}>
                    <View style={styles.modalContainer}>
                      <ScrollView>
                        <AppText style={styles.modalTitle}>
                          {t('Yordam')}
                        </AppText>
                        <AppText style={styles.modalText}>
                          {t('Agar siz oldin ')}

                          <AppText
                            style={{ color: '#28A7E8' }}
                            onPress={openTelegram}
                          >
                            @cpcargo_bot
                          </AppText>
                          {t(
                            ' orqali ro’yxatdan o’tgan bo’lsangiz, tizimga kirish uchun botda ishlatilgan telefon raqamingiz va passport seriya raqamingizni kiriting.',
                          )}
                        </AppText>
                        <AppText style={styles.modalText}>
                          {t('Masalan')}:
                        </AppText>
                        <AppText style={styles.modalText}>
                          {t('Telefon: +998901234567')}
                        </AppText>
                        <AppText style={styles.modalText}>
                          {t('Passport seriya: AA1234567')}
                        </AppText>
                        <AppText style={styles.modalText}>
                          {t(
                            'Shu ma’lumotlarni kiritganingizdan so’ng siz tizimga kirishingiz mumkin.',
                          )}
                        </AppText>

                        <TouchableOpacity
                          style={styles.closeButton}
                          onPress={() => setModalVisible(false)}
                        >
                          <AppText
                            style={{
                              color: '#fff',
                              textAlign: 'center',
                              fontWeight: 'bold',
                            }}
                          >
                            {t('Yopish')}
                          </AppText>
                        </TouchableOpacity>
                      </ScrollView>
                    </View>
                  </View>
                </Modal>
                <TouchableOpacity
                  onPress={() => navigation.navigate('Login')}
                  style={[
                    styles.button,
                    {
                      backgroundColor: 'none',
                      borderWidth: 1,
                      borderColor: '#28A7E8',
                    },
                  ]}
                >
                  <AppText style={[styles.btnText, { color: '#28A7E8' }]}>
                    {t('Tizimga kirish')}
                  </AppText>
                </TouchableOpacity>
              </View>
              <View style={{ gap: 6 }}>
                <TouchableOpacity
                  onPress={() => setNewRegisterModalVisible(true)}
                  style={{
                    gap: 4,
                    flexDirection: 'row',
                    justifyContent: 'center',
                    alignItems: 'center',
                  }}
                >
                  <InfoIcon color="#000" height={18} width={18} />
                  <AppText style={styles.helperText}>
                    {t("Yangi ro’yxatdan o'tmoqchimisiz")}?
                  </AppText>
                </TouchableOpacity>
                <TouchableOpacity
                  onPress={() => navigation.navigate('Register')}
                  style={styles.button}
                >
                  <AppText style={styles.btnText}>
                    {t('Ro’yxatdan o’tish')}
                  </AppText>
                </TouchableOpacity>
              </View>
              <Modal
                animationType="slide"
                transparent={true}
                visible={newRegisterModalVisible}
                onRequestClose={() => setNewRegisterModalVisible(false)}
              >
                <View style={styles.modalOverlay}>
                  <View style={styles.modalContainer}>
                    <ScrollView>
                      <AppText style={styles.modalTitle}>{t('Yordam')}</AppText>

                      <AppText style={styles.modalText}>
                        {t(
                          'Agar siz yangi foydalanuvchi bo’lsangiz, tizimga kirishdan oldin ro’yxatdan o’tishingiz kerak. Ro’yxatdan o’tish jarayonida sizdan quyidagi ma’lumotlar so’raladi:',
                        )}
                      </AppText>

                      <AppText style={styles.modalText}>
                        1. {t('Telefon raqamingiz')}
                      </AppText>
                      <AppText style={styles.modalText}>
                        2. {t('Passport seriya va raqamingiz')}
                      </AppText>
                      <AppText style={styles.modalText}>
                        3. {t('Passport oldi rasmi')}
                      </AppText>
                      <AppText style={styles.modalText}>
                        4. {t('Passport orqa rasmi')}
                      </AppText>

                      <AppText style={styles.modalText}>
                        {t(
                          'Shu ma’lumotlarni kiritganingizdan so’ng siz tizimga kirishingiz mumkin.',
                        )}
                      </AppText>

                      <TouchableOpacity
                        style={styles.closeButton}
                        onPress={() => setNewRegisterModalVisible(false)}
                      >
                        <AppText
                          style={{
                            color: '#fff',
                            textAlign: 'center',
                            fontWeight: 'bold',
                          }}
                        >
                          {t('Yopish')}
                        </AppText>
                      </TouchableOpacity>
                    </ScrollView>
                  </View>
                </View>
              </Modal>
            </View>
          </View>
        </ScrollView>
      </View>
    </SafeAreaView>
  );
};

export default SelectAuth;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingHorizontal: 20,
    margin: 5,
    borderRadius: 12,
  },
  scrollContent: {
    flexGrow: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  innerContainer: {
    width: '100%',
    paddingHorizontal: 10,
  },
  logoWrapper: {
    alignItems: 'center',
    marginBottom: 20,
  },
  logoImage: {
    resizeMode: 'stretch',
  },
  logoText: {
    fontWeight: '500',
    color: '#28A7E8',
  },
  title: {
    fontFamily: 'GolosText-Bold',
    textAlign: 'center',
    color: '#000',
    marginBottom: 20,
  },
  helperText: {
    color: '#252525ff',
    fontSize: 12,
  },
  button: {
    backgroundColor: '#28A7E8',
    height: 56,
    borderRadius: 6,
    justifyContent: 'center',
  },
  btnText: {
    color: '#fff',
    fontSize: 18,
    textAlign: 'center',
  },
  btnContainer: {
    gap: 16,
  },
  modalOverlay: {
    flex: 1,
    backgroundColor: 'rgba(0,0,0,0.5)',
    justifyContent: 'center',
    alignItems: 'center',
  },
  modalContainer: {
    backgroundColor: '#fff',
    borderRadius: 12,
    padding: 20,
    width: '85%',
    maxHeight: '70%',
  },
  modalTitle: {
    fontSize: 18,
    fontWeight: 'bold',
    marginBottom: 10,
  },
  modalText: {
    fontSize: 14,
    marginBottom: 2,
    lineHeight: 20,
    color: '#000',
  },
  closeButton: {
    backgroundColor: '#28A7E8',
    paddingVertical: 12,
    borderRadius: 8,
    marginTop: 10,
  },
});
