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

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

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

const { POST_ICON } = IconClasses
const debug = defineDebug('project:view:my-post-list')

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

  static get defaultProps () {
    return {}
  }

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

        <ApViewBody>
          {
            posts ? (
                <div>
                  <PostList wrap
                            simple
                            posts={ posts.entities }
                            onDetail={ s.handleDetail }
                            alt={ l('alt.NO_POST_YOU_HAVE') }
                            hasMore={ posts.more }
                            moreText={ l('buttons.SHOW_MORE') }
                            onMore={ s.handleMore }
                            moreSpinning={ props.moreSpinning }
                  />
                </div>
              ) : null
          }
        </ApViewBody>
      </ApView>
    )
  }

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

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

  componentDidMount () {
    debug('didMount')
    const s = this

    s.loadMyPosts()
  }

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

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

  handleDetail (post) {
    const s = this
    const { urlForPost } = s.service('urlService')
    s.followLink(urlForPost(post))
  }

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

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

  loadMyPosts () {
    const s = this
    const { use } = s.service('apiService')
    return use('postApi').then((postApi) =>
      s.applySpinWhile(() =>
        s.applyListLoad({
          myPosts: () => postApi.listOwn({})
        })
      )
    )
  }

  loadMoreMyPosts ({ offset }) {
    const s = this
    const { use } = s.service('apiService')
    return use('postApi').then((postApi) =>
      s.applySpinWhile({
        moreSpinning: () =>
          s.applyListMore({
            myPosts: () => postApi.listOwn({ offset })
          })
      })
    )
  }

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

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

}

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

