import React, { useEffect, useState } from 'react';
import { ToastUtils } from '../../session/utils';
import { matchesHash } from '../../util/passwordUtils';
import { getPasswordHash } from '../../data/data';
import { QRCode } from 'react-qr-svg';
import { mn_decode } from '../../session/crypto/mnemonic';
import { SpacerLG, SpacerSM, SpacerXS } from '../basic/Text';
import { recoveryPhraseModal } from '../../state/ducks/modalDialog';
import { useDispatch } from 'react-redux';
import { SessionButton, SessionButtonColor } from '../basic/SessionButton';
import { SessionWrapperModal } from '../SessionWrapperModal';
import { getCurrentRecoveryPhrase } from '../../util/storage';
interface PasswordProps {
  setPasswordValid: (val: boolean) => any;
  passwordHash: string;
}
const Password = (props: PasswordProps) => {
  const { setPasswordValid, passwordHash } = props;
  const i18n = window.i18n;
  const [error, setError] = useState('');
  const dispatch = useDispatch();
  const onClose = () => dispatch(recoveryPhraseModal(null));
  const confirmPassword = () => {
    const passwordValue = (document.getElementById('seed-input-password') as any)?.value;
    const isPasswordValid = matchesHash(passwordValue as string, passwordHash);
    if (!passwordValue) {
      setError('noGivenPassword');
      return false;
    }
    if (passwordHash && !isPasswordValid) {
      setError('invalidPassword');
      return false;
    }
    setPasswordValid(true);
    setError('');
    window.removeEventListener('keyup', onEnter);
    return true;
  };
  const onEnter = (event: any) => {
    if (event.key === 'Enter') {
      confirmPassword();
    }
  };
  return (
    <>
      
{i18n('showRecoveryPhrasePasswordRequest')}
      
      {error && (
        <>
          
          {error}
        >
      )}
      
      
        
        
      
    >
  );
};
interface SeedProps {
  recoveryPhrase: string;
  onClickCopy?: () => any;
}
const Seed = (props: SeedProps) => {
  const { recoveryPhrase, onClickCopy } = props;
  const i18n = window.i18n;
  const bgColor = '#FFFFFF';
  const fgColor = '#1B1B1B';
  const dispatch = useDispatch();
  const hexEncodedSeed = mn_decode(recoveryPhrase, 'english');
  const copyRecoveryPhrase = (recoveryPhraseToCopy: string) => {
    window.clipboard.writeText(recoveryPhraseToCopy);
    ToastUtils.pushCopiedToClipBoard();
    if (onClickCopy) {
      onClickCopy();
    }
    dispatch(recoveryPhraseModal(null));
  };
  return (
    <>
      
        {i18n('recoveryPhraseSavePromptMain')}
        
        
          {recoveryPhrase}
        
       
      
      
         {
            copyRecoveryPhrase(recoveryPhrase);
          }}
        />
      
      
      
        
      
    >
  );
};
interface ModalInnerProps {
  onClickOk?: () => any;
}
const SessionSeedModalInner = (props: ModalInnerProps) => {
  const { onClickOk } = props;
  const [loadingPassword, setLoadingPassword] = useState(true);
  const [loadingSeed, setLoadingSeed] = useState(true);
  const [recoveryPhrase, setRecoveryPhrase] = useState('');
  const [hasPassword, setHasPassword] = useState(null);
  const [passwordValid, setPasswordValid] = useState(false);
  const [passwordHash, setPasswordHash] = useState('');
  const dispatch = useDispatch();
  useEffect(() => {
    setTimeout(() => (document.getElementById('seed-input-password') as any)?.focus(), 100);
    void checkHasPassword();
    void getRecoveryPhrase();
  }, []);
  const i18n = window.i18n;
  const onClose = () => dispatch(recoveryPhraseModal(null));
  const checkHasPassword = async () => {
    if (!loadingPassword) {
      return;
    }
    const hash = await getPasswordHash();
    setHasPassword(!!hash);
    setPasswordHash(hash || '');
    setLoadingPassword(false);
  };
  const getRecoveryPhrase = async () => {
    if (recoveryPhrase) {
      return false;
    }
    const newRecoveryPhrase = getCurrentRecoveryPhrase();
    setRecoveryPhrase(newRecoveryPhrase);
    setLoadingSeed(false);
    return true;
  };
  return (
    <>
      {!loadingSeed && (
        
          
          {hasPassword && !passwordValid ? (
            
          ) : (
            
          )}
        
      )}
    >
  );
};
export const SessionSeedModal = SessionSeedModalInner;