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

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.view.animation.LinearInterpolator
import androidx.core.animation.addListener
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.FragmentGuideItem3Binding
import com.cheng.blzb.event.GuideEvent
import com.cheng.blzb.manager.UserConfigManager
import com.cheng.blzb.ui.fragment.guide.GuideFragment
import com.cheng.blzb.ui.fragment.guide.GuideViewModel
import com.example.base.common.RxBus
import com.example.base.extensions.gone
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.extensions.visible
import com.example.base.ui.BaseFragment
import com.example.base.utils.DensityUtils
import com.example.base.utils.ScreenUtils

class GuideItem3Fragment : BaseFragment<FragmentGuideItem3Binding, GuideViewModel>() {
    private val ids = mutableListOf<Int>()

    private var canClick = false

    override fun initView() {
        super.initView()
        binding.tvName1.typeface = Constants.douyinsansB
        binding.tvName2.typeface = Constants.douyinsansB
        binding.tvName3.typeface = Constants.douyinsansB
        binding.tvName4.typeface = Constants.douyinsansB
        binding.tvName5.typeface = Constants.douyinsansB
        binding.tvName6.typeface = Constants.douyinsansB
        binding.tvName7.typeface = Constants.douyinsansB
        binding.tvName8.typeface = Constants.douyinsansB
    }

    override fun onLazyLoad() {
        super.onLazyLoad()
        if (UserConfigManager.getGuideKeywords().isNotEmpty()) {
            setData()
        } else {
            mViewModel.getHotWordList()
        }

    }

    override fun initListener() {
        super.initListener()
        binding.layoutOption1.onClick {
            val id = binding.layoutOption1.tag as Int
            if (ids.contains(id)) {
                ids.remove(id)
                binding.ivCheck1.gone()
            } else {
                ids.add(id)
                binding.ivCheck1.visible()
            }
        }

        binding.layoutOption2.onClick {
            val id = binding.layoutOption2.tag as Int
            if (ids.contains(id)) {
                ids.remove(id)
                binding.ivCheck2.gone()
            } else {
                ids.add(id)
                binding.ivCheck2.visible()
            }
        }

        binding.layoutOption3.onClick {
            val id = binding.layoutOption3.tag as Int
            if (ids.contains(id)) {
                ids.remove(id)
                binding.ivCheck3.gone()
            } else {
                ids.add(id)
                binding.ivCheck3.visible()
            }
        }

        binding.layoutOption4.onClick {
            val id = binding.layoutOption4.tag as Int
            if (ids.contains(id)) {
                ids.remove(id)
                binding.ivCheck4.gone()
            } else {
                ids.add(id)
                binding.ivCheck4.visible()
            }
        }

        binding.layoutOption5.onClick {
            val id = binding.layoutOption5.tag as Int
            if (ids.contains(id)) {
                ids.remove(id)
                binding.ivCheck5.gone()
            } else {
                ids.add(id)
                binding.ivCheck5.visible()
            }
        }

        binding.layoutOption6.onClick {
            val id = binding.layoutOption6.tag as Int
            if (ids.contains(id)) {
                ids.remove(id)
                binding.ivCheck6.gone()
            } else {
                ids.add(id)
                binding.ivCheck6.visible()
            }
        }

        binding.layoutOption7.onClick {
            val id = binding.layoutOption7.tag as Int
            if (ids.contains(id)) {
                ids.remove(id)
                binding.ivCheck7.gone()
            } else {
                ids.add(id)
                binding.ivCheck7.visible()
            }
        }

        binding.layoutOption8.onClick {
            val id = 0
            if (ids.contains(id)) {
                ids.remove(id)
                binding.ivCheck8.gone()
            } else {
                ids.add(id)
                binding.ivCheck8.visible()
            }
        }

        binding.ivBtn.onClick {
            if (canClick) {
                if (ids.isEmpty()) {
                    toast("请选择行业")
                    return@onClick
                }
                val hasOther = ids.contains(0)
                val selectedList = if (hasOther) {
                    UserConfigManager.getGuideKeywords()
                } else {
                    UserConfigManager.getGuideKeywords().filter { ids.contains(it.id) }
                }
                selectedList.forEach {
                    GuideFragment.hotWordList.addAll(it.children)
                }
                RxBus.defaultInstance.post(GuideEvent(3))
            }
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.hotWordLiveData.observe(this) {
            UserConfigManager.saveGuideKeywords(it)
            setData()
        }
    }

    private fun setData() {
        val hotWords = UserConfigManager.getGuideKeywords()
        hotWords.forEachIndexed { index, item ->
            when(index) {
                0 -> {
                    binding.tvName1.text = item.name
                    binding.ivIcon1.setImageResource(R.mipmap.ic_guide_item3_icon1)
                    binding.layoutOption1.tag = item.id
                }
                1 -> {
                    binding.tvName2.text = item.name
                    binding.ivIcon2.setImageResource(R.mipmap.ic_guide_item3_icon2)
                    binding.layoutOption2.tag = item.id
                }
                2 -> {
                    binding.tvName3.text = item.name
                    binding.ivIcon3.setImageResource(R.mipmap.ic_guide_item3_icon3)
                    binding.layoutOption3.tag = item.id
                }
                3 -> {
                    binding.tvName4.text = item.name
                    binding.ivIcon4.setImageResource(R.mipmap.ic_guide_item3_icon4)
                    binding.layoutOption4.tag = item.id
                }
                4 -> {
                    binding.tvName5.text = item.name
                    binding.ivIcon5.setImageResource(R.mipmap.ic_guide_item3_icon5)
                    binding.layoutOption5.tag = item.id
                }
                5 -> {
                    binding.tvName6.text = item.name
                    binding.ivIcon6.setImageResource(R.mipmap.ic_guide_item3_icon6)
                    binding.layoutOption6.tag = item.id
                }
                6 -> {
                    binding.tvName7.text = item.name
                    binding.ivIcon7.setImageResource(R.mipmap.ic_guide_item3_icon7)
                    binding.layoutOption7.tag = item.id
                }
            }
        }
        startAnim()
    }

    private fun startAnim() {
        val scaleXHolder = PropertyValuesHolder.ofFloat("scaleX", 0.85f, 1.15f, 0.9f, 1.1f, 0.95f, 1f)
        val scaleYHolder = PropertyValuesHolder.ofFloat("scaleY", 0.85f, 1.15f, 0.9f, 1.1f, 0.95f, 1f)

        val option1ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.layoutOption1, scaleXHolder, scaleYHolder)
        option1ScaleAnim.duration = 1000
        option1ScaleAnim.startDelay = 1000
        option1ScaleAnim.interpolator = LinearInterpolator()
        option1ScaleAnim.addListener(onStart = {
            binding.layoutOption1.visible()
        })

        val option2ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.layoutOption2, scaleXHolder, scaleYHolder)
        option2ScaleAnim.duration = 1000
        option2ScaleAnim.startDelay = 1400
        option2ScaleAnim.interpolator = LinearInterpolator()
        option2ScaleAnim.addListener(onStart = {
            binding.layoutOption2.visible()
        })

        val option3ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.layoutOption3, scaleXHolder, scaleYHolder)
        option3ScaleAnim.duration = 1000
        option3ScaleAnim.startDelay = 1800
        option3ScaleAnim.interpolator = LinearInterpolator()
        option3ScaleAnim.addListener(onStart = {
            binding.layoutOption3.visible()
        })

        val option4ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.layoutOption4, scaleXHolder, scaleYHolder)
        option4ScaleAnim.duration = 1000
        option4ScaleAnim.startDelay = 1400
        option4ScaleAnim.interpolator = LinearInterpolator()
        option4ScaleAnim.addListener(onStart = {
            binding.layoutOption4.visible()
        })

        val option5ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.layoutOption5, scaleXHolder, scaleYHolder)
        option5ScaleAnim.duration = 1000
        option5ScaleAnim.startDelay = 1800
        option5ScaleAnim.interpolator = LinearInterpolator()
        option5ScaleAnim.addListener(onStart = {
            binding.layoutOption5.visible()
        })

        val option6ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.layoutOption6, scaleXHolder, scaleYHolder)
        option6ScaleAnim.duration = 1000
        option6ScaleAnim.startDelay = 1200
        option6ScaleAnim.interpolator = LinearInterpolator()
        option6ScaleAnim.addListener(onStart = {
            binding.layoutOption6.visible()
        })

        val option7ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.layoutOption7, scaleXHolder, scaleYHolder)
        option7ScaleAnim.duration = 1000
        option7ScaleAnim.startDelay = 1600
        option7ScaleAnim.interpolator = LinearInterpolator()
        option7ScaleAnim.addListener(onStart = {
            binding.layoutOption7.visible()
        })

        val option8ScaleAnim = ObjectAnimator.ofPropertyValuesHolder(binding.layoutOption8, scaleXHolder, scaleYHolder)
        option8ScaleAnim.duration = 1000
        option8ScaleAnim.startDelay = 1800
        option8ScaleAnim.interpolator = LinearInterpolator()
        option8ScaleAnim.addListener(onStart = {
            binding.layoutOption8.visible()
        })

        val bgTransAnim = ObjectAnimator.ofFloat(binding.ivBg, "translationY", ScreenUtils.getScreenHeight().toFloat(), 0f)
        bgTransAnim.duration = 1000
        bgTransAnim.addListener(onStart = {
            binding.ivBg.visible()
        })

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

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

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

        val animSet = AnimatorSet()
        animSet.playTogether(
            option1ScaleAnim,
            option2ScaleAnim,
            option3ScaleAnim,
            option4ScaleAnim,
            option5ScaleAnim,
            option6ScaleAnim,
            option7ScaleAnim,
            option8ScaleAnim,
            bgTransAnim,
            titleTransAnim,
            descTransAnim,
            btnScaleAnim
        )
        animSet.start()
    }
}