/**
* @Author Think
* @Date 2018/11/24
*/

<template>
  <div
    class="content" @touchstart.capture="touchStart" @touchend.capture="touchEnd">
    <slot/>
  </div>
</template>

<script>
import { detectOS } from '../../common/utils/index'

export default {
  name: 'HContent',
  props: {
    calContent: {
      type: Boolean,
      default: true,
    },
  },
  data () {
    return {
      fontSize: Number(window.document.documentElement.style.fontSize.replace('px', '')),
      winHeight: window.innerHeight,
      winWidth: window.innerWidth,
      height: 0,
      startY: 0,
      endY: 0,
    }
  },
  mounted () {
    let vm = this
    if (this.calContent) {
      this.contentHeight()
    }

    window.addEventListener('native.keyboardshow', function (e) {
      setTimeout(function () {
        if (detectOS() === 'android') {
          vm.$el.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s'
          vm.$el.style.transform = 'translate(0px, -45px) scale(1) translateZ(0px)'
        } else {
          let keyBoardHeight = vm.getKeyBoardHeight()
          let scollHeight = (innerHeight - vm.endY) < keyBoardHeight ? (keyBoardHeight - (innerHeight - vm.endY)) : 0
          if (scollHeight) {
            vm.$el.style.paddingBottom = (scollHeight + 20) + 'px'
            vm.$el.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s'
            vm.$el.style.transform = 'translate(0px, -' + scollHeight + 'px) scale(1) translateZ(0px)'
          }
        }
      }, 300)
    })

    window.addEventListener('native.keyboardhide', function (e) {
      setTimeout(function () {
        vm.$el.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s'
        vm.$el.style.transform = 'translate(0px, 0px) scale(1) translateZ(0px)'
        vm.$el.style.paddingBottom = '0px'
      }, 300)
    })
  },
  methods: {
    touchStart (event) {
      if (detectOS() === 'ios') {
        if (event.target.readOnly) return
        if (event.target.nodeName === 'INPUT' || event.target.nodeName === 'TEXTAREA') {
          this.startY = event.changedTouches[0].clientY
        }
      }
    },
    touchEnd (event) {
      if (detectOS() === 'ios') {
        if (event.target.readOnly) return
        if (event.target.nodeName === 'INPUT' || event.target.nodeName === 'TEXTAREA') {
          this.endY = event.changedTouches[0].clientY
        }
      }
    },

    getHeaderHeight () {
      let vm = this
      let $el = vm.$el.previousElementSibling
      let headerHeight = 0
      do {
        if ($el) {
          let elHeight = window.getComputedStyle($el).height
          if (elHeight) {
            headerHeight += Number(elHeight.replace('px', ''))
          }
          if ($el._prevClass && $el._prevClass.indexOf('h-header') === 0) {
            if (detectOS() === 'ios' && vm.winWidth === 375 && vm.winHeight === 812) {
              headerHeight += 0.8 * vm.fontSize
            } else if (detectOS() === 'ios' && vm.winWidth === 414 && vm.winHeight === 896) {
              headerHeight += 0.8 * vm.fontSize
            } else if (detectOS() === 'ios') {
              headerHeight += 0.4 * vm.fontSize
            }
          }
          $el = $el.previousElementSibling
        }
      } while ($el)

      return headerHeight
    },
    getNextElementHeight () {
      let vm = this
      let nextElement = this.$el.nextElementSibling
      let nextHeight = 0
      do {
        if (nextElement) {
          let elHeight = window.getComputedStyle(nextElement).height
          if (elHeight) {
            nextHeight += Number(elHeight.replace('px', ''))
          }
          if (nextElement._prevClass && nextElement._prevClass.indexOf('h-bottom-tab') === 0) {
            if (detectOS() === 'ios' && vm.winWidth === 375 && vm.winHeight === 812) {
              nextHeight += vm.fontSize * 0.68
            } else if (detectOS() === 'ios' && vm.winWidth === 414 && vm.winHeight === 896) {
              nextHeight += vm.fontSize * 0.68
            }
          }
          nextElement = nextElement.nextElementSibling
        }
      } while (nextElement)
      return nextHeight
    },
    contentHeight () {
      let vm = this
      const headerHeight = vm.getHeaderHeight()
      const nextHeight = vm.getNextElementHeight()
      let content = vm.$el
      vm.height = (window.innerHeight - nextHeight - headerHeight)
      content.style.height = (window.innerHeight - nextHeight - headerHeight) + 'px'
    },
    getKeyBoardHeight () {
      let innerWidth = window.innerWidth
      if (detectOS() === 'ios') {
        if (innerWidth >= 375 && innerHeight >= 812) {
          return 460
        }
        return 400
      } else {
        return 275
      }
    },
  },
}
</script>

<style lang="less">
  .content {
    flex: 1;
    overflow: hidden;
    background-color: #fafafa;
    position: relative;
    overflow-y: scroll;
    height: 100%;
    -webkit-overflow-scrolling: touch;
    overflow-scrolling: touch;
  }

  // iPhoneX适配
  @media (device-width: 375px) and (device-height: 812px) and (-webkit-min-device-pixel-ratio: 3) {
    .platform-ios {
      .has-header {
        top: 84px;
      }
    }
  }

  // iPhoneX Max适配
  @media (device-width: 414px) and (device-height: 896px) {
    .platform-ios {
      .has-header {
        top: 84px;
      }
    }
  }
</style>
