package com.yidian.bcommon.refresh

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshComponent
import com.scwang.smart.refresh.layout.api.RefreshContent
import com.scwang.smart.refresh.layout.api.RefreshKernel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState

class CustomSmartRefreshLayout @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : SmartRefreshLayout(context, attributeSet) {
    init {
        mKernel = InnerImpl(RefreshKernelImpl())
    }
    private var onMoving: ((Int) -> Unit)? = null
    fun setOnLoadMoreMovingListener(onMoving: (Int) -> Unit) {
        this.onMoving = onMoving
    }
    inner class InnerImpl(private val realKernelImpl: RefreshKernel) : RefreshKernel {
        override fun requestNeedTouchEventFor(
            internal: RefreshComponent,
            request: Boolean
        ): RefreshKernel {
            realKernelImpl.requestNeedTouchEventFor(internal, request)
            return this
        }

        override fun requestRemeasureHeightFor(internal: RefreshComponent): RefreshKernel {
            realKernelImpl.requestRemeasureHeightFor(internal)
            return this
        }

        override fun moveSpinner(spinner: Int, isDragging: Boolean): RefreshKernel {

            onMoving?.invoke(spinner)
            realKernelImpl.moveSpinner(spinner, isDragging)
            return this
        }

        override fun getRefreshLayout(): RefreshLayout {
            return refreshLayout
        }

        override fun setState(state: RefreshState): RefreshKernel {
            realKernelImpl.setState(state)
            return this
        }

        override fun requestFloorBottomPullUpToCloseRate(rate: Float): RefreshKernel {
            realKernelImpl.requestFloorBottomPullUpToCloseRate(rate)
            return this
        }

        override fun requestDefaultTranslationContentFor(
            internal: RefreshComponent,
            translation: Boolean
        ): RefreshKernel {
            realKernelImpl.requestDefaultTranslationContentFor(internal, translation)
            return this
        }

        override fun requestFloorDuration(duration: Int): RefreshKernel {
            realKernelImpl.requestFloorDuration(duration)
            return this
        }

        override fun animSpinner(endSpinner: Int): ValueAnimator {

            return realKernelImpl.animSpinner(endSpinner)
        }

        override fun requestDrawBackgroundFor(
            internal: RefreshComponent,
            backgroundColor: Int
        ): RefreshKernel {
            realKernelImpl.requestDrawBackgroundFor(internal, backgroundColor)
            return this
        }

        override fun getRefreshContent(): RefreshContent {
            return refreshContent
        }

        override fun startTwoLevel(open: Boolean): RefreshKernel {
            realKernelImpl.startTwoLevel(open)
            return this
        }

        override fun finishTwoLevel(): RefreshKernel {
            realKernelImpl.finishTwoLevel()
            return this
        }

    }


}
