package com.zj365.health.act.datamanage

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
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.PictureShowAdapter
import com.zj365.health.databinding.ActYshiDataManageDetailsBinding
import com.zj365.health.vm.DataManageVm
import com.xty.network.model.MedicalDataBean

/**
 * 饮食管理--详情页面
 */
@Route(path = ARouterUrl.DATAMANAGE_YSHI_DETAILS_ACT)
class YShiDataManageDetailsAct : BaseVmAct<DataManageVm>() {

    private val binding by lazy { ActYshiDataManageDetailsBinding.inflate(layoutInflater) }
    override fun setLayout(): View = binding.root

    private val mAdapter by lazy { PictureShowAdapter() }

    private var resultData: MedicalDataBean? = null

    private var phoneView: PhotoViewer? = null
    private var config: TransferConfig? = null
    private var userId = ""

    @SuppressLint("SetTextI18n")
    override fun initData() {
        super.initData()
        val result = intent.getStringExtra("data")
        userId = intent.getStringExtra("userId").toString()
        result?.let {
            resultData = GsonUtils.fromJson(it, MedicalDataBean::class.java)
            if (resultData != null) {
                val colorIndex = when (resultData?.upType) {
                    1 -> {
                        0
                    }

                    2 -> {
                        1
                    }

                    else -> {
                        2
                    }
                }
                binding.tvUploadStatus.apply {
                    setBackgroundResource(bg[colorIndex])
                    setTextColor(resources.getColor(textColor[colorIndex]))
                    setPadding(
                        SizeUtil.dp2px(context, 10f),
                        SizeUtil.dp2px(context, 5f),
                        SizeUtil.dp2px(context, 10f),
                        SizeUtil.dp2px(context, 5f)
                    )
                    text = getDataType(resultData?.upType!!)
                }

                binding.tvUploadingTime.text = "上传时间：${resultData?.createTime}"
                binding.tvEatType.text = "用餐类型：${getEatType(resultData?.mealType!!.toInt())}"
                val time = resultData?.time
                if (!TextUtils.isEmpty(time)) {
                    val substring = time!!.substring(0, time.length - 3)
                    binding.tvEatTime.text = "用餐时间：${substring}"
                }
                if (!TextUtils.isEmpty(resultData?.mealExplain)) {
                    binding.tvEatExplain.text = "用餐说明：${resultData?.mealExplain}"
                } else {
                    binding.tvEatExplain.text = "用餐说明：无"
                }

                binding.rvEatImaList.layoutManager = GridLayoutManager(this, 2)
                binding.rvEatImaList.adapter = mAdapter
                val mealList = resultData?.mealList
                if (mealList!!.isNotEmpty()) {
                    val imgList = mutableListOf<String>()
                    val list = mutableListOf<BaseImgUrlBean>()
                    mealList.forEach { img ->
                        imgList.add(img.imageUrl!!)
                        val url = BaseImgUrlBean(img.imageUrl)
                        list.add(url)
                    }
                    mAdapter.setNewInstance(imgList)

                    config = TransferConfig.build()
                        .setSourceBeanList(list)
                        .setImageLoader(GlideImageLoader.with(this)).bindRecyclerView(binding.rvEatImaList, R.id.ivImage)
                }
            }
        }
    }

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

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "饮食详情"
        binding.title.mIvBack.apply {
            this.setOnClickListener {
                finish()
            }
        }
        phoneView = PhotoViewer.getDefault(this)
        if (mAdapter.data.isNotEmpty()) {
            phoneView?.apply(config)
        }

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

    override fun liveObserver() {

    }

    private val textColor by lazy {
        arrayOf(
            R.color.col_9BD,
            R.color.col_9Bd,
            R.color.col_FDA481
        )
    }

    private val bg by lazy {
        arrayOf(
            R.drawable.shape_xya_status_yellow,
            R.drawable.shape_xya_status,
            R.drawable.shape_xya_status_orange,
        )
    }

    private fun getDataType(dataType: Int): String {
        val showName = when (dataType) {
            1 -> {
                "用户上传"
            }

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

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

    private fun getEatType(mealType: Int):String {
        val mealTypeStr = when (mealType){
            1 -> {
                "早餐"
            }
            2 -> {
                "午餐"
            }
            3 -> {
                "晚餐"
            }
            4 -> {
                "早餐加餐"
            }
            5 -> {
                "午餐加餐"
            }
            6 -> {
                "晚餐加餐"
            }
            else -> {
                "其它"
            }
        }
        return mealTypeStr
    }

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