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

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

import { IconClasses, ViewEvents } from 'ore-fol-enums'
import { ThreadCard } from '../../fragments'

const debug = defineDebug('project:view:thread-destroy')
const { THREAD_ICON } = IconClasses
const { THREAD_DESTROY_EVENT } = ViewEvents

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

  static get defaultProps () {
    return {}
  }

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

        <ApViewBody>
          {
            thread ? (
                <div className="thread-destroy">
                  <div className="card-wrap">
                    <ThreadCard thread={ thread }/>
                    <p className="lead">{ l('messages.SURE_TO_DESTROY_THREAD') }</p>
                  </div>
                  <ApButtonGroup>
                    <ApButton onTap={ s.handleSubmit }
                              wide
                              danger
                              className='danger-button'>
                      { l('buttons.DO_THREAD_DESTROY') }
                    </ApButton>
                  </ApButtonGroup>
                </div>
              ) : null
          }
        </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 { l, props } = s
    const { use } = s.service('apiService')
    const { infoToast } = s.service('toastService')
    let { threadId } = props
    return use('threadApi').then((threadApi) =>
      s.applySpinWhile(() =>
        threadApi.destroy(threadId)
          .then((destroyed) => {
            infoToast(l('toasts.DESTROY_SUCCESS'))
            s.fireEvent(THREAD_DESTROY_EVENT, { destroyed })
            s.handlePop()
          })
      )
    )
  }

  // --------------------
  // Callbacks
  // --------------------
  threadDidLoad (thread) {
    const s = this

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

export default combine(
  asServed(),
  forSpin(),
  forOne(),
  forEvent(),
  ofThread(),
  ofSigned(),
  asLocaled(), asView(),
  asConnected()
)(ThreadDestroyView)

