package com.adealink.weparty.commonui

import android.content.Context
import android.util.AttributeSet
import com.adealink.weparty.R
import com.opensource.svgaplayer.WenextSvgaView
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.simple.SimpleComponent

/**
 */
class SVGARefreshHeader @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : SimpleComponent(context, attrs, defStyleAttr), RefreshHeader {

    private var svgaLoadingView: WenextSvgaView

    init {
        inflate(context, R.layout.svga_refresh_header, this)
        svgaLoadingView = findViewById(R.id.svga_view)
    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
        super.onStartAnimator(refreshLayout, height, maxDragHeight)
        svgaLoadingView.startAnimation()
    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        return 833
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        super.onStateChanged(refreshLayout, oldState, newState)
        if (newState == RefreshState.None) {
            svgaLoadingView.stopAnimation()
            svgaLoadingView.stepToFrame(1, false)
        }
    }

}