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

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

import { IconClasses, ViewEvents } from 'ore-fol-enums'
import { PostCard, PostDetailStyle, BanForm } from '../../fragments'
import { BanTypeCodes } from 'ore-fol-codes'
const { BAN_ICON } = IconClasses
const { POST_BAN_EVENT, POST_UNBAN_EVENT } = ViewEvents
const debug = defineDebug('project:view:post-ban')

const {
  WRONG_THREAD_BAN,
  VIOLATION_BAN,
  INAPPROPRIATE_BAN,
  OTHER_REASON_BAN
} = BanTypeCodes

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

  static get defaultProps () {
    return {}
  }

  render () {
    const s = this
    const {
      props,
      l,
      labels,
      errors,
      banTypes,
      placeholders
    } = s
    let {
      post
    } = props
    let target = post ? (<PostCard post={ post }/>) : null
    return (
      <ApView id='post-ban-view'
              className='view'
              spinning={ props.spinning }
              way={ props.way }
      >
        <ApViewHeader onLeftTap={ s.handlePop }
                      leftText={ null }
                      leftIcon={ props.popIcon }
                      titleText={ l('titles.POST_BAN') }
                      titleIcon={ BAN_ICON }
                      onRightTap={ null }
                      rightIcon={ null }
                      rightText={ null }
        />

        <ApViewBody>
          {
            post ? (
                <div className='post-ban'
                     id={ `post-ban-${post.id}` }>
                  <PostDetailStyle prefix={ `#post-ban-${post.id}` }
                                   color={ post.thread.color }
                  />
                  {
                    post.banned ? (
                        <div className='post-unban'>
                          <p className='lead'>{ l('messages.POST_IS_BANED') }</p>
                          <ApButton onTap={ s.handleUnban }>
                            { l('buttons.DO_UNBAN') }
                          </ApButton>
                        </div>
                      ) : (
                        <div className='post-bun'>
                          <p className='lead'>{ l('leads.BAN_POST_AS_OWNER') }</p>
                          <BanForm labels={ labels }
                                   types={ banTypes }
                                   target={ target }
                                   placeholders={ placeholders }
                                   spinning={ props.formSpinning }
                                   values={ props.entryValues }
                                   errors={ errors }
                                   onSubmit={ s.handleSubmit }
                                   onUpdate={ s.handleEntryUpdate }/>
                        </div>
                      )
                  }
                </div>
              ) : null
          }
        </ApViewBody>
      </ApView>
    )
  }

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

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

  componentDidMount () {
    debug('didMount')
    const s = this
    s.handleEntryUpdate({ type: WRONG_THREAD_BAN })
  }

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

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

  handleUnban () {
    const s = this
    const { use } = s.service('apiService')
    const { infoToast } = s.service('toastService')
    const { props, l } = s
    let { postId } = props
    return s.applySpinWhile(() =>
      use('banApi')
        .then((banApi) =>
          banApi.unbanPost(postId)
        )
        .then(() => {
          infoToast(l('toasts.UNBAN_SUCCESS'))
          s.fireEvent(POST_BAN_EVENT, { postId })
          return s.handlePop()
            .then(s.handleEntryClear)
        })
        .catch(s.handleEntryErrorCatch)
    )
  }

  handleSubmit () {
    const s = this
    const { use } = s.service('apiService')
    const { infoToast } = s.service('toastService')
    const { props, l } = s
    let { postId, entryValues } = props
    let { type, reason } = entryValues
    return s.applySpinWhile(() =>
      use('banApi')
        .then((banApi) =>
          banApi.banPost(postId, { type, reason })
        )
        .then(() => {
          infoToast(l('toasts.BAN_SUCCESS'))
          s.fireEvent(POST_UNBAN_EVENT, { postId })
          return s.handlePop()
            .then(s.handleEntryClear)
        })
        .catch(s.handleEntryErrorCatch)
    )
  }

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

  get labels () {
    const s = this
    const { l } = s
    return {
      type: l('labels.BAN_TYPE'),
      target: l('labels.BAN_TARGET'),
      reason: l('labels.BAN_REASON'),
      submit: l('buttons.DO_BAN')
    }
  }

  get placeholders () {
    const s = this
    const { l } = s
    return {
      reason: l('placeholders.BAN_REASON')
    }
  }

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

  get banTypes () {
    const s = this
    const { l } = s
    return {
      [WRONG_THREAD_BAN]: l('labels.BAN_WRONG_THREAD'),
      [VIOLATION_BAN]: l('labels.BAN_VIOLATION'),
      [INAPPROPRIATE_BAN]: l('labels.BAN_INAPPROPRIATE'),
      [OTHER_REASON_BAN]: l('labels.BAN_OTHER_REASON')
    }
  }

}

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

