package com.gh.gamecenter.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.header.ClassicsHeader

class HomeRefreshHeader @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ClassicsHeader(context, attrs) {

    override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {
        val arrowView: View = mArrowView
        val updateView: View = mLastUpdateText
        when (newState) {
            RefreshState.None -> {
                mTitleText.visibility = VISIBLE
                updateView.visibility = if (mEnableLastTime) VISIBLE else GONE
                mTitleText.text = mTextPulling
                arrowView.visibility = VISIBLE
                arrowView.animate().rotation(180F).duration = 100L
            }
            RefreshState.PullDownToRefresh -> {
                mTitleText.visibility = VISIBLE
                mTitleText.text = mTextPulling
                arrowView.visibility = VISIBLE
                arrowView.animate().rotation(180F).duration = 100L
            }
            RefreshState.Refreshing, RefreshState.RefreshReleased -> {
                mTitleText.visibility = VISIBLE
                mTitleText.text = mTextRefreshing
                arrowView.visibility = GONE
            }
            RefreshState.ReleaseToRefresh -> {
                arrowView.visibility = VISIBLE
                mTitleText.visibility = VISIBLE
                mTitleText.text = mTextRelease
                arrowView.animate().rotation(360F).duration = 100L
            }
            RefreshState.ReleaseToTwoLevel -> {
                arrowView.visibility = GONE
                mTitleText.visibility = GONE
            }
            RefreshState.Loading -> {
                mTitleText.visibility = VISIBLE
                arrowView.visibility = GONE
                updateView.visibility = if (mEnableLastTime) INVISIBLE else GONE
                mTitleText.text = mTextLoading
            }
            RefreshState.TwoLevel -> {
                arrowView.visibility = GONE
                mTitleText.visibility = GONE
            }
            else -> {}
        }
    }

    fun setArrowColorFilter(@ColorInt color: Int) {
        mArrowView.setColorFilter(color)
    }
}