package com.zj365.health.act.healthcode

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.xty.base.act.BaseVmAct
import com.xty.base.vm.BaseVm
import com.xty.common.arouter.ARouterUrl
import com.zj365.health.databinding.ActHealthBodyInquiryBinding
import com.zj365.health.vm.HealthBodyInquiryVm
import me.chunyu.call_kit.CallKit

@Route(path = ARouterUrl.HEALTH_BODY_INQUIRY_ACT)
class HealthBodyInquiryAct : BaseVmAct<HealthBodyInquiryVm>() {

    val binding by lazy { ActHealthBodyInquiryBinding.inflate(layoutInflater) }

    var id:Long = 0

    override fun liveObserver() {
        mViewModel.inquiryDetailLiveData.observe(this){
            binding.title.mTvTitle.text = "${it.data.ref_name}"
            binding.tvDepartment.text = it.data.department
            binding.tvSurvey.text = it.data.condition
            binding.tvDefinition.text = it.data.seek
            binding.tvPathogeny.text = it.data.etiology

        }

        mViewModel.videoDoctorLive.observe(this){
            if (it.data.type == 2){
                CallKit.startCallKitLogic(this@HealthBodyInquiryAct,it.data.url)
            }else{
                val appId = "wx032bacb749c0dcb0" // 填移动应用(App)的 AppId，非小程序的 AppID

                val api = WXAPIFactory.createWXAPI(this@HealthBodyInquiryAct, appId)

                val req = WXLaunchMiniProgram.Req()
                req.userName = it.data.wxid // 填小程序原始id

                req.path = it.data.url ////拉起小程序页面的可带参路径，不填默认拉起小程序首页，对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"。
                if(it.data.wxtype== 1){
                    req.miniprogramType =
                        WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE // 可选打开 开发版，体验版和正式版
                }else{
                    req.miniprogramType =
                        WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW // 可选打开 开发版，体验版和正式版
                }

                api.sendReq(req)
            }
        }
    }

    override fun setLayout() = binding.root

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        id = intent.extras!!.getLong("id")
        mViewModel.getInquiryDetail(id)

        binding.imgVideoInquiry.setOnClickListener {
            mViewModel.getVideoDoctorType()
        }
    }
}