package com.zj365.health.act.datamanage

import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.core.glideimageloader.GlideImageLoader
import com.xiaoguang.widget.entry.BaseImgUrlBean
import com.xiaoguang.widget.viewer.PhotoViewer
import com.xiaoguang.widget.viewer.TransferConfig
import com.xty.base.act.BaseVmAct
import com.xty.common.SizeUtil
import com.xty.common.arouter.ARouterUrl
import com.zj365.health.R
import com.zj365.health.adapter.PictureShowBigAdapter
import com.zj365.health.databinding.ActDataManageMedicalDetailBinding
import com.zj365.health.vm.DataManageVm
import com.xty.network.model.MedicalDataBean

/**
 *  异常数据
 */
@Route(path = ARouterUrl.DATAMANAGE_DETAIL_ACT)
class DataDetailMedicalAct : BaseVmAct<DataManageVm>() {
    val binding by lazy { ActDataManageMedicalDetailBinding.inflate(layoutInflater) }
    val adapter by lazy { PictureShowBigAdapter() }
    private val adapter1 by lazy { PictureShowBigAdapter() }
    private var resultData: MedicalDataBean? = null
    var isManage = false

    var phoneView: PhotoViewer? = null
    var config: TransferConfig? = null
    private fun getDataType(dataType: Int): String {
        val showName = when (dataType) {
            1 -> {
                "用户上传"
            }

            2 -> {
                "管家上传"
            }

            3 -> {
                "物联网上传"
            }

            4 -> {
                "管理员上传"
            }

            else -> {
                "家人上传"
            }
        }
        return showName
    }

    val textColor by lazy { arrayOf(R.color.col_9BD, R.color.col_9Bd, R.color.col_FDA481) }
    val bg by lazy {
        arrayOf(
            R.drawable.shape_xya_status_yellow,
            R.drawable.shape_xya_status,
            R.drawable.shape_xya_status_orange,
        )
    }

    override fun setLayout() = binding.root
    var list: ArrayList<String>? = null
    override fun initData() {
        super.initData()
        val type = intent.getStringExtra("type")
        val result = intent.getStringExtra("data")
        result?.let {
            resultData = GsonUtils.fromJson(it, MedicalDataBean::class.java)
        }

        phoneView = PhotoViewer.getDefault(this)

        val data1 = mutableListOf<String>()
        val data2 = mutableListOf<String>()

        val list = mutableListOf<BaseImgUrlBean>()
        var recycleView: RecyclerView = binding.rvList
        resultData?.examinationList?.forEach {
            data1.add(it.imageUrl!!)
            adapter.setNewInstance(data1)
            val url = BaseImgUrlBean(it.imageUrl)
            list.add(url)
        }

        resultData?.hospitalList?.forEach {
            data1.add(it.imageUrl!!)
            adapter.setNewInstance(data1)
            val url = BaseImgUrlBean(it.imageUrl)
            list.add(url)
        }

        resultData?.medicationList?.forEach {
            data2.add(it.imageUrl!!)
            adapter1.setNewInstance(data2)
            val url = BaseImgUrlBean(it.imageUrl)
            list.add(url)
            recycleView = binding.rvMedicalList
        }

        config = TransferConfig.build()
            .setSourceBeanList(list)
            .setImageLoader(GlideImageLoader.with(this)).bindRecyclerView(recycleView, R.id.ivImage)
        binding.tvRemark.text = resultData?.remark


        binding.uploadStatus.text = getDataType(resultData?.upType!!)
        val colorIndex = when (resultData?.upType) {
            1 -> {
                0
            }

            2,5 -> {
                1
            }

            else -> {
                2
            }
        }
        binding.uploadStatus.setBackgroundResource(bg[colorIndex])
        binding.uploadStatus.apply {
            setTextColor(resources.getColor(textColor[colorIndex]))
            setPadding(
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f),
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f)
            )
        }

        when (type) {
            "7" -> {
                binding.title.mTvTitle.text = "用药详情"
                binding.llMedicalLayout.visibility = View.VISIBLE
                binding.llTijianLayout.visibility = View.GONE

                binding.tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,18f)
                if (resultData?.updateTime.isNullOrEmpty()){
                    binding.tvTitle.text = "上传时间: ${resultData?.createTime?:""}"
                }else{
                    binding.tvTitle.text = "上传时间: ${resultData?.updateTime?:""}"
                }

                binding.tvMeicalName.text = "药品名称: ${(resultData?.medicineName)?:""}"
                binding.tvMeicalStartTime.text =
                    "开始时间: ${resultData?.startTime?.replace("00:00:00", "")?:""}"
                binding.tvMeicalEndTime.text =
                    "结束时间: ${resultData?.endTime?.replace("00:00:00", "")?:""}"

            }
            "6" -> {
                binding.llMedicalLayout.visibility = View.GONE
                binding.title.mTvTitle.text = "体检详情"
                binding.tvTitle.text = resultData?.name
                if (resultData?.updateTime.isNullOrEmpty()){
                    binding.tvUploadTime.text = resultData?.createTime?:""
                }else{
                    binding.tvUploadTime.text = resultData?.updateTime?:""
                }
                resultData?.time?.replace("00:00:00", "")?.let {
                    binding.tvTijianTime.text = it.trim()
                }
                binding.tvOrg.text = resultData?.orgName
            }
            else -> {  //住院详情

                binding.llMedicalLayout.visibility = View.GONE

                binding.title.mTvTitle.text = "住院详情"
                binding.tvEventTime.text = "住院时间: "
                binding.tvUploadTime.text = "上传时间: "
                binding.tvOrgName.text = "住院医院: "
                binding.tvList.text = "上传报告: "

                resultData?.apply {
                    binding.tvTitle.text = hospitalExplain

                    val sTime =  if (startTime !=null && startTime.length>9){
                        startTime.subSequence(0,10)
                    }else{
                        ""
                    }
                    val eTime = if (endTime !=null && endTime.length>9){
                        endTime.subSequence(0,10)
                    }else{
                        ""
                    }

                    binding.tvTijianTime.text =
                        "$sTime 至 $eTime"
                    if (resultData?.updateTime.isNullOrEmpty()){
                        binding.tvUploadTime.text = "${resultData?.createTime}"
                    }else{
                        binding.tvUploadTime.text = "${resultData?.updateTime}"
                    }
                    binding.tvOrg.text = hospital
                }
            }
        }
    }

    fun showIndexPic(index: Int) {
        config?.nowThumbnailIndex = index
        phoneView?.apply(config)?.show()
    }


    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.apply {
            this.setOnClickListener {
                finish()
            }
        }
        initRecycle()
        if (adapter.data.isNotEmpty()) {
            phoneView?.apply(config)
        }

        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
//        binding.title.mTvTitle.text = "数据管理"
    }

    override fun liveObserver() {
    }

    fun initRecycle() {
        binding.rvList.layoutManager = GridLayoutManager(this, 2)
        binding.rvList.adapter = adapter

        binding.rvMedicalList.layoutManager = GridLayoutManager(this, 2)
        binding.rvMedicalList.adapter = adapter1

        adapter.setOnItemClickListener { adapter, view, position ->
            showIndexPic(position)
        }

        adapter1.setOnItemClickListener { adapter, view, position ->
            showIndexPic(position)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        phoneView?.destroy()
    }
}