package com.zj365.health.act

import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.FamilyEvent
import com.xty.common.util.CommonToastUtils
import com.xty.common.util.EditTextMonitor
import com.zj365.health.R
import com.zj365.health.databinding.ActFriendWatchBinding
import com.zj365.health.vm.BindFamilyVm
import com.xty.network.model.FamilyBean
import com.xty.network.model.FriendInfo
import org.greenrobot.eventbus.EventBus

/**
 * 家人朋友信息
 */
@Route(path = ARouterUrl.FRIEND_WATCH)
class FriendWatchAct : BaseVmAct<BindFamilyVm>() {
    val status by lazy { arrayOf("未佩戴", "已佩戴") }
    val binding by lazy { ActFriendWatchBinding.inflate(layoutInflater) }
    private val monitor by lazy { EditTextMonitor() }
    var bean: FamilyBean? = null
    var friedInfo: FriendInfo? = null
    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        isDarkBar = true
        intent.extras?.let {
            if (it.containsKey("bean")) {
                bean = it.getSerializable("bean") as FamilyBean

            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        if (bean == null) {
            return
        }

        binding.title.mTvTitle.text = "家人的健康手表"
        binding.title.mTvRight.apply {
            text = getString(R.string.save)
            //visibility = View.VISIBLE
            setTextColor(ContextCompat.getColor(this@FriendWatchAct, R.color.col_009))
            setOnClickListener {
            }
        }

        binding.tvName.setOnClickListener {
            bundle.putSerializable("personInfo", friedInfo)
            RouteManager.goAct(ARouterUrl.PERSON_INFO, bundle)
        }

        mViewModel.getFriendInfo(bean!!.id, bean!!.userJointId.toString())

        binding.etBelong.setOnEditorActionListener(object : OnEditorActionListener {
            override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    val belong = binding.etBelong.text.toString()
                    val nickName = binding.etNickName.text.toString()
                    if (TextUtils.isEmpty(belong)) {
                        CommonToastUtils.showToast("请输入关系")
                        return false
                    }
                    val nickname = if (TextUtils.isEmpty(nickName)) {
                        bean?.fCall
                    } else {
                        nickName
                    }
                    mViewModel.changeFamilyNickName(
                        bean,
                        nickname,
                        binding.etBelong.text.toString(),
                    )
                    return true
                }
                return false
            }
        })

        binding.etNickName.setOnEditorActionListener(object :OnEditorActionListener{
            override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    val belong = binding.etBelong.text.toString()
                    val nickName = binding.etNickName.text.toString()
                    if (TextUtils.isEmpty(nickName)) {
                        CommonToastUtils.showToast("请输入称呼")
                        return false
                    }
                    val bel = if (TextUtils.isEmpty(belong)) {
                        bean?.type
                    } else {
                        belong
                    }
                    mViewModel.changeFamilyNickName(
                        bean,
                        binding.etNickName.text.toString(),
                        bel,
                    )
                    return true
                }
                return false
            }

        })
    }

    override fun liveObserver() {
        mViewModel.friendLive.observe(this) {

            friedInfo = it.data

            binding.etNickName.setText(if (it.data.fcall.isNullOrEmpty()) bean!!.type else it.data.fcall)
            binding.etBelong.setText(it.data.type)
            binding.tvName.text = it.data.name

            var whatchName = if (it.data.watchName == null) "" else it.data.watchName
            binding.mInfo.text = "设备名称: ${whatchName}"
            binding.mDevidCode.text = "设备编号: ${it.data.deviceId}"
            binding.mTime.text = "服务时间: ${it.data.speriodic}"
            binding.mStatus.text = "实时状态: ${it.data.adornState}"

            if (it.data.deviceType != null) {
                when (it.data.deviceType) {
                    "U32" -> {
                        binding.mImage.setImageResource(R.mipmap.ic_watch_u32_big)
                        binding.mStatus.visibility = View.GONE
                    }

                    "A3" -> {
                        binding.mImage.setImageResource(R.mipmap.ic_watch_a3_big)
                    }

                    "X1" -> {
                        binding.mImage.setImageResource(R.mipmap.ic_watch_x1_big)
                    }

                    "X2" -> {
                        binding.mImage.setImageResource(R.mipmap.ic_watch_x2_big)
                    }
                }
            }
        }

        mViewModel.changeOk.observe(this) {
            CommonToastUtils.showSucceedToast("修改成功")
            EventBus.getDefault().post(FamilyEvent())
        }
    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    fun familyChange(event:FamilyEvent){
//        mViewModel.getFriendInfo(bean!!.id,bean!!.userJointId.toString())
//    }
}