import { isRealNum } from './index'
/**
 * 窗口大小变化监听
 * @param {*} handler 
 */
export const addWindowResizeHandler = function (handler) {
  let oldHandler = window.onresize
  if (typeof window.onresize != 'function') {
    window.onresize = handler
  } else {
    window.onresize = function () {
      oldHandler()
      handler()
    }
  }
}

/**
 * 获取窗口大小
 * @returns 
 */
export const getClientHeight =function(){
  let clientHeight = window.innerHeight //
  if (!clientHeight) {
    clientHeight = document.documentElement.clientHeight
  }
  return clientHeight
}
/**
 * 获取组件的高度
 * @param {Object} leftRef 
 * @param {Number} altitudeDiff 高度差微调
*/
export const queryTargetHeight = function (leftRef,altitudeDiff) {
  let targetHeight = 500;
  let clientHeight = getClientHeight()
  if(!leftRef){
    return  clientHeight
  }
  if (Array.isArray(leftRef)) {
    leftRef = leftRef[0]
  }
  if (leftRef && leftRef.$el) {
    leftRef = leftRef.$el
  }
  let boundClentReact = leftRef.getBoundingClientRect()
  if (boundClentReact) {
    let offsetTop = boundClentReact.top
    targetHeight = clientHeight - offsetTop
  }

  let nextTotalHeight = queryNextDomHeight(leftRef)
  if (isRealNum(nextTotalHeight) && nextTotalHeight < targetHeight) {
    targetHeight -= nextTotalHeight
  }
  let parentMarginPadHeights = getParentMarginPad(leftRef)
  if (isRealNum(parentMarginPadHeights) && parentMarginPadHeights < targetHeight) {
    targetHeight -= parentMarginPadHeights
  }
  if(isRealNum(altitudeDiff)){
    targetHeight += altitudeDiff
  }
  return targetHeight 
}

/**
 * 获取下一个元素的高度
 * @param {*} domRef 
 */
export const queryNextDomHeight = function (domRef) {
  let nextDom = domRef.nextSibling
  let height = 0, marginTopHeigth = 0, marginBottomHeight = 0
  while (nextDom) {
    let domStyle = getDomStyle(nextDom)
    marginTopHeigth = pixelToNumber(domStyle?.marginTop)
    marginBottomHeight = pixelToNumber(domStyle?.marginBottom)
    height += isRealNum(marginBottomHeight) ? marginBottomHeight : 0
    height += isRealNum(marginTopHeigth) ? marginTopHeigth : 0
    height += getDomHeight(nextDom).height
    nextDom = nextDom.nextSibling
  }
  return height;
}

/**
 * 获取元素高度
 * @param {*} domRef 
 * @returns 
 */
export const getDomHeight = function(domRef){
  try{
    let targetRef = domRef
    if (targetRef && targetRef.$el) {
      targetRef = targetRef.$el
    }
    return targetRef?.getBoundingClientRect().height
  }catch(err){
    // console.error(err)
  }
  return 0;
}
/**
 * 获取组件样式
 * @param {*} domRef 
 * @returns 
 */
export const getDomStyle = function (domRef) {
  let domStyle = null
  try{
    if (window.getComputedStyle) {
      domStyle = window.getComputedStyle(domRef, null)
    } else {
      domStyle = domRef.currentStyle// IE 写法
    }
  }catch(err){
    // console.error(err)
    return {
      marginTop:0,
      marginBottom:0,
      marginRight:0,
      marginLeft:0,
      paddingBottom:0,
      paddingTop:0,
      paddingRight:0,
      paddingLeft:0
    }
  }
  return domStyle
}

/**
 * 像素转换 数字
 * @param {*} val 
 */
export const pixelToNumber = function (val) {
  if (!val) {
    return 0;
  }
  val = val.toLocaleLowerCase()
  if (typeof val === 'string' && val.indexOf('px') > 0) {
    val = val.replace('px', '').trim()
    return Math.ceil(val)
  }
  return val
}

/**
 * 获取parent marginBottom 和 paddingBottom 
 * @param {*} domRef 
 */
export const getParentMarginPad = function (domRef) {
  let parentDom = domRef.parentNode
  if (!parentDom) {
    return 0
  }
  let className = parentDom.getAttribute('class')
  let tagName = parentDom.tagName
  let marginPadHeight = 0, marginBottomHeight = 0, paddingBottomHeight = 0
  while (parentDom && className && className.indexOf('app-main') < 0 && tagName!=='BODY') {
    let domStyle = getDomStyle(parentDom)
    marginBottomHeight = pixelToNumber(domStyle?.marginBottom)
    paddingBottomHeight = pixelToNumber(domStyle?.paddingBottom)
    marginPadHeight += isRealNum(marginBottomHeight) ? marginBottomHeight : 0
    marginPadHeight += isRealNum(paddingBottomHeight) ? paddingBottomHeight : 0

    if (parentDom.nextSibling) {
      marginPadHeight += queryNextDomHeight(parentDom)
    }
    parentDom = parentDom.parentNode
    className = parentDom.getAttribute('class')
    tagName = parentDom.tagName
  }
  return marginPadHeight
}