/**
 * PostForm component
 *
 * Generated by coz on 2016-12-25,
 * from a template provided by apeman-asset-templates.
 *
 * @function PostForm
 */
'use strict'

import React from 'react'
import defineDebug from 'debug'
import { clone } from 'asobj'
import {
  ApForm,
  ApFieldSet,
  withForm
} from 'apeman-react-basic'
import TextField from '../fields/text_field'
import SelectField from '../fields/select_field'
import UploadField from '../fields/upload_field'
import SubmitField from '../fields/submit_field'
import { PostTypeCodes } from 'ore-fol-codes'

const debug = defineDebug('project:fragment:post-form')
const { UPLOAD_POST, LINK_POST } = PostTypeCodes

/** @lends PostForm */
const PostForm = (props) => {
  let {
    values,
    labels,
    placeholders = {},
    types,
    idOf,
    onUpdate,
    onSubmit,
    errorList,
    errorStyle
  } = props
  let formProps = clone(props, {
    without: [
      'types',
      'onUpdate',
      'onCancel',
      'placeholders',
      'errors',
      'labels'
    ]
  })
  let { type = UPLOAD_POST } = values
  return (
    <ApForm { ...formProps }
            centered
            className='post-form'>
      <ApFieldSet plain>
        { errorList }
        { errorStyle }
        {
          types ? (
              <SelectField id={ idOf('type')}
                           label={ labels.type }
                           name='type'
                           value={ values.type }
                           onUpdate={ onUpdate }
                           options={ types }
              />
            ) : null
        }

        {
          type === UPLOAD_POST ? (
              <UploadField id={ idOf('image') }
                           label={ labels.image }
                           name='uploadedImage'
                           value={ values.image }
                           onUpdateRaw={ onUpdate }
              />

            ) : null
        }
        {
          type === LINK_POST ? (
              <TextField id={ idOf('image') }
                         label={ labels.image }
                         name='linkedImage'
                         value={ values.image }
                         rows={ 1 }
                         onUpdateRaw={ onUpdate }
              />
            ) : null
        }
        <TextField id={ idOf('text') }
                   label={ labels.text }
                   name='text'
                   value={ values.text }
                   rows={ 3 }
                   placeholder={ placeholders.text }
                   onUpdate={ onUpdate }
        />
        <SubmitField id={ idOf('submit')}
                     label={ labels.submit }
                     onSubmit={ onSubmit }
                     values={ values }
        />
      </ApFieldSet>
    </ApForm>
  )
}
export default withForm(PostForm)
