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

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

import { IconClasses } from 'ore-fol-enums'

const { THREAD_ICON } = IconClasses

const debug = defineDebug('project:view:my-thread-list')

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

  static get defaultProps () {
    return {}
  }

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

        <ApViewBody>
          {
            threads ? (
                <div className='thread-list-wrap'>
                  <ThreadList threads={ threads.entities }
                              onDetail={ s.handleDetail }
                              alt={ l('alt.NO_THREAD_YOU_HAVE') }
                              hasMore={ threads.more }
                              moreText={ l('buttons.SHOW_MORE') }
                              onMore={ s.handleMore }
                              moreSpinning={ props.moreSpinning }
                  />
                </div>
              ) : (
                <div className="thread-list-loading">{ l('messages.NOW_LOADING') }</div>
              )
          }
        </ApViewBody>
      </ApView>
    )
  }

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

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

  componentDidMount () {
    debug('didMount')
    const s = this
    s.loadMyThreads()
  }

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

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

  handleDetail (thread) {
    const s = this
    const { urlForThread } = s.service('urlService')
    s.followLink(urlForThread(thread))
  }

  handleMore () {
    const s = this
    const { props } = s
    let { threads } = props
    let { next: offset } = threads.counts
    s.loadMoreMyThreads({ offset })
  }

  // --------------------
  // Custom
  // --------------------

  loadMyThreads () {
    const s = this
    const { use } = s.service('apiService')
    return s.applySpinWhile(() =>
      use('threadApi').then((threadApi) =>
        s.applyListLoad({
          myThreads: () => threadApi.listOwn()
        })
      )
    )
  }

  loadMoreMyThreads ({ offset }) {
    const s = this
    const { use } = s.service('apiService')
    return use('threadApi').then((threadApi) =>
      s.applySpinWhile({
        moreSpinning: () => s.applyListMore({
          myThreads: () => threadApi.listOwn({ offset })
        })
      })
    )
  }

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

  get title () {
    const s = this
    const { props, l } = s
    const { threads } = props
    const { totalText } = s.service('localeService')
    return l('titles.MY_THREADS', {
      vars: { total: totalText(threads && threads.counts) }
    })
  }

}

export default combine(
  asServed(),
  forSpin(),
  forList(),
  forLink(),
  ofSigned(),
  asLocaled(),
  asView(),
  asConnected(),
)(MyThreadsView)

