package com.zj365.health.act

import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.tamsiree.rxkit.RxTimeTool
import com.xty.base.act.BaseVmAct
import com.xty.common.*
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.picture.PictureUtils
import com.xty.common.recycleUtils.RecycleItem
import com.xty.common.util.CommonToastUtils
import com.xty.common.util.CommonUtils
import com.zj365.health.R
import com.zj365.health.adapter.XdAnswerAdapter
import com.zj365.health.databinding.ActXdDetailBinding
import com.zj365.health.vm.XdVm
import com.xty.network.model.XdInfoBean
import java.text.SimpleDateFormat

/**
 *
 * 心电分析详情
 */
@Route(path = ARouterUrl.XD_DETAIL)
class XdDetailAct: BaseVmAct<XdVm>() {
    //val mAdapter by lazy{ XdAdapter() }

    var typeName=""
    var isAif:Boolean=false
    val array by lazy{ arrayOf("窦性心动过缓","窦性心动过速","窦性心律不齐","房颤","房性早搏","室性早搏","ST段抬高","ST段压低","T波倒置","房室传导阻滞")}
    val mAdapterAnswer by lazy{ XdAnswerAdapter() }
    val binding by lazy {ActXdDetailBinding.inflate(layoutInflater)}
    var dataLiat= mutableListOf<Int>()
    var bean: XdInfoBean?=null
    var hr=""
//    var hrv=""
//    var dbp=""
    var imagePath=""
    var state=-1 //状态
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        intent.extras?.let {
            if(it.containsKey("bean")){
                bean=it.getSerializable("bean") as XdInfoBean
            }else{
                hr=it.getString("hr").toString()
//                hrv= it.getString("hrv").toString()
//                dbp=it.getString("dbp").toString()
                typeName=it.getString("typeName").toString()
                imagePath=  it.getString("image").toString()
                isAif=  it.getBoolean("isAif")
            }
        }


    }


    override fun initView() {
        super.initView()
        statusBar(binding.mTitle.mView)
        binding.mTitle.mTvTitle.text=resources.getString(R.string.xd_detail)
        var time=RxTimeTool.getCurTimeString(SimpleDateFormat("HH:mm"))

        var parentView = binding.mTitle.mTvTitle.parent as View
        var bg = ContextCompat.getDrawable(this, R.drawable.shape_bg_health_round)
        parentView.background = bg
        binding.mTitle.mIvBack.apply {
            this.setImageResource(R.mipmap.icon_back_white)
            this.setOnClickListener {
                finish()
            }
        }
        binding.mTitle.mIvReight.apply {
            this.visibility=View.VISIBLE
            this.setImageResource(R.mipmap.icon_share)
            this.setOnClickListener {

                getPageScreen()

            }
        }

        //默认拿本人的信息
        if(null != bean && null != Const.otherLiveData && bean!!.userId == Const.otherLiveData!!.value?.user?.id){
            //如果非本人则拿otherLiveData里面的性别年龄信息
            Const.otherLiveData?.value?.let {
                binding.mTv2.text=if(it.user.sex==1) "男" else "女"
                binding.mTv3.text=it.user.age
            }
        }else{
            //如果是本人则拿mainLiveData里面的性别年龄信息
            CommonUtils.getMyUserInfo()?.let {
                binding.mTv2.text=if(it.sex==1) "男" else "女"
                binding.mTv3.text=it.age.toString()
            }
        }


        if(bean!=null){
            binding.mTv1.text=bean!!.hrData.toString()
//            binding.mTv4.text=bean!!.dbpData
//            binding.mTv5.text=bean!!.hrvData.toString()
            binding.mImage.apply {
                setImage(context, getImageUrl(bean!!.fileUrl))
            }
            binding.mImage.tag= getImageUrl(bean!!.fileUrl)
            var time=RxTimeTool.milliseconds2String( RxTimeTool.string2Milliseconds(bean!!.createTime),SimpleDateFormat("HH:mm"))
            var mTypeName = if(bean!!.state == "正常") "未见异常" else bean!!.state
            binding.mTypeName.setCenterBottom(mTypeName, time)
            if(bean!!.state=="正常"){
               binding.mDesc.visibility= View.VISIBLE
            }else{
                binding.mDesc.visibility= View.GONE
            }
        }else{
            binding.mTv1.text=hr
//            binding.mTv4.text=dbp
//            binding.mTv5.text=hrv
            binding.mImage.setImage(this,imagePath)
            binding.mImage.tag=imagePath
            var mTypeName = if (typeName == "正常") "未见异常" else typeName
            binding.mTypeName.setCenterBottom(mTypeName, time)
            if(typeName=="正常"){
                binding.mDesc.visibility= View.VISIBLE
            }else{
                binding.mDesc.visibility= View.GONE
            }
        }
        var stateStr=binding.mTypeName.center_txt
        when(stateStr){
            "房颤"->state=3
            "室性早博"->state=5
            "房性早博"->state=4
        }

        mAdapterAnswer.status=state
        binding.mImage.setOnClickListener {
            if(it.tag!=null){
                PictureUtils.httpPreviewImage(this,0, mutableListOf(it.tag.toString()))
            }

        }
        initRecycle()
    }

    /**
     *截取当前界面的图片
     */
    fun getPageScreen() {
        binding.mChildView.post {
            var startH=0
          //  var startH=(binding.mTitle.mTvTitle.parent as View).height
            var bitmap = binding.mChildView.Screen(startH)
            WxUtils.shareBitmap(this, bitmap)
        }
    }

    private fun initRecycle() {
        var flexBox= FlexboxLayoutManager(this)
        //设置主轴排列方式
        flexBox.flexDirection = FlexDirection.ROW
        //设置是否换行
        flexBox.setFlexWrap(FlexWrap.WRAP);
        flexBox.setAlignItems(AlignItems.STRETCH)
        binding.mRecycleAnswer.adapter=mAdapterAnswer
        //binding.mRecycleAnswer.layoutManager=flexBox
        val recycleItem = RecycleItem(this, 4, 1)
        recycleItem.setSpace(40, 40)
        binding.mRecycleAnswer.addItemDecoration(recycleItem)
        binding.mRecycleAnswer.layoutManager=GridLayoutManager(this,3)
        mAdapterAnswer.setNewInstance(array.toMutableList())
        mAdapterAnswer.setOnItemClickListener { adapter, view, position ->
            var name=adapter.getItem(position) as String
             mViewModel.uploadXmInfo("${position+1}")
        }

        //
    }

    override fun liveObserver() {
        mViewModel.xdLive.observe(this){
            if(it.data==null){
                CommonToastUtils.showToast("无此解释")
                return@observe
            }
            bundle.clear()
            bundle.putSerializable("bean",it.data)
            RouteManager.goAct(ARouterUrl.XD_INFO,bundle)

        }

    }


}