import React, { Component } from 'react'
import PropTypes from 'prop-types'
import SendRowWrapper from '../send-row-wrapper/'
import AmountMaxButton from './amount-max-button/'
import CurrencyDisplay from '../../../send/currency-display'

export default class SendAmountRow extends Component {

  static propTypes = {
    amount: PropTypes.string,
    amountConversionRate: PropTypes.oneOfType([
      PropTypes.string,
      PropTypes.number,
    ]),
    balance: PropTypes.string,
    conversionRate: PropTypes.number,
    convertedCurrency: PropTypes.string,
    gasTotal: PropTypes.string,
    inError: PropTypes.bool,
    primaryCurrency: PropTypes.string,
    selectedToken: PropTypes.object,
    setMaxModeTo: PropTypes.func,
    tokenBalance: PropTypes.string,
    updateSendAmount: PropTypes.func,
    updateSendAmountError: PropTypes.func,
  }

  validateAmount (amount) {
    const {
      amountConversionRate,
      balance,
      conversionRate,
      gasTotal,
      primaryCurrency,
      selectedToken,
      tokenBalance,
      updateSendAmountError,
    } = this.props

    updateSendAmountError({
      amount,
      amountConversionRate,
      balance,
      conversionRate,
      gasTotal,
      primaryCurrency,
      selectedToken,
      tokenBalance,
    })
  }

  handleAmountChange (amount) {
    const { updateSendAmount, setMaxModeTo } = this.props

    setMaxModeTo(false)
    this.validateAmount(amount)
    updateSendAmount(amount)
  }

  render () {
    const {
      amount,
      amountConversionRate,
      convertedCurrency,
      gasTotal,
      inError,
      primaryCurrency,
      selectedToken,
    } = this.props

    return (
      <SendRowWrapper
        label={`${this.context.t('amount')}:`}
        showError={inError}
        errorType={'amount'}
      >
        {!inError && gasTotal && <AmountMaxButton />}
        <CurrencyDisplay
          conversionRate={amountConversionRate}
          convertedCurrency={convertedCurrency}
          handleChange={newAmount => this.handleAmountChange(newAmount)}
          inError={inError}
          primaryCurrency={primaryCurrency || 'ETH'}
          selectedToken={selectedToken}
          value={amount || '0x0'}
        />
      </SendRowWrapper>
    )
  }

}

SendAmountRow.contextTypes = {
  t: PropTypes.func,
}

graphics/stats/korean/uniksc/pkg-descr'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/freebsd-ports-graphics/log/korean/uniksc/pkg-descr'>
<select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/freebsd-ports-graphics/log/'>root</a>/<a href='/~lantw44/cgit/freebsd-ports-graphics/log/korean'>korean</a>/<a href='/~lantw44/cgit/freebsd-ports-graphics/log/korean/uniksc'>uniksc</a>/<a href='/~lantw44/cgit/freebsd-ports-graphics/log/korean/uniksc/pkg-descr'>pkg-descr</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/freebsd-ports-graphics/log/korean/uniksc/pkg-descr?showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>