package com.led.scroller.barrage.banner.ui

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.activity.ComponentActivity
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.led.scroller.barrage.banner.databinding.ActivityShowBarrageBinding
import com.led.scroller.barrage.banner.sp.AppSP
import androidx.core.view.isVisible
import com.ad.tradpluslibrary.TPAdManager

class ShowBarrageActivity : ComponentActivity() {
    companion object {
        const val CONTENT_KEY = "content_key"
    }

    private var animSet: AnimatorSet? = null
    private var animOne: ObjectAnimator? = null
    private lateinit var binding: ActivityShowBarrageBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityShowBarrageBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val controller = WindowCompat.getInsetsController(window, window.decorView)
        hideSystemBars(controller)
        initView()
        val content = intent.getStringExtra(CONTENT_KEY)
        animatorRightToLeft(content.toString())
    }

    private fun hideSystemBars(controller: WindowInsetsControllerCompat) {
        // 让导航栏和状态栏完全隐藏
        controller.systemBarsBehavior =
            WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        controller.hide(WindowInsetsCompat.Type.systemBars())
    }

    private fun initView() {
        binding.root.setOnClickListener {
            if (binding.backImg.isVisible) {
                binding.backImg.visibility = View.GONE
            } else {
                binding.backImg.visibility = View.VISIBLE
            }
        }
        binding.backImg.setOnClickListener {
            Log.d("ShowBarrageActivity", "返回按钮点击，调用finish()")
            TPAdManager.showTPAD(this@ShowBarrageActivity){
                finish()
            }

        }
    }

    private fun animatorRightToLeft(content: String) {
        val sp = AppSP(this)
        binding.previewLayout.setBackgroundColor(getColor(sp.bgColorResId))
        binding.previewText.text = content
        binding.previewText.setTextColor(getColor(sp.fontColorResId))
        binding.previewText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, sp.fontSize.toFloat())
        // 加载字体文件
        val typeface = ResourcesCompat.getFont(this, sp.fontResId)
        binding.previewText.typeface = typeface

        val textPaint = binding.previewText.paint
        val textPaintWidth = textPaint.measureText(content) //获取文字的宽度。
        val screenWidth = resources.displayMetrics.widthPixels //获取屏幕的宽度。
        animSet = AnimatorSet()
        animSet?.interpolator = LinearInterpolator() //线性
        animOne = ObjectAnimator.ofFloat(
            binding.previewText,
            "translationX",
            screenWidth.toString().toFloat(),
            -textPaintWidth
        ) //X轴移动，屏幕宽度为起始值，也就是会从最右边开始到看不到文字。
        animOne?.duration = 5000
        animOne?.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                animatorRightToLeft(content)
            }
        })
        animSet?.play(animOne)
        animSet?.start()
    }
}