package com.base.pdfviewerscannerwhite.ui.appprocess

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.core.animation.doOnEnd
import androidx.lifecycle.lifecycleScope
import com.base.pdfviewerscannerwhite.ads.admob.AdmobInterstitialUtils
import com.base.pdfviewerscannerwhite.databinding.ActivityAppProcessLoadingBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.utils.BarUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

class AppProcessLoadingActivity : BaseActivity<ActivityAppProcessLoadingBinding>() {

    override val binding: ActivityAppProcessLoadingBinding by lazy {
        ActivityAppProcessLoadingBinding.inflate(layoutInflater)
    }

    override fun initView() {

        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
//        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        overridePendingTransition(0, 0)

        val colors = intArrayOf(
            Color.parseColor("#EE5C42"),
            Color.parseColor("#F07D34"),
            Color.parseColor("#9171E2"),
            Color.parseColor("#5C66FB"),
            Color.parseColor("#4EA183")
        )

        startColorAnimation(binding.view, colors, 0, 800)

        lifecycleScope.launch(Dispatchers.IO) {
            AppProcessActivity.initData(this@AppProcessLoadingActivity)
        }
        lifecycleScope.launch(Dispatchers.Main) {
            var i = 1
            while (isActive) {
                i++
                binding.tvPercent.text = i.toString()
                delay(50)
            }
        }
        jumpAction = {
            AdmobInterstitialUtils.showInterstitialAd(this) {
                startActivity(Intent(this, AppProcessActivity::class.java))
                finish()
            }
        }
    }

    override fun initListener() {
        super.initListener()
        binding.flFanhui.setOnClickListener {
            jumpAction = null
            AdmobInterstitialUtils.showInterstitialAd(this) {
                finishToMain()
            }
        }
    }

    var jumpAction: (() -> Unit)? = null
    private fun startColorAnimation(view: View, colors: IntArray, index: Int, duration: Long) {
        if (index == colors.size - 1) {
            jumpAction?.invoke()
            jumpAction = null
            return // 如果索引超出颜色数组范围，则停止动画
        }

        val nextIndex = (index + 1)
        val animator = ValueAnimator.ofArgb(colors[index], colors[nextIndex])
        animator.interpolator = LinearInterpolator()
        animator.duration = duration
        animator.addUpdateListener { animation ->
            view.setBackgroundColor(animation.animatedValue as Int)
        }
        animator.doOnEnd {
            // 当前颜色渐变动画结束后，开始下一个颜色渐变动画
            startColorAnimation(view, colors, nextIndex, duration)
        }
        animator.start()
    }

}