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.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_DRAGGING
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.account.loginQeruy.ExternalExchanges
import cn.muchinfo.rma.global.data.account.loginQeruy.GoodsGroups
import cn.muchinfo.rma.global.data.account.loginQeruy.GoodsInfo
import cn.muchinfo.rma.global.database.AppDatabase
import cn.muchinfo.rma.global.room.Builder
import cn.muchinfo.rma.view.base.BaseFragment

/**
 * 报价牌的通用fragment
 */
class MarketFragment(data: ExternalExchanges) : BaseFragment<MarketBaseModel>() {

    var data: ExternalExchanges? = data

    companion object {
        fun getInstance(data: ExternalExchanges) = MarketFragment(data)
    }

    //region views
    private var _view: View? = null
    private var idMarketList: RecyclerView? = null // 列表

    var fistShow: Int = 0
    var lastShow: Int = 0
//endregion

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

    private fun initViews() {
        idMarketList = _view?.findViewById(R.id.id_market_list)

        val goodsGroupData = GlobalDataCollection.instance?.loginQueryData?.goodsgroups?.find {
            it.goodsgroupid == data?.autoid
        }
        val list = Builder.getDatabase<AppDatabase>().goodsInfoDao.getGoodsGroups(
            goodsGroupData?.goodsgroupid ?: 0
        )
        if (list.size > 10){
            lastShow = 10
        }else{
            if (list.size > 0){
                lastShow = list.size
            }
        }

        val layoutManager = LinearLayoutManager(context)
        idMarketList?.layoutManager = layoutManager
        viewModel.list = list
        val adapter = viewModel.getAdapter()
        idMarketList?.adapter = adapter
        if (fistShow != -1 && lastShow != -1){
            viewModel.queryQuoteDay(first = fistShow,last = lastShow){isCompleted, err ->
                if (isCompleted){
                    adapter.notifyItemRangeChanged(fistShow,lastShow)
                }
            }
        }


        idMarketList?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                // DES: 找出当前可视Item位置
                val linearLayoutManager = idMarketList!!.layoutManager as LinearLayoutManager
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {// 这个判断是当recyclerview不滚动时
                    fistShow = linearLayoutManager.findFirstVisibleItemPosition()//获取第一个显示条目
                    lastShow = linearLayoutManager.findLastVisibleItemPosition()//获取最后一个显示条目
                    if (fistShow != -1 && lastShow != -1){
                        viewModel.queryQuoteDay(first = fistShow,last = lastShow){isCompleted, err ->
                            if (isCompleted){
                                adapter.notifyItemRangeChanged(fistShow,lastShow)
                            }
                        }
                    }

                }
            }
        })
    }

//region 函数

//endregion
}