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

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

import { IconClasses, ViewEvents } from 'ore-fol-enums'
import { ThreadColorCodes } from 'ore-fol-codes'
const { THREAD_ICON } = IconClasses
const { THREAD_CREATE_EVENT } = ViewEvents
const { COLOR_A: DEFAULT_COLOR } = ThreadColorCodes

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

/** @lends ThreadCreateView */
class ThreadCreateView 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-create-view'
              className='view'
              spinning={ props.spinning }
              way={ props.way }
      >
        <ApViewHeader onLeftTap={ s.handlePop }
                      leftText={ null }
                      leftIcon={ props.popIcon }
                      titleText={ l('titles.NEW_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
    s.handleEntryUpdate({
      color: DEFAULT_COLOR
    })
  }

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

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

  // --------------------
  // Dynamic Variables
  // --------------------

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

  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_CREATE')
    }
  }

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

  handleSubmit () {
    const s = this
    const { props, l } = s
    const { use } = s.service('apiService')
    const { infoToast } = s.service('toastService')
    let { entryValues } = props
    return s.applySpinWhile(() =>
      use('threadApi')
        .then((threadApi) =>
          threadApi.create(entryValues)
        )
        .then((created) => {
          let { id: threadId } = created
          infoToast(l('toasts.CREATE_SUCCESS'))
          s.fireEvent(THREAD_CREATE_EVENT, { created })
          return s.handlePop()
            .then(s.handleEntryClear)
            .then(() => s.handlePush('ThreadDetailView', { threadId }))
        })
        .catch(s.handleEntryErrorCatch)
    )
  }
}

export default combine(
  asServed(),
  forSpin(),
  forEntry(),
  forEvent(),
  asLocaled(),
  asView(),
  asConnected()
)(ThreadCreateView)

