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

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.view.autoWidget.DetailItem
import cn.muchinfo.rma.view.base.BaseViewModel

class CostViewModel : BaseViewModel() {
    private var costInfoList: ArrayList<CostInfoData>? = ArrayList() // 资金信息
    var costList: ArrayList<CostListData>? = ArrayList() // 资金流水
    var type: String? = null

//region cost info 资金信息
    /**
     * 组装数据
     */
    fun makeCostInfo() {
        costInfoList?.clear()
        val paddingLeft = 250 // 右侧的文字，距离左侧的距离
        val leftTextPadding = 0 // 左侧的文字，距离左侧的距离

        var m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_currency))
        m.setRightText("USD")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_current_equity))
        m.setRightText("1000.01")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_available_am))
        m.setRightText("55522.02")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_occupation_amount))
        m.setRightText("5522.29")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_freezing_of_funds))
        m.setRightText("599.45")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_position_profit_loss))
        m.setRightText("955.66")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_close_profit_loss))
        m.setRightText("1485.34")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_in_am))
        m.setRightText("72222.1")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_out_am))
        m.setRightText("985.25")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)

        m = CostInfoData()
        m.setLeftText(context?.getString(R.string.str_rate_of_use))
        m.setRightText("10.5%")
        m.setRightTextPadding(paddingLeft)
        m.setLeftTextPadding(leftTextPadding)
        costInfoList?.add(m)
    }

    /**
     * 获取配置器。
     */
    fun getCostInfoAdapter(): RecyclerView.Adapter<CostInfoAdapter.CostInfoHolder> {
        return CostInfoAdapter(context, costInfoList)
    }

    /**
     * 资金信息的适配器。
     */
    class CostInfoAdapter(context: Context?, list: ArrayList<CostInfoData>?) : RecyclerView.Adapter<CostInfoAdapter.CostInfoHolder>() {
        private var list: ArrayList<CostInfoData>? = null
        private var inflater: LayoutInflater? = null
        private var context: Context? = null
        init {
            this.list = list
            this.inflater = LayoutInflater.from(context)
            this.context = context
        }

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

        class CostInfoHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            var detailItem = itemView?.findViewById<DetailItem>(R.id.id_item)
        }

        override fun onBindViewHolder(holder: CostInfoHolder, position: Int) {
            holder.detailItem?.setLeftText(list?.get(position)?.getLeftText().toString())
            holder.detailItem?.setRightText(list?.get(position)?.getRightText().toString())
            list?.get(position)?.getRightTextPadding()?.let { holder.detailItem?.setRightValuePadding(it) }
            list?.get(position)?.getLeftTextPadding()?.let { holder.detailItem?.setLeftValuePadding(it) }
            holder.detailItem?.showLine(true)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CostInfoHolder {
            return CostInfoHolder(inflater?.inflate(R.layout.cost_info_item, parent, false))
        }

    }
//endregion

//region cost list 资金流水

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

    /**
     * 资金流水适配器
     */
    class CostListAdapter(context: Context?, list: ArrayList<CostListData>?): RecyclerView.Adapter<CostListAdapter.CostListHolder>() {
        private var list: ArrayList<CostListData>? = null
        private var inflater: LayoutInflater? = null
        private var context: Context? = null
        init {
            this.list = list
            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
        }

        override fun onBindViewHolder(holder: CostListHolder, position: Int) {
            holder.idTime?.text = list?.get(position)?.getTime()
            holder.idOperateType?.text = list?.get(position)?.getOperateType()
            holder.idAm?.text = list?.get(position)?.getAm()
        }

        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) // 时间
        }

    }


//endregion
}