import React from 'react';
import { SessionButton } from '../session/SessionButton';
import { ToastUtils, UserUtils } from '../../session/utils';
import { withTheme } from 'styled-components';
import { PasswordUtil } from '../../util';
import { getPasswordHash } from '../../data/data';
import { QRCode } from 'react-qr-svg';
import { mn_decode } from '../../session/crypto/mnemonic';
import { SessionWrapperModal } from '../session/SessionWrapperModal';
import { SpacerLG, SpacerSM, SpacerXS } from '../basic/Text';
import autoBind from 'auto-bind';
import { recoveryPhraseModal } from '../../state/ducks/modalDialog';
interface State {
  error: string;
  loadingPassword: boolean;
  loadingSeed: boolean;
  recoveryPhrase: string;
  hasPassword: boolean | null;
  passwordHash: string;
  passwordValid: boolean;
}
class SessionSeedModalInner extends React.Component<{}, State> {
  constructor(props: any) {
    super(props);
    this.state = {
      error: '',
      loadingPassword: true,
      loadingSeed: true,
      recoveryPhrase: '',
      hasPassword: null,
      passwordHash: '',
      passwordValid: false,
    };
    autoBind(this);
  }
  public componentDidMount() {
    setTimeout(() => ($('#seed-input-password') as any).focus(), 100);
    void this.checkHasPassword();
    void this.getRecoveryPhrase();
  }
  public render() {
    const i18n = window.i18n;
    const { hasPassword, passwordValid } = this.state;
    const loading = this.state.loadingPassword || this.state.loadingSeed;
    const onClose = () => window.inboxStore?.dispatch(recoveryPhraseModal(null));
    return (
      <>
        {!loading && (
          
            
            {hasPassword && !passwordValid ? (
              <>{this.renderPasswordView()}>
            ) : (
              <>{this.renderSeedView()}>
            )}
          
        )}
      >
    );
  }
  private renderPasswordView() {
    const error = this.state.error;
    const i18n = window.i18n;
    const onClose = () => window.inboxStore?.dispatch(recoveryPhraseModal(null));
    return (
      <>
        
{i18n('showRecoveryPhrasePasswordRequest')}
        
        {error && (
          <>
            
            {error}
          >
        )}
        
        
          
          
        
      >
    );
  }
  private renderSeedView() {
    const i18n = window.i18n;
    const bgColor = '#FFFFFF';
    const fgColor = '#1B1B1B';
    const hexEncodedSeed = mn_decode(this.state.recoveryPhrase, 'english');
    return (
      <>
        
          {i18n('recoveryPhraseSavePromptMain')}
          
          {this.state.recoveryPhrase}
         
        
        
          
        
        
        
           {
              this.copyRecoveryPhrase(this.state.recoveryPhrase);
            }}
          />
        
      >
    );
  }
  private confirmPassword() {
    const passwordHash = this.state.passwordHash;
    const passwordValue = jQuery('#seed-input-password').val();
    const isPasswordValid = PasswordUtil.matchesHash(passwordValue as string, passwordHash);
    if (!passwordValue) {
      this.setState({
        error: window.i18n('noGivenPassword'),
      });
      return false;
    }
    if (passwordHash && !isPasswordValid) {
      this.setState({
        error: window.i18n('invalidPassword'),
      });
      return false;
    }
    this.setState({
      passwordValid: true,
      error: '',
    });
    window.removeEventListener('keyup', this.onEnter);
    return true;
  }
  private async checkHasPassword() {
    if (!this.state.loadingPassword) {
      return;
    }
    const hash = await getPasswordHash();
    this.setState({
      hasPassword: !!hash,
      passwordHash: hash || '',
      loadingPassword: false,
    });
  }
  private async getRecoveryPhrase() {
    if (this.state.recoveryPhrase) {
      return false;
    }
    const recoveryPhrase = UserUtils.getCurrentRecoveryPhrase();
    this.setState({
      recoveryPhrase,
      loadingSeed: false,
    });
    return true;
  }
  private copyRecoveryPhrase(recoveryPhrase: string) {
    window.clipboard.writeText(recoveryPhrase);
    ToastUtils.pushCopiedToClipBoard();
    window.inboxStore?.dispatch(recoveryPhraseModal(null));
  }
  private onEnter(event: any) {
    if (event.key === 'Enter') {
      this.confirmPassword();
    }
  }
}
export const SessionSeedModal = withTheme(SessionSeedModalInner);