/**
 * CrawlNichan view
 *
 * Generated by coz on 2017-01-03,
 * from a template provided by apeman-asset-templates.
 *
 * @function CrawlNichanView
 */
'use strict'

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

import { IconClasses } from 'ore-fol-enums'
const debug = defineDebug('project:view:crawl-nichan')
const { CRAWL_ICON } = IconClasses

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

  static get defaultProps () {
    return {}
  }

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

        <ApViewBody>
          <NichanCrawlForm labels={ labels }
                           placeholders={ placeholders }
                           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

    s.handleEntryUpdate({
      url: ''
    })
  }

  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 { url } = props.entryValues
    return s.applySpinWhile(() =>
      use('crawlApi').then((crawlApi) =>
        crawlApi
          .crawlNichan({ url })
          .then(({ id: crawlId }) => {
            infoToast(l('toasts.CRAWL_SUCCESS'))
            s.handlePush('CrawlResultView', { crawlId })
          })
      )
        .catch(s.handleEntryErrorCatch)
    )
  }

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

  get labels () {
    const s = this
    const { l } = s
    return {
      lead: l('leads.NICHAN_CRAWL'),
      url: l('labels.CRAWL_URL'),
      submit: l('buttons.DO_CRAWL')
    }
  }

  get placeholders () {
    const s = this
    const { l } = s
    return {
      url: l('placeholders.NICHAN_URL')
    }
  }

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

}

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

