package com.zj365.health.act.familyhealth

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.xty.base.act.BaseListAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.AddSuccessEvent
import com.xty.common.util.CommonUtils
import com.xty.network.model.CancerGoodsBean
import com.xty.network.model.FamilyHealthBean
import com.xty.network.model.InviteCustodyBean
import com.zj365.health.R
import com.zj365.health.adapter.familyhealth.HealthFamilyAdapter
import com.zj365.health.databinding.ActHealthFamilyBinding
import com.zj365.health.vm.FamilyHealthVm
import com.zj365.health.weight.DialogTipNew
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = ARouterUrl.FAMILY_HEALTH_ACT)
class FamilyHealthAct : BaseListAct<FamilyHealthVm>() {
    val binding by lazy { ActHealthFamilyBinding.inflate(layoutInflater) }
    val mAdapter by lazy { HealthFamilyAdapter() }

    var inviteCustodyBean: InviteCustodyBean? = null

    val dialogInvite by lazy {
        DialogTipNew(this, "监护邀请","", "确认","取消",false,{
            inviteCustodyBean?.let {
                mViewModel.confirmInvite(it.id,"1")
            }
        }){
            inviteCustodyBean?.let {
                mViewModel.confirmInvite(it.id,"0")
            }
        }

    }

    override fun liveObserver() {
        mViewModel.familyHealthListData.observe(this){
            mAdapter.setNewInstance(it.data)
        }

        mViewModel.exitCustody.observe(this){
            page =1
            loadData()
        }

        mViewModel.findInfoLiveData.observe(this){
            if (it.data != null){
                inviteCustodyBean = it.data
                dialogInvite.setContentNew("${CommonUtils.replaceMiddleNumber(it.data.phone)}用户邀请您成为 监护人")
                dialogInvite.show()
            }

        }
    }

    override fun setLayout() = binding.root

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "家人健康"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.recyclerView, binding.mRefresh, true)
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = mAdapter
        mAdapter.addChildClickViewIds(R.id.tv_add_cancel, R.id.tv_watch,R.id.tv_current_time,R.id.user_avatar_recycler,R.id.tv_num)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val familyHealthBean = adapter.data[position] as FamilyHealthBean
            when (view.id) {
                R.id.tv_add_cancel -> {
                  if(familyHealthBean.isMy == 1){
                      RouteManager.goAct(ARouterUrl.ADD_FAMILY_GUARDIAN_ACT)
                  }else{
                    mViewModel.exitCustody(familyHealthBean.id.toString())
                  }
                }
                R.id.tv_num ->{
                    RouteManager.goAct(ARouterUrl.FAMILY_GUARDIAN_LIST_ACT)

                }
                R.id.user_avatar_recycler ->{
                    RouteManager.goAct(ARouterUrl.FAMILY_GUARDIAN_LIST_ACT)
                }
                R.id.tv_current_time ->{
                    bundle.clear()
                    bundle.putLong("id",familyHealthBean.userId)
                    RouteManager.goAct(ARouterUrl.FAMILY_HEALTH_REAL_TIME_ACT,bundle)

                }
                R.id.tv_watch ->{
                    bundle.clear()
                    bundle.putString("id",familyHealthBean.userId.toString())
                    RouteManager.goAct(ARouterUrl.WATCH_FAMILY_HEALTH,bundle)
                }
            }
        }
    }

    override fun loadData() {
        if(page == 1){
            mViewModel.searchInviteInfo()
        }
       mViewModel.getFamilyHealthList()
    }

    override fun initData() {
        super.initData()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun addSuccess(event: AddSuccessEvent){
        page = 1
        if(page == 1){
            mViewModel.searchInviteInfo()
        }
        loadData()
    }

}