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

import React, { Component, PropTypes as types } from 'react'
import defineDebug from 'debug'
import {
  ApView, ApViewHeader, ApViewBody
} from 'apeman-react-basic'
import { asConnected, asView, asLocaled, asServed, forSpin, forEntry, ofSigned, combine } from '../../wrappers'
import { AccountEmailForm } from '../../fragments'
import { IconClasses } from 'ore-fol-enums'
const { USER_ICON } = IconClasses
const debug = defineDebug('project:view:account-account-edit')

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

  static get defaultProps () {
    return {}
  }

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

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

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

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

  componentDidMount () {
    debug('didMount')
    const s = this
    const { props } = s
    let { signed } = props
    if (signed) {
      let { email } = signed
      s.handleEntryUpdate({
        email
      })
    }
  }

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

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

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

  // --------------------
  // Read Only Properties
  // --------------------
  get labels () {
    const s = this
    const { l } = s
    return {
      key: l('labels.EMAIL'),
      email: l('labels.EMAIL'),
      submit: l('buttons.DO_UPDATE')
    }
  }

  get errors () {
    const s = this
    const { labels, props } = s
    const { localizeErrors } = s.service('localeService')
    return localizeErrors(props.entryErrors, { labels })
  }
}

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

