/**
 * AccountEdit view
 *
 * Generated by coz on 2016-10-10,
 * from a template provided by apeman-asset-templates.
 *
 * @class AccountProfileEditView
 */
'use strict'

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

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

/** @lends AccountProfileEditView */
class AccountProfileEditView 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-edit-view'
              className='view'
              way={ props.way }
              spinning={ props.spinning }
      >
        <ApViewHeader onLeftTap={ s.handlePop }
                      leftText={ null }
                      leftIcon={ props.popIcon }
                      titleText={ l('titles.ACCOUNT_PROFILE_EDIT') }
                      titleIcon={ USER_ICON }
                      onRightTap={ null }
                      rightIcon={ null }
        />

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

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

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

  componentDidMount () {
    debug('didMount')
    const s = this
    let { signed } = s.props
    if (signed) {
      s.handleEntryUpdate(
        Object.assign({}, flatten({ profile: signed.profile || {} }))
      )
    }
  }

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

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

  handleSubmit () {
    const s = this
    const { props, l } = s
    const { use } = s.service('apiService')
    const { infoToast } = s.service('toastService')

    let { entryValues } = props
    let { profile: profileValues } = expand(entryValues)
    return s.applySpinWhile(() =>
      use('accountApi')
        .then((accountApi) =>
          accountApi.updateProfile(profileValues)
        )
        .then(() => s.syncSigned())
        .then(() => {
          infoToast(l('toasts.UPDATE_SUCCESS'))
          s.handlePop().then(s.handleEntryClear)
        })
        .catch(s.handleEntryErrorCatch)
    )
  }

  // --------------------
  // Labels
  // --------------------

  get labels () {
    const s = this
    const { l } = s
    return {
      'profile.name': l('labels.PROFILE_NAME'),
      'profile.image': l('labels.PROFILE_IMAGE'),
      'submit': l('buttons.DO_UPDATE')
    }
  }

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

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

