package com.zj365.health.act

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.recycleUtils.RecycleItem
import com.xty.common.setImageUser
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.DkAdapter
import com.zj365.health.databinding.ActDkBinding
import com.xty.network.model.DkBean
import com.zj365.health.vm.DkVM
import com.xty.network.model.SettingBean

/**
 * 健康打卡 详情
 */
@Route(path = ARouterUrl.DK_ACT)
class DkAct  : BaseVmAct<DkVM>() {
    val defaultIcon by lazy { arrayOf( R.mipmap.ic_default_man , R.mipmap.ic_default_women)}
    val binding by lazy{ActDkBinding.inflate(layoutInflater)}
    val adapter by lazy{ DkAdapter() }
    var bean= mutableListOf<DkBean>()
    var clickPosition=0

    var user: SettingBean.User?=null
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        user= intent.extras!!.getSerializable("user") as SettingBean.User
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text=resources.getString(R.string.intera_1)
        binding.title.mIvBack.apply {
            this.setOnClickListener {
                finish()
            }
        }
        initRecycle()
        mViewModel.getDkInfo()
        user?.let {
            binding.mTvName.text=it.name
            if(it.avatarUrl.isNullOrEmpty()){
                binding.mImage.setImageResource(defaultIcon[if(it.sex-1==-1)0 else it.sex-1])
            }else{
                binding.mImage.setImageUser(this,it.avatarUrl)
            }

        }

    }
    fun initRecycle(){
        binding.mRecycle.addItemDecoration(RecycleItem(this,30))
        binding.mRecycle.layoutManager= LinearLayoutManager(this)
        binding.mRecycle.adapter=adapter
        adapter.addChildClickViewIds(R.id.mTvStatus)
        adapter.setOnItemChildClickListener { adapter, view, position ->
            if(!view.isSelected){
                clickPosition=position
                mViewModel.clickDk(position+1)
            }
        }
    }

    override fun liveObserver() {
        mViewModel.dkInfo.observe(this){
            var list= mutableListOf<DkBean.ClockIn>()
            for(i in 0..5){  //有6条数据
                var bean=DkBean.ClockIn()
                it.data.clockIn.forEach { clockin->
                  if(!clockin.type.isNullOrEmpty()&&clockin.type!!.toInt()==i+1){
                      bean.isSelect=true
                  }
                }
                list.add(bean)
            }
            adapter.setNewInstance(list)
            binding.mWakl.text=it.data.step.toString()
            binding.mDk.text=it.data.count.toString()


        }
        mViewModel.dkOpration.observe(this){
            CommonToastUtils.showSucceedToast("打卡成功")
            adapter.data[clickPosition].isSelect=true
            binding.mDk.text="${binding.mDk.text.toString().toInt()+1}"
            adapter.notifyDataSetChanged()
        }
    }
}