import React, { useState } from 'react';
import type { PropsWithChildren } from 'react';
import {
  Alert,
  Button,
  SafeAreaView,
  ScrollView,
  StatusBar,
  Text,
  TextInput,
  useColorScheme,
  View,
} from 'react-native';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import { HDNode } from 'ethers/lib/utils';

import { generateWallet, resetWallet, signMessage } from './utils';
import styles from './styles/stylesheet';
import { Section } from './components/Section';

const App = (): React.JSX.Element => {
  const [message, setMessage] = useState('');
  const [isWalletCreated, setIsWalletCreated] = useState<boolean>(false);
  const [wallet, setWallet] = useState<HDNode | null>();

  const createWallet = async () => {
    Alert.alert('Creating Wallet...');
    const wallet = await generateWallet();
    if (wallet) {
      setWallet(wallet);
      setIsWalletCreated(true);
    }
  };

  const isDarkMode = useColorScheme() === 'dark';

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  return (
    <SafeAreaView style={backgroundStyle}>
      <StatusBar
        barStyle={isDarkMode ? 'light-content' : 'dark-content'}
        backgroundColor={backgroundStyle.backgroundColor}
      />
      <ScrollView
        contentInsetAdjustmentBehavior="automatic"
        style={backgroundStyle}>
        <View style={styles.headerContainer}>
          <Text style={styles.headerText}>Laconic Wallet</Text>
        </View>
        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
          }}>
          <View>
            {isWalletCreated ? (
              <View>
                <Section title="Wallet Details" />
                <View style={{ marginTop: 32, paddingHorizontal: 24 }}>
                  <Text style={{ fontSize: 16, color: 'black' }}>
                    Address: {wallet && wallet.address.toString()}
                  </Text>
                  <Text style={{ fontSize: 16, color: 'black' }}>
                    Public Key: {wallet && wallet.publicKey.toString()}
                  </Text>
                </View>
                <Section title="Sign a message" />
                <View>
                  <TextInput
                    style={{
                      height: 40,
                      borderColor: 'gray',
                      borderWidth: 1,
                      margin: 10,
                      padding: 5,
                    }}
                    placeholder="Enter your message"
                    onChangeText={text => setMessage(text)}
                    value={message}
                  />
                  <View
                    style={{ marginTop: 20, width: 150, alignSelf: 'center' }}>
                    <Button
                      title="Sign Message"
                      color="#841584"
                      onPress={() => {
                        signMessage(message);
                      }}
                    />
                  </View>
                </View>
                <Section title="" />
                <View>
                  <View
                    style={{ marginTop: 20, width: 150, alignSelf: 'center' }}>
                    <Button
                      title="Reset Wallet"
                      color="#D30000"
                      onPress={async () => {
                        await resetWallet();
                        setIsWalletCreated(false);
                        setWallet(null);
                      }}
                    />
                  </View>
                </View>
              </View>
            ) : (
              <View>
                <Section title="Create Wallet" />
                <View style={{ marginTop: 20, width: 150, alignSelf: 'center' }}>
                  <Button
                    title="Create"
                    color="#841584"
                    onPress={createWallet}
                  />
                </View>
              </View>
            )}
          </View>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};

export default App;
