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

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.AvaildGoodsGroupData
import cn.muchinfo.rma.global.data.account.loginQeruy.GoodsInfo
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.base.BaseViewModel
import com.blankj.utilcode.util.ToastUtils

class MarketBaseModel : BaseViewModel() {
    var list: List<GoodsInfo>? = null
    var type: String? = null

    /**
     * 获取配置器。
     */
    fun getAdapter(): RecyclerView.Adapter<MyAdapter.MarketHolder> {
        return MyAdapter(context, list)
    }

    fun getgoodsCodes(first: Int, last: Int): String {
        var goodsCodes = ""
        val newList = list?.subList(first, last)
        newList?.forEach {
            goodsCodes = goodsCodes + it.outgoodscode + ","
        }
        return goodsCodes
    }

    /**
     * 请求盘面信息
     * @param first Int
     * @param last Int
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     */
    fun queryQuoteDay(
        first: Int,
        last: Int,
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        val goodsCodes = getgoodsCodes(first, last)
        val params = mutableMapOf<String, String>().apply {
            put("goodsCodes", goodsCodes)
        }
        MyApplication.getInstance()?.futureManager?.queryQuoteDay(params = params) { isSuccess, respData, error ->
            if (isSuccess) {
                val map = GlobalDataCollection.instance?.getmGoodsInfoClass()
                respData?.forEach {
                    map?.put(it.goodscode, it)
                }
                callback(true, null)
            } else {
                callback(false, error)
            }
        }
    }

    /**
     * 通用的适配器。
     */
    class MyAdapter(context: Context?, list: List<GoodsInfo>?) :
        RecyclerView.Adapter<MyAdapter.MarketHolder>() {
        private var list: List<GoodsInfo>? = null
        private var inflater: LayoutInflater? = null
        private var context: Context? = null
        private var choose: Drawable? = null
        private var unChoose: Drawable? = null
        private var noChange : Drawable? = null
        private var pink: Drawable? = null
        private var green: Drawable? = null
        private var priceRed: Int = 0
        private var priceGreen: Int = 0
        private var noChangeBlack : Int = 0

        init {
            this.list = list
            this.inflater = LayoutInflater.from(context)
            this.context = context
            GlobalDataCollection.instance
            choose = context?.getDrawable(R.mipmap.rma_choose)
            unChoose = context?.getDrawable(R.mipmap.rma_un_choose)
            pink = context?.getDrawable(R.drawable.rma_red)
            green = context?.getDrawable(R.drawable.rma_green)
            priceGreen = context?.getColor(R.color.p_price_green)!!
            priceRed = context.getColor(R.color.p_price_red)
            noChange = context.getDrawable(R.drawable.rma_white)
            noChangeBlack = context.getColor(R.color.rma_black_33)
        }

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

        class MarketHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            var idChoose = itemView?.findViewById<RelativeLayout>(R.id.id_choose)
            var idIsChoose = itemView?.findViewById<ImageView>(R.id.id_is_choose)
            var idGoodsName = itemView?.findViewById<TextView>(R.id.id_goods_name)
            var idGoodsCode = itemView?.findViewById<TextView>(R.id.id_goods_code)
            var idPrice = itemView?.findViewById<TextView>(R.id.id_price)//价格
            var idUpDown = itemView?.findViewById<TextView>(R.id.id_up_down)//涨跌幅
            var idHoldTotal = itemView?.findViewById<TextView>(R.id.id_hold_total)//持仓量
        }

        override fun onBindViewHolder(holder: MarketHolder, position: Int) {
            holder.idGoodsName?.text = list?.get(position)?.goodsname
            holder.idGoodsCode?.text = list?.get(position)?.goodscode

            //获取盘面信息
            val quoteDayData = GlobalDataCollection.instance?.getmGoodsInfoClass()?.get(list?.get(position)?.outgoodscode)
            if (quoteDayData != null){
                //最新价->今开->昨结 价格
                if (quoteDayData.last != 0.0){//优先最新价
                    holder.idPrice?.text = quoteDayData.last.toString()
                }else{//今开
                    if (quoteDayData.opened != 0.0){
                        holder.idPrice?.text = quoteDayData.opened.toString()
                    }else{//昨结
                        if (quoteDayData.preclose != 0.0){
                            holder.idPrice?.text = quoteDayData.preclose.toString()
                        }else{
                            holder.idPrice?.text = "--"
                        }
                    }
                }

                //涨跌
                if (quoteDayData.last != 0.0 && quoteDayData.preclose != 0.0){
                    holder.idUpDown?.text = quoteDayData.last.minus(quoteDayData.preclose).toString()
                }else{
                    holder.idUpDown?.text = "--"
                }

                 val upDown = quoteDayData.last.minus(quoteDayData.preclose)
                if (upDown == 0.0){
                    holder.idUpDown?.background = noChange
                    holder.idUpDown?.setTextColor(noChangeBlack)
                    holder.idPrice?.background = noChange
                    holder.idPrice?.setTextColor(noChangeBlack)
                }else if (upDown > 0){
                    holder.idUpDown?.background = pink
                    holder.idUpDown?.setTextColor(priceRed)
                    holder.idPrice?.background = pink
                    holder.idPrice?.setTextColor(priceRed)
                }else if (upDown < 0){
                    holder.idUpDown?.background = green
                    holder.idUpDown?.setTextColor(priceGreen)
                    holder.idPrice?.background = green
                    holder.idPrice?.setTextColor(priceGreen)
                }

                holder.idHoldTotal?.text = quoteDayData.totalvolume
            }else{
                holder.idHoldTotal?.text = "--"
                holder.idUpDown?.text = "--"
                holder.idPrice?.text = "--"
            }


            // 是否自选的
//            if (list?.get(position)?.isChoose == true) {
            holder.idIsChoose?.setImageDrawable(choose)
//            } else {
//                holder.idIsChoose?.setImageDrawable(unChoose)
//            }
            holder.idChoose?.setOnClickListener {
                ToastUtils.showShort(list?.get(position)?.goodsname)
            }
        }

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

    }
}