/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

function getLoading () {
  const { extractComponentStyle } = weex
  const { dispatchNativeEvent } = weex.utils

  return {
    name: 'weex-loading',
    props: {
      pullHeight: {
        type: Number,
        default: 0
      },
      display: {
        type: String,
        default: 'show',
        validator (value) {
          return ['show', 'hide'].indexOf(value) !== -1
        }
      }
    },
    data () {
      return {
        height: -1,
        viewHeight: 0
      }
    },
    mounted () {
      this.viewHeight = this.$el.offsetHeight
      this.$el.style['justify-content'] = 'flex-start'
      this.$el.style.transition = `height .2s`
      if (this.display === 'hide') {
        this.height = 0
      }
      else {
        this.height = this.viewHeight
      }
      this.setMaxPullHeight(this.pullHeight)
    },
    watch: {
      pullHeight (val) {
        this.setMaxPullHeight(val)
      },
      height (val) {
        this.$el.style.height = val + 'px'
      },
      display (val) {
        if (val === 'hide') {
          this.height = 0
        }
        else {
          this.height = this.viewHeight
        }
      }
    },
    methods: {
      setMaxPullHeight (val) {
        if (!this.$el) {
          return
        }
        if (val < 0) {
          this.$el.style.maxHeight = ''
        }
        else if (val > 0) {
          this.$el.style.maxHeight = val + 'px'
        }
        else {
          this.$el.style.maxHeight = this.viewHeight + 'px'
        }
      },
      pulling (offsetY = 0) {
        this.height = offsetY
      },
      pullingUp (offsetY) {
        this.$el.style.transition = `height 0s`
        this.pulling(offsetY)
      },
      pullingEnd () {
        this.$el && (this.$el.style.transition = `height .2s`)
        if (this.height >= this.viewHeight) {
          this.pulling(this.viewHeight)
          if (this.$el) {
            dispatchNativeEvent(this.$el, 'loading')
          }
        }
        else {
          this.pulling(0)
        }
      },
      getChildren () {
        // 解决loading遮挡底下内容的问题
        return this.$slots.default || []
        // const children = this.$slots.default || []
        // if (this.display === 'show') {
        //   return children
        // }
        // return children.filter(vnode => {
        //   return !(vnode.componentOptions
        //     && vnode.componentOptions.tag === 'loading-indicator')
        // })
      }
    },
    render (createElement) {
      let parent = this.$parent
      while (parent) {
        if (['scroller', 'list', 'waterfall'].includes(parent.weexType)) {
          parent._refresh = this
          break
        }
        else {
          parent = parent.$parent
        }
      }
      return createElement('aside', {
        ref: 'loading',
        attrs: { 'weex-type': 'loading' },
        staticClass: 'weex-loading weex-ct',
        staticStyle: extractComponentStyle(this)
      }, this.getChildren())
    }
  }
}

export default {
  init (weex) {
    weex.registerComponent('loading', getLoading(weex))
  }
}
