package cn.muchinfo.rma.view.base.future.entrusts

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.NonNull
import androidx.annotation.Nullable
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import cn.muchinfo.rma.R
import cn.muchinfo.rma.view.base.BaseFragment
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.base.future.BaseFutureModel
import com.google.android.material.tabs.TabLayout
import com.qmuiteam.qmui.widget.tab.QMUITabSegment

/**
 * 委托单列表
 */
class EntrustFragment: BaseFragment<EntrustViewModel>() {

    companion object {
        fun getInstance() = EntrustFragment()
    }

//region 数据
    private val tabs: Array<String?> = arrayOf(
        MyApplication.getInstance()?.resources?.getString(R.string.str_can_cancel_today), // 今日可撤
        MyApplication.getInstance()?.resources?.getString(R.string.str_entrust_today) // 今日委托
    )
    private val tabFragments: Array<Fragment> = arrayOf(
        EntrustChildFragment.getInstance(MyApplication.getInstance()?.resources?.getString(R.string.str_can_cancel_today)!!),
        EntrustChildFragment.getInstance(MyApplication.getInstance()?.resources?.getString(R.string.str_entrust_today)!!)
    )
//endregion

//region views
    private var _view: View? = null
    private var idEntrustTab: QMUITabSegment? = null // 切换
    private var idPager: ViewPager? = null
//endregion

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _view = inflater.inflate(R.layout.entrust_fragment, container, false)
        initViews()
        return _view
    }

//region 函数

    private fun initViews() {
        idPager = _view?.findViewById(R.id.id_pager)
        idEntrustTab = _view?.findViewById(R.id.id_entrust_tab)

        // 设置
        val builder = BaseFutureModel().qmuiTabSegmentConfig(idEntrustTab, 15, context)
        idEntrustTab?.mode = QMUITabSegment.MODE_FIXED
        // 加载数据
        for (cat in tabs) {
            val item = builder?.setText(cat)?.build(context)
            idEntrustTab?.addTab(item)
        }

        idPager?.adapter = object : FragmentPagerAdapter(
            childFragmentManager,
            BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            @NonNull
            override fun getItem(position: Int): Fragment {
                return tabFragments[position]
            }

            override fun getCount(): Int {
                return tabFragments.size
            }

            @Nullable
            override fun getPageTitle(position: Int): CharSequence? {
                return tabs[position]
            }
        }
        idEntrustTab?.setupWithViewPager(idPager, false)
        idEntrustTab?.addOnTabSelectedListener(object : QMUITabSegment.OnTabSelectedListener {
            /**
             * 当某个 Tab 被选中时会触发
             * @param index 被选中的 Tab 下标
             */
            override fun onTabSelected(index: Int) {
            }

            /**
             * 当某个 Tab 被取消选中时会触发
             * @param index 被取消选中的 Tab 下标
             */
            override fun onTabUnselected(index: Int) {}

            /**
             * 当某个 Tab 处于被选中状态下再次被点击时会触发
             * @param index 被再次点击的 Tab 下标
             */
            override fun onTabReselected(index: Int) {
            }
            /**
             * 当某个 Tab 被双击时会触发
             * @param index 被双击的 Tab 下标
             */
            override fun onDoubleTap(index: Int) {}
        })
    }

//endregion
}