/**
 * ThreadEdit view
 *
 * Generated by coz on 2016-12-23,
 * from a template provided by apeman-asset-templates.
 *
 * @function ThreadEditView
 */
'use strict'

import React, { Component, PropTypes as types } from 'react'
import defineDebug from 'debug'
import {
  ApView, ApViewHeader, ApViewBody, ApViewFooter
} from 'apeman-react-basic'
import { ThreadForm } from '../../fragments'
import {
  asConnected,
  asLocaled,
  asView,
  forOne,
  forEntry,
  asServed,
  forSpin,
  forEvent,
  ofThread,
  combine
} from '../../wrappers'

import { IconClasses, ViewEvents } from 'ore-fol-enums'

const { THREAD_ICON } = IconClasses
const { THREAD_UPDATE_EVENT } = ViewEvents

const debug = defineDebug('project:view:thread-edit')

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

  static get defaultProps () {
    return {}
  }

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

        <ApViewBody style={ { backgroundColor: color } }>
          <ThreadForm 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
  }

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

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

  handleSubmit () {
    const s = this
    const { use } = s.service('apiService')
    const { infoToast } = s.service('toastService')
    const { l, props } = s
    let { entryValues, threadId } = props
    return s.applySpinWhile(() =>
      use('threadApi')
        .then((threadApi) =>
          threadApi.update(threadId, entryValues)
        )
        .then((updated) => {
          infoToast(l('toasts.UPDATE_SUCCESS'))
          s.fireEvent(THREAD_UPDATE_EVENT, { updated })
          return s.handlePop()
            .then(s.handleEntryClear)
        })
        .catch(s.handleEntryErrorCatch)
    )
  }

  // --------------------
  // Callbacks
  // --------------------

  threadDidLoad (thread) {
    const s = this

    if (!s.isOwnThread(thread)) {
      console.warn('Try to edit thread not own:', thread.id)
      s.handlePop()
      return
    }

    let { name, description, color } = thread
    s.handleEntryUpdate({ name, description, color })
  }

  // --------------------
  // Dynamic Variables
  // --------------------
  get labels () {
    const s = this
    const { l } = s
    return {
      name: l('labels.THREAD_NAME'),
      description: l('labels.THREAD_DESCRIPTION'),
      color: l('labels.THREAD_COLOR'),
      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(),
  forOne(),
  forEntry(),
  forEvent(),
  ofThread(),
  asLocaled(),
  asView(),
  asConnected()
)(ThreadEditView)

