package com.cheng.bole.ui.fragment.guide

import android.annotation.SuppressLint
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.example.base.extensions.gone
import com.example.base.extensions.onClick
import com.example.base.ui.BaseFragment
import com.cheng.bole.ui.activity.MainActivity
import com.cheng.bole.ui.activity.PublicActivity
import com.cheng.bole.common.Constants
import com.cheng.bole.common.EventConstants
import com.cheng.bole.databinding.FragmentGuideContentBinding
import com.cheng.bole.manager.EventReportManager
import com.cheng.bole.manager.UserConfigManager
import com.cheng.bole.ui.base.BasePageAdapter
import com.cheng.bole.ui.fragment.mine.vip.VipFragment
import org.jetbrains.anko.startActivity

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

    private var titles = listOf("guide1", "guide2", "guide3","guide4")

    private val list by lazy {
        ArrayList<Fragment>()
    }

    private val indicatorAdapter by lazy { IndicatorAdapter() }

    private var mIsScrolled = false
    private var currentPosition = 0

    override fun initView() {
        super.initView()
        titles.forEachIndexed { index, s ->
            list.add(GuideItemFragment.newInstance(index))
        }
        val pageAdapter = BasePageAdapter(childFragmentManager, titles, list)
        binding.viewPager.adapter = pageAdapter
        binding.viewPager.offscreenPageLimit = list.size

        indicatorAdapter.setList(MutableList(titles.size) { i -> i })
        binding.rvIndicator.adapter = indicatorAdapter

        binding.tvTitle.typeface = Constants.almmsht
    }

    override fun initData() {
        super.initData()
        binding.tvDesc.text = UserConfigManager.getGuideHint()
        EventReportManager.eventReport(EventConstants.GUIDE_LAUNCH, "", "")
    }

    override fun initListener() {
        super.initListener()
        binding.btnStart.onClick {
            binding.btnStart.postDelayed({
                binding.layoutGuide.gone()
            }, 200)
        }

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

            }

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

            override fun onPageScrollStateChanged(state: Int) {
                when(state){
                    ViewPager.SCROLL_STATE_DRAGGING->{
                        mIsScrolled = false
                        return
                    }
                    ViewPager.SCROLL_STATE_SETTLING->{
                        mIsScrolled = true
                        return
                    }
                    ViewPager.SCROLL_STATE_IDLE->{
                        if (!mIsScrolled && currentPosition == indicatorAdapter.itemCount - 1){
                            if (UserConfigManager.getGuidePayEnable()){
                                PublicActivity.start(requireActivity(), VipFragment::class.java, Pair("origin", "guide"))
                            }else{
                                requireActivity().startActivity<MainActivity>()
                            }
                        }
                        mIsScrolled = true
                        return
                    }
                }
            }

        })

    }
}