/**
 * AccountAuthEdit view
 *
 * Generated by coz on 2016-11-08,
 * from a template provided by apeman-asset-templates.
 *
 * @function AccountAuthEditView
 */
'use strict'

import React, { Component, PropTypes as types } from 'react'
import defineDebug from 'debug'
import {
  ApView, ApViewHeader, ApViewBody, ApViewFooter
} from 'apeman-react-basic'
import { AccountPasswordForm } from '../../fragments'
import { asConnected, asView, asLocaled, asServed, forSpin, forEntry, ofSigned, combine } from '../../wrappers'
import { IconClasses } from 'ore-fol-enums'

const { USER_ICON } = IconClasses
const debug = defineDebug('project:view:account-auth-edit')

/** @lends AccountAuthEditView */
class AccountAuthEditView extends Component {
  // --------------------
  // Specs
  // --------------------
  static get propTypes () {
    return {}
  }

  static get defaultProps () {
    return {}
  }

  render () {
    const s = this
    const {
      props,
      l,
      labels
    } = s
    return (
      <ApView id='account-auth-edit-view'
              className='view'
              spinning={ props.spinning }
              way={ props.way }
      >
        <ApViewHeader onLeftTap={ s.handlePop }
                      leftText={ null }
                      leftIcon={ props.popIcon }
                      titleText={ l('titles.ACCOUNT_AUTH_EDIT') }
                      titleIcon={ USER_ICON }
                      onRightTap={ null }
                      rightIcon={ null }
                      rightText={ null }
        />

        <ApViewBody>
          <AccountPasswordForm labels={ labels }
                               values={ props.entryValues }
                               errors={ props.entryErrors }
                               onSubmit={ s.handleSubmit }
                               onUpdate={ s.handleEntryUpdate }
          />
        </ApViewBody>
      </ApView>
    )
  }

  // --------------------
  // Lifecycle
  // --------------------

  componentWillMount () {
    debug('willMount')
    const s = this
  }

  componentDidMount () {
    debug('didMount')
    const s = this
  }

  componentWillUnmount () {
    debug('willUnmount')
    const s = this
  }

  // --------------------
  // Handlers
  // --------------------

  handleSubmit () {
    const s = this
    const { props, l } = s
    const { entryValues } = props
    const { use } = s.service('apiService')
    const { infoToast } = s.service('toastService')
    let { password } = entryValues
    return s.applySpinWhile(() =>
      use('signApi')
        .then((signApi) =>
          signApi.signmod({ password })
        )
        .then(() => {
          infoToast(l('toasts.UPDATE_SUCCESS'))
          s.handlePop().then(s.handleEntryClear)
        })
        .catch(s.handleEntryErrorCatch)
    )
  }

  // --------------------
  // Dynamic props
  // --------------------
  get labels () {
    const s = this
    const { l } = s
    return {
      current: l('labels.CURRENT_PASSWORD'),
      new: l('labels.NEW_PASSWORD'),
      submit: l('buttons.DO_UPDATE')
    }
  }

  // --------------------
  // For redux
  // --------------------
  static mapStateToProps (state, ownProps) {
    return {}
  }

}

export default combine(
  asServed(),
  forSpin(),
  forEntry(),
  asLocaled(),
  asView(),
  ofSigned(),
  asConnected()
)(AccountAuthEditView)

