import { View } from 'react-native';
import { Button, TextInput } from 'react-native-paper';
import React, { useState } from 'react';
import { Alert } from 'react-native';

import { NativeStackScreenProps } from '@react-navigation/native-stack';

import { StackParamsList } from '../types';
import { signMessage } from '../utils';

type SignProps = NativeStackScreenProps<StackParamsList, 'SignMessage'>;

const SignMessage = ({ route }: SignProps) => {
  const network = route.params?.selectedNetwork;

  const [message, setMessage] = useState<string>('');

  const signMessageHandler = async () => {
    if (network) {
      const signedMessage = await signMessage(message, network, 0);
      Alert.alert('Signature', signedMessage);
    }
  };

  return (
    <View style={{ marginTop: 30, paddingHorizontal: 48 }}>
      <TextInput
        mode="outlined"
        placeholder="Enter your message"
        onChangeText={text => setMessage(text)}
        value={message}
      />
      <View style={{ marginTop: 20, width: 150, alignSelf: 'center' }}>
        <Button mode="contained" onPress={signMessageHandler}>
          Sign
        </Button>
      </View>
    </View>
  );
};

export default SignMessage;
