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

import React, { Component, PropTypes as types } from 'react'
import defineDebug from 'debug'
import {
  ApView,
  ApViewHeader,
  ApViewBody,
  ApButton,
  ApButtonGroup
} from 'apeman-react-basic'
import {
  asConnected,
  asLocaled,
  asView,
  asServed,
  forSpin,
  forOne,
  forEvent,
  ofPost,
  ofSigned,
  combine
} from '../../wrappers'
import { PostCard } from '../../fragments'
import { IconClasses, ViewEvents } from 'ore-fol-enums'

const { POST_DESTROY_EVENT } = ViewEvents
const { DESTROY_ICON } = IconClasses
const debug = defineDebug('project:view:post-destroy')

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

  static get defaultProps () {
    return {}
  }

  render () {
    const s = this
    const {
      props,
      l
    } = s

    let {
      post
    } = props
    return (
      <ApView id='post-destroy-view'
              className='view'
              spinning={ props.spinning }
              way={ props.way }
      >
        <ApViewHeader onLeftTap={ s.handlePop }
                      leftText={ null }
                      leftIcon={ props.popIcon }
                      titleText={ l('titles.DESTROY_POST') }
                      titleIcon={ DESTROY_ICON }
                      onRightTap={ null }
                      rightIcon={ null }
                      rightText={ null }
        />

        <ApViewBody>

          {
            post ? (
                <div className='post-destroy'>
                  <p className='lead'>{ l('messages.SURE_TO_DESTROY_POST') }</p>
                  <div className='card-wrap'>
                    <PostCard post={ post }/>
                  </div>
                  <ApButtonGroup>
                    <ApButton onTap={ s.handleSubmit }
                              wide
                              danger
                              className='danger-button'>
                      { l('buttons.DO_POST_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 { use } = s.service('apiService')
    const { infoToast } = s.service('toastService')
    const { props, l } = s
    let { postId } = props
    return use('postApi').then((postApi) =>
      s.applySpinWhile(() =>
        postApi.destroy(postId)
          .then((destroyed) => {
            infoToast(l('toasts.DESTROY_SUCCESS'))
            s.fireEvent(POST_DESTROY_EVENT, { destroyed })
            s.handlePop()
          })
      )
    )
  }

}

export default combine(
  asServed(), forSpin(),
  forOne(),
  forEvent(),
  ofPost(),
  ofSigned(),
  asLocaled(), asView(),
  asConnected()
)(PostDestroyView)

