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

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.AmountLogData
import cn.muchinfo.rma.global.isBlankNumber
import cn.muchinfo.rma.global.toShowTime
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.autoWidget.onThrottleFirstClick
import cn.muchinfo.rma.view.base.BaseViewModel
import com.blankj.utilcode.util.ActivityUtils

class CostViewModel : BaseViewModel() {
    private var orderDetailList: ArrayList<OrderDetailData>? = ArrayList() // 资金信息
//    var costList: ArrayList<CostListData>? = ArrayList() // 资金流水

    /**
     * 资金流水数据
     */
    val costList : MutableLiveData<List<AmountLogData>> = MutableLiveData()

    var type: String? = null

//region cost info 资金信息

    fun queryAmountLog(){
        val params = mutableMapOf<String, String>()
        params["accountID"] = GlobalDataCollection.instance?.accountId.toString()
        MyApplication.getInstance()?.futureManager?.queryAmountLog(params) { isSuccess, respData, _ ->
            if (isSuccess && !respData!!.isNullOrEmpty()) {
                costList.value = respData
            } else {
                // 没有数据或者接口返回错误的时候，通知界面没有获取到数据

            }
        }
    }

//region cost list 资金流水

    fun getCostListAdapter(): RecyclerView.Adapter<CostListAdapter.CostListHolder> {
        return CostListAdapter(context)
    }

    /**
     * 资金流水适配器
     */
    class CostListAdapter(context: Context?): RecyclerView.Adapter<CostListAdapter.CostListHolder>() {
        private var list: ArrayList<AmountLogData>? = null
        private var inflater: LayoutInflater? = null
        private var context: Context? = null
        init {
            this.inflater = LayoutInflater.from(context)
            this.context = context
        }

        // 创建句柄
        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): CostListHolder {
            return CostListHolder(inflater?.inflate(R.layout.cost_list_item, parent, false))
        }

        override fun getItemCount(): Int {
            return list?.size ?: 0
        }


        fun update(list: ArrayList<AmountLogData>?) {
            this.list = list
            notifyDataSetChanged()
        }

        override fun onBindViewHolder(holder: CostListHolder, position: Int) {
            holder.idTime?.text = list?.get(position)?.createtime?.toShowTime()
            holder.idOperateType?.text = list?.get(position)?.OPERATETYPENAME
            holder.idAm?.text = list?.get(position)?.currentbalance?.isBlankNumber()
            holder.all_view?.onThrottleFirstClick {
                val intent = Intent()
                intent.putExtra("data",list?.get(position))
                intent.setClass(context!!,CostDetailsActivity::class.java)
                ActivityUtils.startActivity(intent)
            }
        }

        class CostListHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            var idOperateType = itemView?.findViewById<TextView>(R.id.id_operate_type) // 操作类型
            var idAm = itemView?.findViewById<TextView>(R.id.id_am) // 资金
            var idTime = itemView?.findViewById<TextView>(R.id.id_time) // 时间
            var all_view = itemView?.findViewById<ConstraintLayout>(R.id.all_view)
        }

    }

}