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

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.global.GlobalDataCollection
import cn.muchinfo.rma.global.database.AppDatabase
import cn.muchinfo.rma.global.room.Builder
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.base.BaseFragment
import cn.muchinfo.rma.view.base.future.BaseFutureModel
import com.qmuiteam.qmui.widget.tab.QMUITabSegment

/**
 * 期货的行情报价牌页面
 */
class FutureMarket : BaseFragment<FutureMarketModel>() {

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

//region views
    private var _view: View? = null
    private var idMarketPager: ViewPager? = null
    private var idFutureTab: QMUITabSegment? = null
//endregion

    //region 数据

    private val tabsData by lazy {
        GlobalDataCollection.instance?.loginQueryData?.externalExchanges
    }
    // 页面，和对应的tab对应
    private val tabFragments : ArrayList<Fragment> = arrayListOf()
//endregion

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

    private fun initData(){

    }

    private fun initViews() {
        idFutureTab = _view?.findViewById(R.id.id_future_tab)
        idMarketPager = _view?.findViewById(R.id.id_market_pager)
        // 设置
        val builder = BaseFutureModel().qmuiTabSegmentConfig(idFutureTab, 14, context!!)

        // 加载数据
//        for (cat in tabs) {
//            val item = builder?.setText(cat)?.build(context)
//            idFutureTab?.addTab(item)
//        }
        tabsData?.forEach {data ->
            val goodsgroupid = GlobalDataCollection.instance?.loginQueryData?.goodsgroups?.find {
                it.goodsgroupid == data.autoid
            }?.goodsgroupid
            if (Builder.getDatabase<AppDatabase>().goodsInfoDao.getGoodsGroups(
                    goodsgroupid ?: 0
                ).isNotEmpty()){
                val item = builder?.setText(data.exexchangename)?.build(context)
                idFutureTab?.addTab(item)
                tabFragments.add(MarketFragment.getInstance(data))
            }
        }


        if (tabsData?.size ?: 0 > 5){
            idFutureTab?.mode = QMUITabSegment.MODE_SCROLLABLE
        }else{
            idFutureTab?.mode = QMUITabSegment.MODE_FIXED
        }

        idMarketPager?.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 tabsData?.get(position)?.exexchangename
            }
        }
        idFutureTab?.setupWithViewPager(idMarketPager, false)
        // 关联fragment
        idFutureTab?.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) {}
        })
    }
}