package com.zj365.health.adapter.familyhealth

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.setImageUser
import com.xty.network.model.FamilyHealthBean
import com.zj365.health.R
import de.hdodenhof.circleimageview.CircleImageView

class HealthFamilyAdapter: BaseAdapter<FamilyHealthBean>(R.layout.item_health_family) {



    override fun convert(holder: BaseViewHolder, item: FamilyHealthBean) {

        var tvRelation = holder.getView<TextView>(R.id.tv_relation)

        var tvPower = holder.getView<TextView>(R.id.tv_power)

        var recycler = holder.getView<RecyclerView>(R.id.recycler)

        var tvAddOrCancel = holder.getView<TextView>(R.id.tv_add_cancel)

        var avatarRecycler = holder.getView<RecyclerView>(R.id.user_avatar_recycler)
        var mAvatarAdapter  = HealthFamilyUserAvatorAdapter()
        avatarRecycler.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        avatarRecycler.adapter = mAvatarAdapter

        holder.setText(R.id.tv_family_name,item.name)
        holder.getView<CircleImageView>(R.id.img_avator).setImageUser(context,item.avatarUrl)
        tvRelation.text = item.relation
        if(item.isMy == 1){ ////是否本人0，否，1，是

            tvRelation.background = context.resources.getDrawable(R.drawable.shape_r7_3fe_8f6)
            tvRelation.setTextColor(context.resources.getColor(R.color.col_6ea))

            tvAddOrCancel.text ="添加监护人"
            tvAddOrCancel.background = context.resources.getDrawable(R.drawable.shape_r25_02c)
            tvAddOrCancel.setTextColor(context.resources.getColor(R.color.white))

            holder.setGone(R.id.user_avatar_recycler,false)
            holder.setGone(R.id.tv_num,false)
            holder.setGone(R.id.tv_watch_him,true)
            item.avatarList?.let {
                mAvatarAdapter.setNewInstance(it)
                holder.setText(R.id.tv_num,"${it.size}/10")
            }

        }else {
            tvRelation.background = context.resources.getDrawable(R.drawable.shape_r7_5f2_481)
            tvRelation.setTextColor(context.resources.getColor(R.color.col_481))

            tvAddOrCancel.text ="退出监护"
            tvAddOrCancel.background = context.resources.getDrawable(R.drawable.shape_r25_stroke_02c)
            tvAddOrCancel.setTextColor(context.resources.getColor(R.color.col_02c))

            holder.setGone(R.id.user_avatar_recycler,true)
            holder.setGone(R.id.tv_num,true)
            holder.setGone(R.id.tv_watch_him,true)


        }
        if(item.power != null){
            item.power?.let {
                tvPower.text = "${it}%"
            }
        }else{
            tvPower.text = "--%"
        }

        if(item.isRealTime == 1){
            holder.setGone(R.id.tv_current_time,false)
        }else{
            holder.setGone(R.id.tv_current_time,true)

        }


        val drawableLeft = context.resources.getDrawable(R.mipmap.icon_power_green)
        drawableLeft.setBounds(0,0,drawableLeft.minimumWidth,drawableLeft.minimumHeight)
        tvPower.setCompoundDrawables(drawableLeft,null,null,null)
        tvPower.compoundDrawablePadding = 5
        var mAdapterTaget =  HealthFamilyTargetAdapter()


        recycler.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        recycler.adapter = mAdapterTaget
        mAdapterTaget.setNewInstance(item.dataList)

    }
}