package com.example.chemical.ui.uses


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.chemical.ChemicalApp
import com.example.chemical.R
import com.example.chemical.databinding.ActivityProcessedBinding
import com.example.chemical.ui.adapter.*
import com.example.chemical.ui.common.BaseCountDownActivity
import com.example.chemical.weidith.ImgDialogs
import com.rc.core.log.RcLog
import com.rc.httpcore.HttpConfig
import com.rc.httpcore.apk.ProcessedBean
import com.rc.httpcore.client.ApiRepository
import java.util.*


//已处理 or  未处理  需要处理    //  item_timeout
class ProcessedActivity : BaseCountDownActivity<ActivityProcessedBinding>() {

    //化学品信息
    private val mAdapter by lazy { TimeoutAdapter() }

    //
    private val mAdapterNotReceived by lazy { NotReceivedAdapter(this) }
    private val mChemicalTimeoutAdapter by lazy { ChemicalTimeoutAdapter() }

    //通知记录
    private val mAdapterLogVoList by lazy { LogVoListAdapter() }

    //报警信息
    private val mImgAdapter by lazy { SnapImgAdapter(this) }

    private var mNoticeId: String? = null
    private var mHandleNotes: String? = null
    override fun createViewBinding() = ActivityProcessedBinding.inflate(LayoutInflater.from(this))

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)
        viewBinding.tvName.text = ChemicalApp.userData!!.userName
        viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
        val imageView = viewBinding.imageName
        // 使用 Glide 加载网络图片
        Glide.with(this)
            .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
            .into(imageView)
        // 使用 Glide 加载网络图片
        RcLog.info("==图片地址${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
        viewBinding.titleDeptName.text =
            "${ChemicalApp.confs!!.deptName}-${ChemicalApp.confs!!.roomNum}"

        Glide.with(this)
            .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.circularLogo}")
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
            .into(viewBinding.image)

        viewBinding.tvReturn.setOnClickListener {
            finish()
        }

        //处理预警问题
        viewBinding.revert.setOnClickListener {
            showLoading("加载中...")
            val disposable = ApiRepository.handleMessage(mNoticeId!!, mHandleNotes!!)
                .subscribe({ data ->
                    dismissLoading()
                    finish()
                }, { throwable ->
                    dismissLoading()
                    showNetError(throwable)
                })
            addDisposable(disposable)
        }

        //照片显示
        mImgAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.img) {
                //弹框变成大窗体
                val mDialogs = ImgDialogs(this, mImgAdapter.data[position])
                if (!mDialogs.isShowing) {
                    mDialogs.show()
                }
            }
        }

        val noticeId = intent.getStringExtra("noticeId")
        if (noticeId != null) {
            getLists(noticeId)
        }

    }

    override fun cdTime(cd: Int) {
        viewBinding.tvReturn.text = "返回${cd}s"
    }


    private fun getLists(noticeId: String) {
        showLoading("加载中...")
        val disposable = ApiRepository.warningNoticeDetail(noticeId)
            .subscribe({ data ->
                dismissLoading()
                upView(data)
            }, { throwable ->
                dismissLoading()
                showNetError(throwable)
            })
        addDisposable(disposable)
    }

    private fun upView(datas: ProcessedBean) {

        when (datas.noticeStatus) {
            1 -> {
                //已处理
                viewBinding.yStatus.visibility = View.VISIBLE
            }
            0 -> {
                //待处理
                viewBinding.dStatus.visibility = View.VISIBLE
                if (datas.isHandlePeople) {
                    //显示  处理
                    mNoticeId = datas.noticeId
                    mHandleNotes = datas.handleNotes!!
                    viewBinding.revert.visibility = View.VISIBLE
                }
            }
            2 -> {
                //待处理  详情显示
                viewBinding.dStatus.visibility = View.VISIBLE
            }
        }
        viewBinding.warnTime.text = "${datas.warnTime}"
        viewBinding.deptName.text = "${datas.deptName}"
        viewBinding.subName.text = "${datas.subName}"
        //1化学品违规带离。2超时未归还。3化学品已过期。4化学品即将过期
        RcLog.info("======当前状态${datas.chemicalWarnType}")
        when (datas.chemicalWarnType) {
            1 -> {  //  化学品信息只显示一条  1-1  已处理
                viewBinding.lin1.visibility = View.VISIBLE
                viewBinding.collectUserName.visibility = View.VISIBLE
                viewBinding.rlBelong.visibility = View.VISIBLE
                viewBinding.belongName.text = "${datas.belongName}" //归属人
                if (datas.snapImg != null && datas.snapImg!!.size > 0) {
                    //报警抓拍 照片显示
                    viewBinding.tvProc.visibility = View.VISIBLE
                    viewBinding.rvProcessed.visibility = View.VISIBLE
                    // 创建GridLayoutManager，设置为每行4个item
                    val layoutManager =
                        GridLayoutManager(this, 4, LinearLayoutManager.HORIZONTAL, false)
                    viewBinding.rvProcessed.layoutManager = layoutManager
                    mImgAdapter.setNewInstance(datas.snapImg!!.toMutableList())
                }
                viewBinding.normsNum.text = "余量：${datas.normsNum}"
                viewBinding.surplus.text = "规格：${datas.surplus}"

                viewBinding.processing.visibility = View.VISIBLE
                viewBinding.lingVi.visibility = View.VISIBLE
                //handleType 处理方式：1手动处理。2归还操作。3出库操作。4入库操作
                when (datas.handleType) {
                    1 -> {
                        viewBinding.typeMsg.text = "手动处理"
                    }
                    2 -> {
                        viewBinding.typeMsg.text = "归还操作"
                    }
                    3 -> {
                        viewBinding.typeMsg.text = "出库操作"
                    }
                    4 -> {
                        viewBinding.typeMsg.text = "入库操作"
                    }
                }
                if (datas.chemicalName != null) {
                    viewBinding.warnContent.text = "${datas.chemicalName}化学品违规带离"
                } else {
                    viewBinding.warnContent.text = "化学品违规带离"
                }

                if (datas.handlePerson != null) {
                    viewBinding.handlePerson.text = "${datas.handlePerson}"
                }
                if (datas.handleTime != null) {
                    viewBinding.handleTime.text = "${datas.handleTime}"
                }
                if (datas.handleNotes != null) {
                    viewBinding.handleNotes.text = "${datas.handleNotes}"
                }
                viewBinding.msg.text = "违规带离的化学品确认已放入化学品柜指定位置后，点击处理"
            }
            2 -> { //领用超过未归还  1-1
                if (datas.snapImg != null && datas.snapImg!!.size > 0) {
                    //报警抓拍 照片显示
                    viewBinding.tvProc.visibility = View.VISIBLE
                    viewBinding.rvProcessed.visibility = View.VISIBLE
                    // 创建GridLayoutManager，设置为每行4个item
                    val layoutManager =
                        GridLayoutManager(this, 4, LinearLayoutManager.HORIZONTAL, false)
                    viewBinding.rvProcessed.layoutManager = layoutManager
                    mImgAdapter.setNewInstance(datas.snapImg!!.toMutableList())
                }
                viewBinding.lin1.visibility = View.VISIBLE
                viewBinding.rlBelong.visibility = View.VISIBLE  //归属人
                viewBinding.lingOne.visibility = View.VISIBLE
                viewBinding.lingTwo.visibility = View.VISIBLE
                viewBinding.belongName.text = "${datas.belongName}" //归属人
                viewBinding.collectTime.text = "${datas.collectTime}"
                viewBinding.collectTimeAging.text = "${datas.collectTimeAging}小时"
                if (datas.chemicalName != null) {
                    viewBinding.chemicalName.text = "${datas.chemicalName}"
                }

                viewBinding.position.text = "${datas.position}层"
                viewBinding.collectUserName.text = "领用人：${datas.collectUserName}"
                viewBinding.normsNum.text = "规格：${datas.normsNum}"
                viewBinding.surplus.text = "余量：${datas.surplus}"
                viewBinding.msg.text = "操作归还后报警状态自动恢复正常"
                viewBinding.processing.visibility = View.VISIBLE
                viewBinding.lingVi.visibility = View.VISIBLE
                viewBinding.lint3.visibility = View.GONE

                when (datas.handleType) {
                    1 -> {
                        viewBinding.typeMsg.text = "手动处理"
                    }
                    2 -> {
                        viewBinding.typeMsg.text = "归还操作"
                    }
                    3 -> {
                        viewBinding.typeMsg.text = "出库操作"
                    }
                    4 -> {
                        viewBinding.typeMsg.text = "入库操作"
                    }
                }


                if (datas.handlePerson != null) {
                    viewBinding.handlePerson.text = "${datas.handlePerson}"
                }
                if (datas.handleTime != null) {
                    viewBinding.handleTime.text = "${datas.handleTime}"
                }
                if (datas.chemicalName != null) {
                    viewBinding.warnContent.text = "${datas.chemicalName}超时未归还"
                } else {
                    viewBinding.warnContent.text = "超时未归还"
                }
                if (datas.rfidCode != null) {
                    viewBinding.chemicalCode.visibility = View.VISIBLE
                    viewBinding.chemicalCode.text = "-${datas.rfidCode}"
                }
            }
            3 -> {  //化学品已过期   多个
                viewBinding.lin2.visibility = View.VISIBLE
                viewBinding.rvView?.layoutManager = LinearLayoutManager(this)
                viewBinding.rvView?.adapter = mAdapter
                mAdapter.setNewInstance(datas.chemicalExpireList.toMutableList())
                viewBinding.msg.text = "出库后报警状态自动恢复正常"
                if (datas.chemicalName != null) {
                    viewBinding.warnContent.text = "有${datas.chemicalExpireList.size}个化学品过期"
                } else {
                    viewBinding.warnContent.text = "化学品过期"
                }

                viewBinding.lingVi.visibility = View.VISIBLE
                viewBinding.lint3.visibility = View.GONE

                when (datas.handleType) {
                    1 -> {
                        viewBinding.typeMsg.text = "手动处理"
                    }
                    2 -> {
                        viewBinding.typeMsg.text = "归还操作"
                    }
                    3 -> {
                        viewBinding.typeMsg.text = "出库操作"
                    }
                    4 -> {
                        viewBinding.typeMsg.text = "入库操作"
                    }
                }
                if (datas.handlePerson != null) {
                    viewBinding.handlePerson.text = "${datas.handlePerson}"
                }
                if (datas.handleTime != null) {
                    viewBinding.handleTime.text = "${datas.handleTime}"
                }

            }
            4 -> {  //化学品即将过期  多个
                viewBinding.lin2.visibility = View.VISIBLE
                viewBinding.rvView?.layoutManager = LinearLayoutManager(this)
                viewBinding.rvView?.adapter = mAdapter
                mAdapter.setNewInstance(datas.chemicalExpireList.toMutableList())
                viewBinding.msg.text = "出库后报警状态自动恢复正常"
                viewBinding.warnContent.text = "有${datas.chemicalExpireList.size}个化学品即将过期"

                viewBinding.lingVi.visibility = View.VISIBLE
                viewBinding.lint3.visibility = View.GONE

                when (datas.handleType) {
                    1 -> {
                        viewBinding.typeMsg.text = "手动处理"
                    }
                    2 -> {
                        viewBinding.typeMsg.text = "归还操作"
                    }
                    3 -> {
                        viewBinding.typeMsg.text = "出库操作"
                    }
                    4 -> {
                        viewBinding.typeMsg.text = "入库操作"
                    }
                }
                if (datas.handlePerson != null) {
                    viewBinding.handlePerson.text = "${datas.handlePerson}"
                }
                if (datas.handleTime != null) {
                    viewBinding.handleTime.text = "${datas.handleTime}"
                }

            }
            5 -> {//超期未入库
                viewBinding.lin2.visibility = View.VISIBLE
                viewBinding.rvView?.layoutManager = LinearLayoutManager(this)
                viewBinding.rvView?.adapter = mAdapterNotReceived
                mAdapterNotReceived.setNewInstance(datas.chemicalOverdueStockList!!.toMutableList())
                viewBinding.msg.text = "入库后报警状态自动恢复正常"
                if (datas.chemicalOverdueStockList != null && datas.chemicalOverdueStockList!!.size > 0) {
                    viewBinding.warnContent.text =
                        "有${datas.chemicalOverdueStockList!!.size}个化学品超期未入库"
                }

                viewBinding.lingVi.visibility = View.VISIBLE
                viewBinding.lint3.visibility = View.GONE

                when (datas.handleType) {
                    1 -> {
                        viewBinding.typeMsg.text = "手动处理"
                    }
                    2 -> {
                        viewBinding.typeMsg.text = "归还操作"
                    }
                    3 -> {
                        viewBinding.typeMsg.text = "出库操作"
                    }
                    4 -> {
                        viewBinding.typeMsg.text = "入库操作"
                    }
                }
                if (datas.handlePerson != null) {
                    viewBinding.handlePerson.text = "${datas.handlePerson}"
                }
                if (datas.handleTime != null) {
                    viewBinding.handleTime.text = "${datas.handleTime}"
                }
            }
            6 -> {//业务超时
//                viewBinding.lin2.visibility = View.VISIBLE
//                viewBinding.rvView?.layoutManager = LinearLayoutManager(this)
//                viewBinding.rvView?.adapter = mChemicalTimeoutAdapter
//                mChemicalTimeoutAdapter.setNewInstance(datas.chemicalTimeoutList!!.toMutableList())
                viewBinding.msg.text = "打开柜门后未完成业务操作，系统自动报警"
                viewBinding.warnContent.text = "业务操作超时，请尽快处理。"
//                viewBinding.lingVi.visibility = View.VISIBLE
//                viewBinding.lint3.visibility = View.GONE
//
//                when (datas.handleType) {
//                    1 -> {
//                        viewBinding.typeMsg.text = "手动处理"
//                    }
//                    2 -> {
//                        viewBinding.typeMsg.text = "归还操作"
//                    }
//                    3 -> {
//                        viewBinding.typeMsg.text = "出库操作"
//                    }
//                    4 -> {
//                        viewBinding.typeMsg.text = "入库操作"
//                    }
//                }
//                viewBinding.handlePerson.text = "${datas.handlePerson}"
//                viewBinding.handleTime.text = "${datas.handleTime}"

                viewBinding.rlBelongTow.visibility = View.VISIBLE
                viewBinding.operationPerson.text = "${datas.operationPerson}"
                viewBinding.alarmType.text = "${datas.alarmType}"
                //柜子信息
                if (datas.position != null) {
                    viewBinding.cabinetName.text = "${datas.position}"
                }

            }
        }

        //通知记录
        viewBinding.rvUse?.layoutManager = LinearLayoutManager(this)
        viewBinding.rvUse?.adapter = mAdapterLogVoList
        mAdapterLogVoList.setNewInstance(datas.warningNoticeLogVoList.toMutableList())
    }


}