/**
 * Style for ApView.
 * @class ApViewStyle
 */

'use strict'

import React, { Component, PropTypes as types } from 'react'
import { ApStyle } from 'apeman-react-style'

/** @lends ApViewStyle */
class ApViewStyle extends Component {
  render () {
    const s = this
    let { props } = s

    let { all, small, medium, large } = ApViewStyle.styleData(props)

    return (
      <ApStyle data={ Object.assign(all, props.style) }
               smallMediaData={ small }
               mediumMediaData={ medium }
               largeMediaData={ large }
      >{ props.children }</ApStyle>
    )
  }
}

Object.assign(ApViewStyle, {

  propTypes: {
    style: types.object,
    sidePadding: types.number,
    headerHeight: types.number,
    highlightColor: types.string,
    backgroundColor: types.string
  },

  defaultProps: {
    style: {},
    sidePadding: 8,
    headerHeight: 44,
    highlightColor: ApStyle.DEFAULT_HIGHLIGHT_COLOR,
    backgroundColor: ApStyle.DEFAULT_BACKGROUND_COLOR,
    footerBackgroundColor: ApStyle.COVER_BACKGROUND
  },
  styleData (config) {
    let {
      headerHeight,
      backgroundColor,
      sidePadding,
      footerBackgroundColor
    } = config
    let sidebarRate = 0.8
    let sideTransitionTimeout = 400
    return {
      all: {
        '.ap-view': {
          position: 'relative',
          overflow: 'auto'
        },
        '.ap-view-wide .ap-view-body-inner.ap-container': {
          maxWidth: 'none'
        },
        '.ap-view-headerless .ap-view-body': {
          paddingTop: 0
        },
        '.ap-view-headerless .ap-view-header': {
          display: 'none'
        },
        '.ap-view-inner': {
          position: 'absolute',
          left: 0,
          top: 0,
          right: 0,
          bottom: 0
        },
        '.ap-view-header': {
          position: 'absolute',
          left: 0,
          top: 0,
          right: 0,
          height: `${headerHeight}px`,
          display: 'block',
          lineHeight: `${headerHeight}px`,
          padding: 0,
          margin: 0,
          zIndex: 1,
          boxSizing: 'border-box'
        },
        '.ap-view-header .ap-container': {
          borderBottom: '1px solid #F5F5F5'
        },
        '.ap-view-header-inner': {
          display: 'flex',
          padding: 0,
          margin: '0 auto'
        },
        '.ap-view-header-content': {
          width: '100%',
          flex: 1
        },
        '.ap-view-body': {
          padding: `${headerHeight + 8}px ${sidePadding}px 8px`,
          margin: 0,
          display: 'block',
          backgroundColor: `${backgroundColor}`
        },
        '.ap-view-body-inner': {
          margin: '0 auto'
        },
        '.ap-view-header-button': {
          display: 'inline-block',
          cursor: 'pointer',
          textAlign: 'center',
          lineHeight: `${headerHeight}px`
        },
        '.ap-view-header-button:hover': {
          opacity: 0.9
        },
        '.ap-view-header-button:active': {
          opacity: 0.66
        },
        '.ap-view-header-button-alt': {
          display: 'inline-block',
          minWidth: 44
        },
        '.ap-view-header-button-inner': {
          display: 'inline-block',
          padding: '0 4px',
          minWidth: '44px'
        },
        '.ap-view-header-button-aligner': {
          display: 'inline-block',
          width: '1px',
          overflow: 'hidden',
          opacity: 0,
          color: 'transparent',
          marginRight: '-1px',
          lineHeight: `${headerHeight}px`,
          height: `${headerHeight}px`,
          verticalAlign: 'middle'
        },
        '.ap-view-header-ion': {
          display: 'inline-block',
          lineHeight: `${headerHeight}px`,
          verticalAlign: 'middle'
        },
        '.ap-view-header-button-text': {
          lineHeight: `${headerHeight}px`,
          verticalAlign: 'middle'
        },
        '.ap-view-header-title': {
          margin: '0 auto',
          display: 'block',
          verticalAlign: 'middle',
          fontWeight: 'normal',
          fontSize: '14px',
          textAlign: 'center'
        },
        '.ap-view-header-title-inner': {
          display: 'inline-flex',
          height: `${headerHeight}px`,
          alignItems: 'center'
        },
        '.ap-view-footer': {
          position: 'relative',
          display: 'block',
          width: '100%'
        },
        '.ap-view-footer-space': {
          display: 'none'
        },
        '.ap-view-footer-fixed': {},
        '.ap-view-footer-content': {
          boxSizing: 'border-box'
        },
        '.ap-view-footer-inner': {
          padding: `${sidePadding}px ${sidePadding}px`,
          background: `${footerBackgroundColor}`,
          borderStyle: 'solid',
          borderWidth: '1px 1px 0',
          borderColor: '#F0F0F0',
          boxSizing: 'border-box'
        },
        '.ap-view-footer-fixed .ap-view-footer-content': {
          position: 'fixed',
          left: 0,
          right: 0,
          bottom: 0,
          zIndex: 4
        },
        '.ap-view-footer-fixed .ap-view-footer-space': {
          display: 'block'
        },
        '.ap-view-sidebar': {
          overflow: 'visible',
          height: 0,
          width: 0,
          position: 'static'
        },
        '.ap-view-sidebar-enabled': {},
        '.ap-view-sidebar-inner': {
          position: 'absolute',
          left: 0,
          top: 0,
          bottom: 0,
          right: 0,
          zIndex: 8,
          display: 'flex',
          justifyContent: 'flex-start',
          background: 'rgba(0,0,0,0.3)',
          boxSizing: 'border-box'
        },
        '.ap-view-sidebar-content': {
          background: backgroundColor,
          width: `${100 * sidebarRate}%`,
          height: '100%',
          position: 'relative',
          overflowY: 'auto',
          borderRight: '1px solid #F0F0F0',
          display: 'block',
          boxShadow: '1px 1px 2px rgba(0, 0, 0, 0.1)'
        },
        '.ap-view-sidebar-closer': {
          width: `${100 * (1 - sidebarRate)}%`,
          height: '100%',
          display: 'block'
        },
        '.ap-view-sidebar-transition-enter': {
          background: 'rgba(0,0,0,0)'
        },
        '.ap-view-sidebar-transition-enter .ap-view-sidebar-content': {
          left: `${-100 * sidebarRate}%`
        },
        '.ap-view-sidebar-transition-enter.ap-view-sidebar-transition-enter-active': {
          background: 'rgba(0,0,0,0.3)',
          transition: `background ${sideTransitionTimeout}ms`
        },
        '.ap-view-sidebar-transition-enter.ap-view-sidebar-transition-enter-active .ap-view-sidebar-content': {
          left: '0',
          transition: `left ${sideTransitionTimeout}ms`
        },
        '.ap-view-sidebar-transition-leave': {
          background: 'rgba(0,0,0,0.3)'
        },
        '.ap-view-sidebar-transition-leave .ap-view-sidebar-content': {
          left: '0'
        },
        '.ap-view-sidebar-transition-leave.ap-view-sidebar-transition-leave-active': {
          background: 'rgba(0,0,0,0)',
          transition: `background ${sideTransitionTimeout}ms`
        },
        '.ap-view-sidebar-transition-leave.ap-view-sidebar-transition-leave-active .ap-view-sidebar-content': {
          left: `${-100 * sidebarRate}%`,
          transition: `left ${sideTransitionTimeout}ms`
        }
      }
    }
  }
})

export default ApViewStyle
