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

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.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.futureOrders.FutureEntrustData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseFragment
import cn.muchinfo.rma.view.base.future.enums.CustomerEnums
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.TaskUiModel
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.linearLayout
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.support.v4.UI

/**
 * 今日委托和今日可撤
 */
class EntrustChildFragment(type: CustomerEnums.EntrustType) : BaseFragment<EntrustChildViewModel>() {
    private var type: CustomerEnums.EntrustType? = type
    private var loadingDialog: Dialog? = null

    /**
     * 撤单请求状态
     */
    val loadingDialogStatus: MutableLiveData<TaskUiModel> = MutableLiveData()

    companion object {
        fun getInstance(type: CustomerEnums.EntrustType) = EntrustChildFragment(type)
    }

    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout
    private var idEntrustList: RecyclerView? = null // 委托单列表

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return UI {
            linearLayout {
                loadingDialog = createLoadingDialog(hintStr = "请求中")
                loadingDialog?.bindTaskStatus(context!!, loadingDialogStatus)
                statusLayout(contentBlock = {
                    statusLayout = this
                    swipeToLoadLayout {
                        swipeToLayout = this
                        setEnableRefresh(true)
                        setEnableLoadMore(false)
                        setOnRefreshListener {
                            viewModel.queryOrder()
                        }
                        setEnableScrollContentWhenLoaded(false)
                        setEnableLoadMoreWhenContentNotFull(false)
                        recyclerView {
                            idEntrustList = this
                            background = resources.getDrawable(R.color.white, context.theme)
                            adapter = viewModel.getAdapter(this@EntrustChildFragment)
                        }.lparams(matchParent, matchParent)
                    }
                }, emptyBlock = {
                    emptyView(hint = resources.getString(R.string.now_no_data))
                }).lparams(matchParent, matchParent)
            }

            // 有数据来了，就通知刷新
            viewModel.entrustLiveData.bindOptional(context!!) { data ->

                (idEntrustList?.adapter as EntrustChildViewModel.EntrustAdapter).update(data as ArrayList<FutureEntrustData>?)
                if (swipeToLayout.getIsRefreshing()) {
                    swipeToLayout.finishRefresh()
                }
            }
            // 没有数据，就显示无数据
            viewModel.noDataLiveData.bindOptional(context!!) {
                statusLayout.showEmpty()
                if (swipeToLayout.getIsRefreshing()) {
                    swipeToLayout.finishRefresh()
                }
            }

        }.view
    }

    override fun onResume() {
        super.onResume()
        viewModel.queryOrder()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) viewModel.queryOrder()
    }

    fun refresh(){
        viewModel.queryOrder()
    }
//region

//endregion
}