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

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.toArrayList
import cn.muchinfo.rma.view.base.BaseFragment
import cn.muchinfo.rma.view.eventbus.MessageEvent
import cn.muchinfo.rma.view.eventbus.QuoteMessageEvent
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.TaskUiModel
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 持仓列表界面
 */
class HoldFragment : BaseFragment<HoldViewModel>() {

    companion object {
        fun getInstance() = HoldFragment()
    }
//region views
    private var _view: View? = null
    private var idHoldList: RecyclerView? = null // 持仓列表
    private var loadingDialog: Dialog? = null
    var isBackhandByLose = 0 //是否是快捷反手操作，1是反手操作，0不是反手操作

    //此页面是否在显示，true行情下发可刷新列表 false则不刷新
    var canrefrashQuote: Boolean = false

    /**
     * 数据初始化状态控制
     */
    val loadingDialogStatus: MutableLiveData<TaskUiModel> = MutableLiveData()
//endregion
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _view = inflater.inflate(R.layout.hold_fragment, container, false)
        initViews()
        loadingDialog = createLoadingDialog(hintStr = "请求中")
        loadingDialog?.bindTaskStatus(context!!, loadingDialogStatus)
        viewModel.holdLiveData.bindOptional(context!!) { data ->
            (idHoldList?.adapter as HoldViewModel.HoldAdapter).update(data?.toArrayList())
        }

        return _view
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onQuoteMessageEvent(quoteMessageEvent: QuoteMessageEvent) {
        /** 是否可刷新ui **/
        if (canrefrashQuote) {
            /** 行情推送过来的goodsid **/
            quoteMessageEvent.goodsid.forEach { goodid ->
                //找到相应商品信息
                val goodsInfo = viewModel.list?.find { it.goodsid == goodid }
                viewModel.list?.indexOf(goodsInfo)?.let {
                    if (it != -1) {//刷新相应item
                        idHoldList?.adapter?.notifyItemChanged(it)
                    }
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    private fun initViews() {
        idHoldList = _view?.findViewById(R.id.id_hold_list)

        val adapter = viewModel.getAdapter(this)
        val layoutManager = LinearLayoutManager(context)
        idHoldList?.layoutManager = layoutManager
        idHoldList?.adapter = adapter
    }

    override fun onPause() {
        super.onPause()
        canrefrashQuote = false
    }

    override fun onResume() {
        super.onResume()
        canrefrashQuote = true
        viewModel.queryHold()

    }
    //主动更新数据
    fun onRefresh(){
        viewModel.queryHold()
    }
}