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

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.viewpager.widget.ViewPager
import cn.muchinfo.rma.R
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.base.BaseFragment
import cn.muchinfo.rma.view.base.future.costs.CostFragment
import cn.muchinfo.rma.view.base.future.deals.DealFragment
import cn.muchinfo.rma.view.base.future.entrusts.EntrustFragment
import cn.muchinfo.rma.view.base.future.holds.HoldFragment
import cn.muchinfo.rma.view.base.future.markets.FutureMarket
import com.qmuiteam.qmui.widget.tab.QMUITabSegment


/**
 * 期货列表
 */
class FutureFragment : BaseFragment<FutureViewModel>() {
    companion object {
        fun newInstance() = FutureFragment()
    }
    private var _view: View? = null
    private var idTab: QMUITabSegment? = null
    private var idPager: ViewPager? = null
    // tab 值
    private val tabs: Array<String?> = arrayOf(
        MyApplication.getInstance()?.resources?.getString(R.string.str_markets), // 行情
        MyApplication.getInstance()?.resources?.getString(R.string.str_holds), // 持仓
        MyApplication.getInstance()?.resources?.getString(R.string.str_entrusts), // 委托
        MyApplication.getInstance()?.resources?.getString(R.string.str_deals), // 成交
        MyApplication.getInstance()?.resources?.getString(R.string.str_funds) // 资金
    )

    // 页面，和对应的tab对应
    private val tabFragments: Array<Fragment> = arrayOf(
        FutureMarket.getInstance(), // 行情
        HoldFragment.getInstance(), // 持仓
        EntrustFragment.getInstance(), // 委托
        DealFragment.getInstance(), // 成交
        CostFragment.getInstance() // 资金
    )

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

    private fun initViews() {
        idTab = _view?.findViewById(R.id.id_tab)
        idPager = _view?.findViewById(R.id.id_pager)

        // 设置
        val builder = BaseFutureModel().qmuiTabSegmentConfig(idTab, 15, context)
        idTab?.mode = QMUITabSegment.MODE_FIXED
        // 加载数据
        for (cat in tabs) {
            val item = builder?.setText(cat)?.build(context)
            idTab?.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]
            }
        }
        idTab?.setupWithViewPager(idPager, false)
        idTab?.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) {}
        })
    }

}