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

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

import { IconClasses } from 'ore-fol-enums'
const { COLOR_A: DEFAULT_COLOR } = ThreadColorCodes

const { CRAWL_ICON } = IconClasses
const debug = defineDebug('project:view:crawl-result')

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

  static get defaultProps () {
    return {}
  }

  render () {
    const s = this
    const {
      props,
      labels,
      errors
    } = s

    let {
      crawl
    } = props
    return (
      <ApView id='crawl-result-view'
              className='view'
              spinning={ props.spinning }
              way={ props.way }
      >
        <ApViewHeader onLeftTap={ s.handlePop }
                      leftText={ null }
                      leftIcon={ props.popIcon }
                      titleText={ null }
                      titleIcon={ CRAWL_ICON }
                      onRightTap={ null }
                      rightIcon={ null }
                      rightText={ null }
        />

        <ApViewBody>
          {
            crawl ? (
                <div>
                  <CrawlApplyForm labels={ labels }
                                  crawl={ crawl }
                                  values={ props.entryValues }
                                  errors={ errors }
                                  onSubmit={ s.handleSubmit }
                                  onUpdate={ s.handleEntryUpdate }/>
                </div>
              ) : null
          }
        </ApViewBody>
      </ApView>
    )
  }

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

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

  componentDidMount () {
    debug('didMount')
    const s = this
    s.handleEntryUpdate({
      color: DEFAULT_COLOR
    })
    let { crawlId } = s.props
    s.loadCrawl(crawlId)
  }

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

    s.unloadCrawl()
  }

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

  handleSubmit () {
    const s = this
    const { l, props } = s
    const { use } = s.service('apiService')
    const { urlForThread } = s.service('urlService')
    const { infoToast } = s.service('toastService')
    const { crawl, entryValues } = props
    let { color } = (entryValues || {})
    return s.applySpinWhile(() =>
      use('crawlApi').then((crawlApi) =>
        crawlApi.threadify(crawl.id, { color })
          .then(({ thread, posts }) => {
            infoToast(l('toasts.CRAWL_THREADIFY_SUCCESS'))
            s.followLink(urlForThread(thread))
          })
      )
    )
  }

  // --------------------
  // Custom
  // --------------------
  loadCrawl (crawlId) {
    const s = this
    const { use } = s.service('apiService')
    return use('crawlApi').then((crawlApi) =>
      s.applySpinWhile(() =>
        s.applyOneLoad({
          crawl: () => crawlApi.oneOwn(crawlId)
        }).then(({ crawl }) =>
          crawl ? true : s.handlePop()
        )
      )
    )
  }

  unloadCrawl () {
    const s = this
    return s.applyOneUnload([
      'crawl'
    ])
  }

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

  get labels () {
    const s = this
    const { l } = s
    return {
      color: l('labels.THREAD_COLOR'),
      preview: l('labels.THREAD_PREVIEW'),
      submit: l('buttons.DO_THREADIFY')
    }
  }

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

}

export default combine(
  asServed(),
  forSpin(),
  forOne(),
  forLink(),
  forEntry(),
  ofSigned(),
  asLocaled(),
  asView(),
  asConnected()
)(CrawlResultView)

