React = require 'react'
PropTypes = require 'prop-types'
createClass = require 'create-react-class'
assign = require 'lodash/assign'

animationMixin = require '../mixins/animation_mixin'

Pvr = React.createFactory(require './pvr')
PvrInfoItem = React.createFactory(require './pvr_info_item')

{div} = require 'react-dom-factories'

###
Info Popover Props

@props.items
  array of objects containing at minimum a label and value attribute
  optionally a subLabel property can be passed

@props.itemHeight
  the height of each item row

@props.styleMixin
  object containing any style properties to mixin with and/or overrride the defaults
  note that width height are passed separately so they can have defaults and auto settings
  passing widt/height in this object could cause issues

@props.headerTitle
  optional title String for popover header

@props.headerClass
  optional class for popover header

@props.className
  optional class for whole popover. default is 'plain-pvr'. 'plain-pvr' class should be 
  included if a custom class is passed, i.e., className: 'plain-pvr custom-class'

@props.pvrProps
  properties germane to PVR wrapper: width, height, anchor, hAdjust, vAdjust, direction

###

InfoPvr = createClass
  
  displayName: 'InfoPvr'

  propTypes:
    close: PropTypes.func.isRequired  

  getDefaultProps: ->
    {
      items: []
      itemHeight: 35
      styleMixin: {}
      headerTitle: null
      headerClass: ''
      pvrProps: {}
      className: 'plain-pvr'
    }

  render: ->
    {items, itemHeight, styleMixin, className, headerTitle, headerClass, close} = @props
    
    style = {}
    pvrProps = {}

    scale = 1
    assign pvrProps, @props.pvrProps, {close, scale}

    className = className if className?

    @hasHeader = headerTitle?

    unless pvrProps.height?
      pvrProps.height = items.length * itemHeight + (items.length - 1) + 30
    if @hasHeader
      pvrProps.height += 34    

    assign style, styleMixin

    style.height = pvrProps.height

    if pvrProps.width
      style.width = pvrProps.width

    itemRows = []
    if items?
      for item in items
        itemRows.push PvrInfoItem {
          hasSubLabel: item.subLabel?
          item: item
          itemHeight: itemHeight
          key: item.key
        }

    if itemRows.length is 0
      itemRows = div {className: 'no-items'}, t 'No information to display'

    pvrProps.element = div {
      key: 'plain-pvr'
      className: className
      style: style
    }, [
        if @hasHeader
          div {
            key: 'header'
            className: "header plain-pvr-content-item #{headerClass}"
          }, headerTitle
        
        div {
          key: 'inner-rows'
          className: 'inner-rows'
        }, itemRows
      ]

    Pvr(pvrProps)

    
module.exports = InfoPvr
