/**
* @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,
    },
    tapDelay: {
      type: Number,
      default: 200,
    },
    tapTimeout: {
      type: Number,
      default: 700,
    },
  },
  data () {
    return {
      fontSize: Number(window.document.documentElement.style.fontSize.replace('px', '')),
      winHeight: window.innerHeight,
      winWidth: window.innerWidth,
      height: 0,
      firstClickTime: 0,
      lastClickTime: 0,
      startY: 0, // 触摸位置
      endY: 0, // 结束位置
      distance: 0, // android应该滚动距离
      docmHeight: 0, // 默认屏幕高度
      showHeight: 0, // 实时屏幕高度
      topHeight: 0, // content距离顶部的高度
      contentsHeight: 0, // 内容区域本来高度
      isResize: false, // 默认屏幕高度是否已获取
    }
  },
  watch: {
    'showHeight': function () {
      if (detectOS() !== 'ios') {
        if (this.docmHeight >= this.showHeight) {
        // 软键盘弹出的事件处理
          let activeElement = document.activeElement
          let offsetTop = activeElement.offsetTop
          let offsetParent = activeElement.offsetParent
          let viewTop = offsetParent.clientHeight
          this.topHeight = this.$el.offsetTop
          if (activeElement === document.body) {
            return
          }
          while (offsetParent !== document.body) {
            offsetTop += offsetParent.offsetTop
            offsetParent = offsetParent.offsetParent
          }
          offsetTop = this.docmHeight - offsetTop - viewTop < 0 ? offsetTop - this.topHeight - viewTop : 0
          if (offsetTop > 0) {
            this.$el.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s'
            this.$el.style.transform = 'translate(0px, -' + this.distance + 'px) scale(1) translateZ(0px)'
          }
        } else {
        // 软键盘收起的事件处理
          this.$el.style.height = parseInt(this.contentsHeight) + 'px'
          this.$el.style.transform = 'translate(0px,  0px) scale(1) translateZ(0px)'
        }
      }
    },
  },
  mounted () {
    if (this.calContent) {
      this.contentHeight()
    }
    this.contentsHeight = this.$el.style.height.slice(0, this.$el.style.height.length - 2)
    document.body.addEventListener('focusout', () => {
      // 软键盘收起的事件处理
      this.$el.style.height = parseInt(this.contentsHeight) + 'px'
      this.$el.style.transform = 'translate(0px,  0px) scale(1) translateZ(0px)'
      this.$el.style.paddingBottom = '0px'
    })
    // 当视图窗口大小发生改变时
    window.onresize = () => {
      return (() => {
        if (!this.isResize) {
          // 默认屏幕高度
          this.docmHeight = document.documentElement.clientHeight
          this.isResize = true
        }
        // 实时屏幕高度
        this.showHeight = document.body.clientHeight
      })()
    }
  },
  methods: {
    touchStart (event) {
      let keyboardHeight = this.getKeyBoardHeight()
      this.firstClickTime = event.timeStamp
      if ((event.timeStamp - this.lastClickTime) < this.tapDelay) return
      if (event.target.readOnly) return
      if (event.target.nodeName === 'INPUT' || event.target.nodeName === 'TEXTAREA') {
        this.startY = event.changedTouches[0].clientY
        this.distance = (innerHeight - this.startY) < keyboardHeight ? (keyboardHeight - (innerHeight - this.startY) + 20) : 0
      }
    },
    touchEnd (event) {
      if (detectOS() === 'ios') {
        if ((event.timeStamp - this.lastClickTime) < this.tapDelay) return
        this.lastClickTime = event.timeStamp
        if (event.target.nodeName === 'INPUT' || event.target.nodeName === 'TEXTAREA') {
          this.endY = event.changedTouches[0].clientY
          if ((this.endY - this.startY > 100) || this.endY - this.startY < -100) return
          let keyboardHeight = this.getKeyBoardHeight()
          let scrollTop = (this.winHeight - this.endY) < keyboardHeight ? (keyboardHeight - (this.winHeight - this.endY)) : 0
          if (scrollTop) {
            this.$el.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s'
            this.$el.style.transform = 'translate(0px, -' + scrollTop + 'px) scale(1) translateZ(0px)'
            this.$el.style.paddingBottom = (scrollTop + 20) + 'px'
            this.$el.style.height = parseInt(this.height) + scrollTop + 'px'
          }
          event.target.focus()
        }
      }
    },
    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 () {
      var innerWidth = window.innerWidth
      if (detectOS() === 'ios') {
        if (innerWidth >= 375 && innerHeight >= 812) {
          return 460
        }
        return 400
      } else {
        return 330
      }
    },
  },
}
</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>
