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

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import androidx.core.animation.addListener
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cheng.blzb.bean.AreaEntity
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.FragmentGuideItem2Binding
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.cheng.blzb.ui.fragment.guide.adapter.GuideItem2Area1Adapter
import com.cheng.blzb.ui.fragment.guide.adapter.GuideItem2Area2Adapter
import com.cheng.blzb.widget.LinearTopSmoothScroller
import com.example.base.common.RxBus
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 GuideItem2Fragment : BaseFragment<FragmentGuideItem2Binding, GuideViewModel>() {
    private val area1Adapter by lazy { GuideItem2Area1Adapter() }

    private val area2Adapter by lazy { GuideItem2Area2Adapter() }

    private var canClick = false

    override fun initView() {
        super.initView()
        binding.btnNext.typeface = Constants.almmsht

        binding.rvArea1.adapter = area1Adapter
        binding.rvArea2.adapter = area2Adapter
    }

    override fun onLazyLoad() {
        super.onLazyLoad()
        if (UserConfigManager.getAreaList().isNotEmpty()) {
            setData()
        } else {
            mViewModel.getCityList()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initListener() {
        super.initListener()
        area1Adapter.setOnItemClickListener { _, _, position ->
            val item = area1Adapter.getItem(position)
            area1Adapter.data.find { it.isChecked }?.isChecked = false
            item.isChecked = true

            scrollToPosition(position)
        }

        area2Adapter.setOnCityClickListener {
            val selectList = mutableListOf<AreaEntity>()
            area2Adapter.data.forEach { area ->
                selectList.addAll(area.children.filter { it.isChecked })
            }
            binding.tvCityCount.text = "${selectList.size}"
        }

        binding.rvArea2.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val layoutManager = binding.rvArea2.layoutManager as LinearLayoutManager
                val firstPosition = layoutManager.findFirstCompletelyVisibleItemPosition()

                if (firstPosition >= 0) {
                    val firstItem = area1Adapter.getItem(firstPosition)
                    area1Adapter.data.find { it.isChecked }?.isChecked = false
                    firstItem.isChecked = true
                    area1Adapter.notifyDataSetChanged()
                }
            }
        })

        binding.btnNext.onClick {
            if (canClick) {
                val selectList = mutableListOf<AreaEntity>()
                area1Adapter.data.forEach { area ->
                    selectList.addAll(area.children.filter { it.isChecked })
                }
                if (selectList.isEmpty()) {
                    toast("请选择地区")
                    return@onClick
                }
                GuideFragment.cityList.addAll(selectList)
                RxBus.defaultInstance.post(GuideEvent(2))
            }
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.cityLiveData.observe(this) {
            UserConfigManager.saveAreaList(it)
            setData()
        }
    }

    private fun setData() {
        val areaList = UserConfigManager.getAreaList()
        areaList.add(0, AreaEntity(0, name = "全国地区", isChecked = true))
        area1Adapter.setList(areaList)
        area2Adapter.setList(areaList)
        startAnim()
    }

    private fun scrollToPosition(position: Int) {
        binding.rvArea2.layoutManager?.startSmoothScroll(
            LinearTopSmoothScroller(requireContext(), true)
                .apply { targetPosition = position }
        )
    }

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

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

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

        val icon1TransAnim = ObjectAnimator.ofFloat(binding.ivIcon1, "translationX", DensityUtils.dp2px(200f).toFloat(), 0f)
        icon1TransAnim.duration = 1000
        icon1TransAnim.startDelay = 600
        icon1TransAnim.addListener(onStart = {
            binding.ivIcon1.visible()
        })

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

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

        val btnTransAnim = ObjectAnimator.ofFloat(binding.btnNext, "translationY", DensityUtils.dp2px(200f).toFloat(), 0f)
        btnTransAnim.duration = 1000
        btnTransAnim.startDelay = 400
        btnTransAnim.addListener(onStart = {
            binding.btnNext.visible()
        }, onEnd = {
            canClick = true
        })

        val animSet = AnimatorSet()
        animSet.playTogether(titleTransAnim, descTransAnim, contentTransAnim, icon1TransAnim, icon2TransAnim, countTransAnim, btnTransAnim)
        animSet.start()
    }
}