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

import android.annotation.SuppressLint
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.cheng.blzb.bean.AreaEntity
import com.cheng.blzb.bean.GuideTotalBidEntity
import com.cheng.blzb.bean.HotWordEntity
import com.cheng.blzb.common.EventConstants
import com.cheng.blzb.databinding.FragmentGuideContentBinding
import com.cheng.blzb.event.GuideEvent
import com.cheng.blzb.manager.EventReportManager
import com.cheng.blzb.manager.UserConfigManager
import com.cheng.blzb.ui.base.BasePageAdapter
import com.cheng.blzb.ui.fragment.guide.item.GuideItem1Fragment
import com.cheng.blzb.ui.fragment.guide.item.GuideItem2Fragment
import com.cheng.blzb.ui.fragment.guide.item.GuideItem3Fragment
import com.cheng.blzb.ui.fragment.guide.item.GuideItem4Fragment
import com.cheng.blzb.ui.fragment.guide.item.GuideItem5Fragment
import com.example.base.common.RxBus
import com.example.base.ui.BaseFragment

class GuideFragment : BaseFragment<FragmentGuideContentBinding, GuideViewModel>() {

    private var titles = listOf("guide1", "guide2", "guide3", "guide4", "guide5")
    private val fragmentList by lazy { mutableListOf<Fragment>() }
    private val pageAdapter by lazy {
        BasePageAdapter(
            childFragmentManager,
            titles,
            fragmentList,
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        )
    }
    private var currentPosition = 0

    companion object {
        val cityList = mutableListOf<AreaEntity>()
        val hotWordList = mutableListOf<HotWordEntity.Child>()
        var totalInfo: GuideTotalBidEntity? = null
    }

    override fun initView() {
        super.initView()

        initFragment()
        binding.viewPager.adapter = pageAdapter
    }

    private fun initFragment() {
        fragmentList.add(GuideItem1Fragment())
        fragmentList.add(GuideItem2Fragment())
        fragmentList.add(GuideItem3Fragment())
        fragmentList.add(GuideItem4Fragment())
        fragmentList.add(GuideItem5Fragment())
    }

    override fun initData() {
        super.initData()
        mViewModel.getHotWordList()
        mViewModel.getCityList()
        mViewModel.getBidTypeList()
        EventReportManager.eventReport(EventConstants.GUIDE_LAUNCH, "", "")
    }

    override fun initListener() {
        super.initListener()

        binding.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            @SuppressLint("NotifyDataSetChanged")
            override fun onPageSelected(position: Int) {
                currentPosition = position
                EventReportManager.eventReport(EventConstants.GUIDE_OPPORTUNITY_SCROLL, "${currentPosition + 1}", "")
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
    }

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

        mViewModel.cityLiveData.observe(this) {
            UserConfigManager.saveAreaList(it)
        }

        mViewModel.bidTypeLiveData.observe(this) {
            UserConfigManager.saveBidTypes(it)
        }

        val guideEvent = RxBus.defaultInstance.toObservable(GuideEvent::class.java).subscribe {
            if (it.toIndex < fragmentList.size) {
                binding.viewPager.currentItem = it.toIndex
            }
        }
        addDisposable(guideEvent)
    }
}