package com.cheng.bole.ui.fragment.mine.order

import android.graphics.Color
import android.graphics.Typeface
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.angcyo.tablayout.delegate.ViewPager1Delegate
import com.cheng.bole.R
import com.cheng.bole.common.Constants
import com.cheng.bole.databinding.FragmentOrderBinding
import com.cheng.bole.ui.base.BasePageAdapter
import com.cheng.bole.ui.fragment.mine.order.list.OrderListFragment
import com.example.base.ui.BaseFragment

class OrderFragment: BaseFragment<FragmentOrderBinding, OrderViewModel>() {
    private val tabText = listOf("全部", "待支付", "已支付")
    private val fragmentList by lazy { mutableListOf<Fragment>() }
    private val pageAdapter by lazy {
        BasePageAdapter(
            childFragmentManager,
            tabText,
            fragmentList,
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        )
    }

    override fun initView() {
        super.initView()
        mTitleBar?.background = null

        initFragment()
        binding.viewPager.adapter = pageAdapter
        binding.viewPager.offscreenPageLimit = fragmentList.size
        binding.tabLayout.setupViewPager(ViewPager1Delegate.install(binding.viewPager, binding.tabLayout))
    }

    private fun initFragment() {
        fragmentList.add(OrderListFragment.newInstance(""))
        fragmentList.add(OrderListFragment.newInstance("1"))
        fragmentList.add(OrderListFragment.newInstance("2"))
    }

    override fun initListener() {
        super.initListener()
        binding.tabLayout.observeIndexChange { fromIndex, toIndex, _, _ ->
            binding.viewPager.currentItem = toIndex
            if (fromIndex != -1) {
                (binding.tabLayout.getChildAt(fromIndex) as TextView).typeface = Typeface.DEFAULT
                (binding.tabLayout.getChildAt(fromIndex) as TextView).setBackgroundResource(R.drawable.shape_push_bid_type_default)
            }
            (binding.tabLayout.getChildAt(toIndex) as TextView).typeface = Constants.douyinsansB
            (binding.tabLayout.getChildAt(toIndex) as TextView).setBackgroundResource(R.drawable.shape_push_bid_type_checked)
        }
    }
}