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

import android.annotation.SuppressLint
import android.graphics.Typeface
import android.widget.TextView
import android.window.OnBackInvokedDispatcher
import androidx.activity.addCallback
import androidx.core.os.BuildCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.angcyo.tablayout.delegate.ViewPager1Delegate
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.FragmentOrderBinding
import com.cheng.blzb.ui.activity.MainActivity
import com.cheng.blzb.ui.base.BasePageAdapter
import com.cheng.blzb.ui.fragment.mine.order.list.OrderListFragment
import com.example.base.ui.BaseFragment
import org.jetbrains.anko.startActivity

class OrderFragment: BaseFragment<FragmentOrderBinding, OrderViewModel>() {
    private val from by lazy { arguments?.getInt("from") ?: 0 }
    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()
        if (from == 1) {
            setBackPressed()
            mTitleBar?.setNavigationOnClickListener {
                requireActivity().startActivity<MainActivity>()
            }
        }

        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)
        }
    }

    @SuppressLint("UnsafeOptInUsageError")
    private fun setBackPressed() {
        if (BuildCompat.isAtLeastT()) {
            requireActivity().onBackInvokedDispatcher.registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT) {
                requireActivity().startActivity<MainActivity>()
            }
        } else {
            requireActivity().onBackPressedDispatcher.addCallback(this) {
                requireActivity().startActivity<MainActivity>()
            }
        }
    }
}