/*
*
* 1、url地址，如正式环境企业详情：https://plug.jiansheku.com/enterprise/56546856314e567a69?ak=bc8e534da5ea40639b2f084919280a82&initTime=1694507179060&uid=4a3b4bfdfbca4bff9d9bd7c8fd42f533（ak：接口获得的accesToken；initTime：获得accesToken时的时间戳；uid：用户标识）
*
* */
const steerScroll = function(iframeId, navigation, footHeight, state, parentId, _this) { // iframeId: iframe的id；navigation:页面排除iframe后剩下的顶部高度；footHeight: 页面排除iframe后剩下的底部高度；state:监听or移除监听；parentId: 父级id[不带默认就是铺满整个页面]];_this:指向当前实例（可忽略）
  let dom = window
  if (parentId) { // 默认页面可以滚动
    dom = document.getElementById(parentId)
    if (!dom) {
      return
    }
    dom.style.overflow = 'auto'
  } else {
    document.body.style.overflow = 'visible'
  }
  if (state) {
    window.addEventListener('message', function(e) {
      const data = e.data
      const sct = parentId ? dom.scrollTop : document && document.documentElement.scrollTop || document && document.body.scrollTop
      if (data && typeof data === 'object') {
        // 动态设置iFrame高度
        if (data.height) {
          document.getElementById(iframeId).style.height = data.height + 'px'
          scrolling(iframeId, navigation, footHeight, parentId) // 初始加载获取滚动条距离顶部高度
        }
        // 插件当前路由
        if (_this && data.currentPath) {
          _this.currentRoute = data
          _this.searchFlag = data.currentName.includes('search-') ? true : false
        }
        // 点击企业详情页 栏目名动态设置滚动高度
        if (data.scrollHeight) {
          const navHeight = navigation.isFixed && !parentId ? navigation.totalHeight - navigation.fixedHeight : !parentId ? navigation.totalHeight : 0
          dom.scrollTo(sct, parseInt(data.scrollHeight) + navHeight)
        }
        // 点击企业详情页 栏目下拉子标签动态设置滚动高度
        if (data.clientHeight) {
          dom.scrollTo(sct, sct - parseInt(data.clientHeight))
        }
        // 点击下拉子标签动态设置滚动高度
        if (data.initHeight >= 0) {
          dom.scrollTo(sct, data.initHeight)
        }
        // 根据子页面弹窗显示隐藏控制当前页面是否固定不可滚动
        if (data.scrollDisabled || data.scrollDisabled === false) {
          if (parentId) {
            dom.style.overflow = data.scrollDisabled ? 'hidden' : 'auto'
          } else {
            document.body.style.overflow = data.scrollDisabled ? 'hidden' : 'visible'
          }
        }
      }
    })
    dom.addEventListener('scroll', (event) => {
      scrolling(iframeId, navigation, footHeight, parentId)
    })
  } else {
    dom.removeEventListener('scroll', (event) => {
      scrolling(iframeId, navigation, footHeight, parentId)
    })
  }
}

const scrolling = function(iframeId, navigation, footHeight, parentId) {
  // 滚动条距文档顶部的距离
  let scrollTop = parentId ? document.getElementById(parentId).scrollTop : window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
  // 将滚动距离传入子组件
  const ifa = document.getElementById(iframeId)
  scrollTop = scrollTop + navigation.totalHeight
  let rippleHeight = 0  //插件筛选条件浮动层距离底部距离
  let scrollGap = parentId ? document.getElementById(parentId).scrollTop : window.scrollTop || document.body.scrollTop || document.documentElement.scrollTop
  let scrollHeight = parentId ? document.getElementById(parentId).offsetHeight : window.scrollHeight || document.body.scrollHeight || document.documentElement.scrollHeight
  let domHeight = parentId ? document.getElementById(parentId).clientHeight : window.clientHeight || document.body.clientHeight || document.documentElement.clientHeight
  if(parentId){ //网页内部嵌套时
    rippleHeight = scrollHeight-scrollGap-domHeight
  }else{  //
    if(scrollHeight-scrollGap-domHeight <= footHeight){
      rippleHeight = 0
    }else{
      rippleHeight = scrollHeight-scrollGap-domHeight - footHeight
    }
  }
  ifa.contentWindow.postMessage({ 'scrollTop': scrollTop, 'navHeight': navigation.totalHeight, 'rippleHeight': rippleHeight }, '*')
}

export {
  steerScroll
}
