package com.gh.common.util

import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.TextView
import com.gh.common.constant.Config

/**
 * 光环游戏搜索栏默认文案轮换
 */
object DefaultSearchHintHelper {
    private val hintList = ArrayList<String>()

    init {
        loadData()
    }

    fun loadData() {
        hintList.clear()
        Config.getSettings()?.search?.defaultData?.also {
            if (it.isNotEmpty()) {
                for (h in it) {
                    hintList.add(h.removeSuffix("."))
                }
            }
        }
    }

    fun setSearchHint(searchTv: TextView) {
        var hintIndex = 0
        val scaleAnimation = ScaleAnimation(
            0.4F, 1.0F, 0.4F, 1.0F,
            Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F
        )
        scaleAnimation.duration = 500
        val alphaAnimation = AlphaAnimation(1F, 0.2F)
        alphaAnimation.duration = 300
        alphaAnimation.startOffset = 5000
        scaleAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationEnd(animation: Animation) {
                searchTv.animation = alphaAnimation
                alphaAnimation.start()
            }

            override fun onAnimationRepeat(animation: Animation) {}
        })
        alphaAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationEnd(animation: Animation) {
                // 切换数据
                if (hintList.isNotEmpty()) {
                    if (hintIndex > hintList.size - 1) {
                        hintIndex = 0
                    }
                    searchTv.hint = hintList[hintIndex]
                    hintIndex++
                    searchTv.animation = scaleAnimation
                    scaleAnimation.start()
                }
            }

            override fun onAnimationRepeat(animation: Animation) {}
        })

        if (hintList.isEmpty()) {
            loadData()
        }

        if (hintList.isNotEmpty()) {
            if (hintList.size > 1) {
                searchTv.animation = alphaAnimation
            } else {
                val hint = hintList[0]
                searchTv.hint = hint
            }
        }
    }
}