package com.zj365.health.adapter

import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.zj365.health.R
import com.zj365.health.model.MyCompareBean


//  var sexType=1   //1男 2女
class BodyCompareAdapter(var sexType:Int=1):BaseAdapter<MyCompareBean>(R.layout.item_body_compare) {
    var listUnit= mutableListOf<String>()
    private  val  stateIv= listOf("-","↓","↑")
    private val stateCol= listOf(R.color.col_9BDd,R.color.col_96D)

    //男性
    private var infoRange1=56.1..68.7  //水分
    private var infoRange2=10.0..20.0  //体脂肪
    private var infoRange3=18.5..25.0  //身体质量参数
    private var infoRange4=0.75..0.85  //腰臀脂肪
    private var infoRange5=1200.0..1400.0  //基础代谢

    //女性
    private var infoFemaleRange1=50.0..62.1  //水分
    private var infoFemaleRange2=18.0..28.0  //体脂肪
    private var infoFemaleRange3=18.5..25.0  //身体质量参数
    private var infoFemaleRange4=0.70..0.80  //腰臀脂肪
    private var infoFemaleRange5=1200.0..1400.0  //基础代谢

    //男性正常范围
    private var normalMaleRange= mapOf(2 to infoRange1,3 to infoRange2,4 to infoRange3,5 to infoRange4,6 to infoRange5)
    //女性正常范围
    private var normalFemaleRange= mapOf(2 to infoFemaleRange1,3 to infoFemaleRange2,4 to infoFemaleRange3,5 to infoFemaleRange4,6 to infoFemaleRange5)



    override fun convert(holder: BaseViewHolder, item: MyCompareBean) {
        holder.setText(R.id.mName,"${item.name}")
            .setText(R.id.mCompareInfo,"${item.compareData}${listUnit[holder.layoutPosition]}")
            .setText(R.id.mInfo,"${item.data}${listUnit[holder.layoutPosition]}")

            .setGone(R.id.mCompareState,(item.compareData=="--"||item.compareData=="0"))
            .setGone(R.id.mState,(item.compareData=="--"||item.compareData=="0")  )
        when(holder.layoutPosition){
            7,8,10->  holder.setText(R.id.mStateIv,stateIv[0]).setTextColorRes(R.id.mStateIv,R.color.col_25c)
            else->{

                var compareDataValue=if(item.compareData=="--" || item.compareData.isEmpty()) "0" else item.compareData
                var dataValue=if(item.data=="--" || item.data.isEmpty()) "0" else item.data

               /* if(item.compareData=="--"||item.data=="--"||item.compareData.isEmpty()||item.data.isEmpty()){
                    holder.setText(R.id.mStateIv,stateIv[0]).setTextColorRes(R.id.mStateIv,R.color.col_25c)
                }else {*/
                    if (compareDataValue.toFloat() > dataValue.toFloat()) {
                        holder.setText(R.id.mStateIv, stateIv[1])
                        holder.setTextColorRes(R.id.mStateIv, R.color.col_25c)
                    } else if (compareDataValue.toFloat() < dataValue.toFloat()) {
                        holder.setText(R.id.mStateIv, stateIv[2])
                        holder.setTextColorRes(R.id.mStateIv, R.color.col_96D)
                    } else {
                        holder.setText(R.id.mStateIv, stateIv[0])
                        holder.setTextColorRes(R.id.mStateIv, R.color.col_25c)
                    }
               // }
            }
        }


        var ranMap=if(sexType==1){
            normalMaleRange
        }else{
            normalFemaleRange
        }
        if(ranMap.containsKey(holder.layoutPosition)){
           var range= ranMap[holder.layoutPosition]!!
            if(item.data!="--"){
                    var float=   item.data.toFloat()
                    if(float  in range){
                        holder.setText(R.id.mState,"正常")
                    }else if(float<range.start){
                        holder.setText(R.id.mState,"低")
                    }else if(float>range.endInclusive){
                        holder.setText(R.id.mState,"高")
                    }
            }
            if(item.compareData!="--"){
                var float=   item.compareData.toFloat()
                if(float  in range){
                    holder.setText(R.id.mCompareState,"正常")
                }else if(float<range.start){
                    holder.setText(R.id.mCompareState,"低")
                }else if(float>range.endInclusive){
                    holder.setText(R.id.mCompareState,"高")
                }
            }

            holder.setGone(R.id.mState,item.data=="--")
            holder.setGone(R.id.mCompareState,item.compareData=="--")

        }else{
            holder.setGone(R.id.mState,true)
            holder.setGone(R.id.mCompareState,true)
        }

    }
}