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 styles from './styles/stylesheet';
import { generateWallet, signMessage } from './utils';

type SectionProps = PropsWithChildren<{
  title: string;
}>;

const Section = ({children, title}: SectionProps): React.JSX.Element => {
  const isDarkMode = useColorScheme() === 'dark';
  return (
    <View style={styles.sectionContainer}>
      <Text
        style={[
          styles.sectionTitle,
          {
            color: isDarkMode ? Colors.white : Colors.black,
          },
        ]}>
        {title}
      </Text>
      <Text
        style={[
          styles.sectionDescription,
          {
            color: isDarkMode ? Colors.light : Colors.dark,
          },
        ]}>
        {children}
      </Text>
    </View>
  );
};

const App = (): React.JSX.Element => {
  const [message, setMessage] = useState('');
  const [isWalletCreated, setIsWalletCreated] = useState(false);

  const createWallet = async () => {
    Alert.alert('Creating Wallet...');
    await generateWallet();
    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="Generate Wallet">Wallet Created! </Section>
              </View>
            ) : (
              <View>
                <Section title="Generate Wallet">
                  Click the button to generate the wallet
                </Section>
                <View style={{marginTop: 20, width: 150, alignSelf: 'center'}}>
                  <Button
                    title="Initialise Wallet"
                    color="#841584"
                    onPress={createWallet}
                  />
                </View>
              </View>
            )}
          </View>
          <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>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};

export default App;
