package com.cheng.blzb.ui.fragment.guide.item

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.view.ViewTreeObserver
import android.view.animation.LinearInterpolator
import androidx.core.animation.addListener
import com.cheng.blzb.databinding.FragmentGuideItem1Binding
import com.cheng.blzb.event.GuideEvent
import com.cheng.blzb.ui.fragment.guide.GuideViewModel
import com.example.base.common.RxBus
import com.example.base.extensions.onClick
import com.example.base.extensions.visible
import com.example.base.ui.BaseFragment
import com.example.base.utils.DensityUtils

class GuideItem1Fragment : BaseFragment<FragmentGuideItem1Binding, GuideViewModel>() {
    private var canClick = false

    override fun onLazyLoad() {
        super.onLazyLoad()
        binding.root.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                startAnim()
                binding.root.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })
    }

    override fun initListener() {
        super.initListener()
        binding.ivBtn.onClick {
            if (canClick) {
                RxBus.defaultInstance.post(GuideEvent(1))
            }
        }
    }

    private fun startAnim() {
        val scaleXHolder1 = PropertyValuesHolder.ofFloat("scaleX", 0f, 1.1f)
        val scaleYHolder1 = PropertyValuesHolder.ofFloat("scaleY", 0f, 1.1f)

        val scaleXHolder2 = PropertyValuesHolder.ofFloat("scaleX", 0.85f, 1.15f, 0.9f, 1.1f, 0.95f, 1f)
        val scaleYHolder2 = PropertyValuesHolder.ofFloat("scaleY", 0.85f, 1.15f, 0.9f, 1.1f, 0.95f, 1f)

        val layer1AlphaAnim = ObjectAnimator.ofFloat(binding.ivLayer1, "alpha", 0f, 1f)
        layer1AlphaAnim.duration = 1600
        layer1AlphaAnim.startDelay = 1600
        layer1AlphaAnim.addListener(onStart = {
            binding.ivLayer1.visible()
        })

        val layer2TransAnim = ObjectAnimator.ofFloat(binding.ivLayer2, "translationX", -binding.ivLayer2.width.toFloat(), 0f)
        layer2TransAnim.duration = 1000
        layer2TransAnim.addListener(onStart = {
            binding.ivLayer2.visible()
        })

        val layer3ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.ivLayer3, scaleXHolder1, scaleYHolder1)
        layer3ScaleAnim.duration = 1000
        layer3ScaleAnim.startDelay = 1000
        layer3ScaleAnim.addListener(onStart = {
            binding.ivLayer3.visible()
        })

        val titleTransAnim = ObjectAnimator.ofFloat(binding.ivTitle, "translationY", -DensityUtils.dp2px(200f).toFloat(), 0f)
        titleTransAnim.duration = 1000
        titleTransAnim.startDelay = 1000
        titleTransAnim.addListener(onStart = {
            binding.ivTitle.visible()
        })

        val descTransAnim = ObjectAnimator.ofFloat(binding.ivDesc, "translationY", -DensityUtils.dp2px(200f).toFloat(), 0f)
        descTransAnim.duration = 1000
        descTransAnim.startDelay = 1000
        descTransAnim.addListener(onStart = {
            binding.ivDesc.visible()
        })

        val icon1ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.ivIcon1, scaleXHolder2, scaleYHolder2)
        icon1ScaleAnim.duration = 1000
        icon1ScaleAnim.startDelay = 1400
        icon1ScaleAnim.interpolator = LinearInterpolator()
        icon1ScaleAnim.addListener(onStart = {
            binding.ivIcon1.visible()
        })

        val icon2ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.ivIcon2, scaleXHolder2, scaleYHolder2)
        icon2ScaleAnim.duration = 1000
        icon2ScaleAnim.startDelay = 1600
        icon2ScaleAnim.interpolator = LinearInterpolator()
        icon2ScaleAnim.addListener(onStart = {
            binding.ivIcon2.visible()
        })

        val icon3TransAnim = ObjectAnimator.ofFloat(binding.ivIcon3, "translationX", -DensityUtils.dp2px(200f).toFloat(), DensityUtils.dp2px(30f).toFloat(), -DensityUtils.dp2px(30f).toFloat(), 0f)
        icon3TransAnim.duration = 1000
        icon3TransAnim.startDelay = 1200
        icon3TransAnim.addListener(onStart = {
            binding.ivIcon3.visible()
        })

        val icon4TransAnim = ObjectAnimator.ofFloat(binding.ivIcon4, "translationX", DensityUtils.dp2px(200f).toFloat(), -DensityUtils.dp2px(30f).toFloat(), DensityUtils.dp2px(30f).toFloat(), 0f)
        icon4TransAnim.duration = 1000
        icon4TransAnim.startDelay = 1200
        icon4TransAnim.addListener(onStart = {
            binding.ivIcon4.visible()
        })

        val btnScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.ivBtn, scaleXHolder2, scaleYHolder2)
        btnScaleAnim.duration = 1000
        btnScaleAnim.startDelay = 1800
        btnScaleAnim.interpolator = LinearInterpolator()
        btnScaleAnim.addListener(onStart = {
            binding.ivBtn.visible()
        }, onEnd = {
            canClick = true
        })

        val animSet = AnimatorSet()
        animSet.playTogether(
            layer1AlphaAnim,
            layer2TransAnim,
            layer3ScaleAnim,
            titleTransAnim,
            descTransAnim,
            icon1ScaleAnim,
            icon2ScaleAnim,
            icon3TransAnim,
            icon4TransAnim,
            btnScaleAnim
        )
        animSet.start()
    }
}