package com.grkj.iscs_mc.view.fragment

import android.view.View
import com.bumptech.glide.Glide
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.FragmentMaterialInspectionPlanBinding
import com.grkj.iscs_mc.extentions.navigateTo
import com.grkj.iscs_mc.extentions.navigateUp
import com.grkj.iscs_mc.model.vo.inspection.LatestInspectionPlanRespVO
import com.grkj.iscs_mc.util.ImgUtil
import com.grkj.iscs_mc.view.base.BaseMvpFragment
import com.grkj.iscs_mc.view.dialog.TipDialog
import com.grkj.iscs_mc.view.iview.IMaterialInspectionPlanView
import com.grkj.iscs_mc.view.presenter.MaterialInspectionPlanPresenter
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 物资检查计划页
 */
class MaterialInspectionPlanFragment :
    BaseMvpFragment<IMaterialInspectionPlanView, MaterialInspectionPlanPresenter, FragmentMaterialInspectionPlanBinding>() {

    private val mCheckList = mutableListOf<LatestInspectionPlanRespVO.MaterialsCheckVO>()
    private var mPlanId: Long? = null

    override val viewBinding: FragmentMaterialInspectionPlanBinding
        get() = FragmentMaterialInspectionPlanBinding.inflate(layoutInflater)

    override fun initView() {
        mBinding?.cbBack?.setOnClickListener { navigateUp() }

        mBinding?.rvMaterialInspectionPlan?.adapter = object : CommonAdapter<LatestInspectionPlanRespVO.MaterialsCheckVO>(
            requireContext(),
            R.layout.item_rv_material_inspection_plan,
            mCheckList
        ) {
            override fun convert(holder: ViewHolder, vo: LatestInspectionPlanRespVO.MaterialsCheckVO, position: Int) {
                holder.setText(R.id.tv_name, vo.materialsTypeName)
                holder.setText(R.id.tv_all_number, vo.allNumber.toString())
                holder.setText(R.id.tv_loan_number, vo.loanNumber.toString())
                holder.setText(R.id.tv_check_number, vo.checkNumber.toString())
                holder.setText(R.id.tv_normal_number, vo.normalNumber.toString())
                holder.setText(R.id.tv_timeout_number, vo.timeoutNumber.toString())
                holder.setText(R.id.tv_bad_number, vo.badNumber.toString())
                Glide.with(this@MaterialInspectionPlanFragment).load(vo.materialsTypeIcon).into(holder.getView(R.id.iv_icon))
            }
        }

        presenter?.getLatestInspectionPlan {
            mPlanId = it?.planId

            mBinding?.tvTitle?.text = "${getString(R.string.plan_date)}：${it?.planDate}  " +
                    "${getString(R.string.plan_status)}：" +
                    when (it?.status) {
                        "1" -> getString(R.string.finished)
                        "2" -> getString(R.string.processing)
                        else -> getString(R.string.not_start)
                    }

            it?.list?.let {
                mCheckList.addAll(it)
                mBinding?.rvMaterialInspectionPlan?.adapter?.notifyDataSetChanged()
            }

            when(it?.status) {
                "0" -> mBinding?.cbStart?.visibility = View.VISIBLE
                "2" -> {
                    mBinding?.cbSign?.visibility = View.VISIBLE
                    mBinding?.cbContinue?.visibility = View.VISIBLE
                    mBinding?.cbSubmit?.visibility = View.VISIBLE
                }
            }

            it?.signatureImg?.let { itSign ->
                Glide.with(this).load(ImgUtil.base64ToImage(itSign)).into(mBinding?.ivSign!!)
            }
        }

        mBinding?.cbSign?.visibility = View.VISIBLE
        mBinding?.cbSign?.setOnClickListener {
            mPlanId?.let {
                navigateTo(MaterialInspectionPlanFragmentDirections.actionInspectionPlanFragmentToInspectionSignatureFragment(it))
            }
        }

        mBinding?.cbStart?.setOnClickListener {
            startInspection()
        }

        mBinding?.cbContinue?.setOnClickListener {

        }

        mBinding?.cbSubmit?.setOnClickListener {

        }

        val bundle = arguments
        val bitmap = bundle?.getString("bitmap")
        bitmap?.let {
            // TODO 处理签名bitmap
        }
    }

    private fun startInspection() {
        val dialog = TipDialog(requireContext())
        dialog.setTip(getString(R.string.inspection_start_tip))
        dialog.setConfirmListener {
            mPlanId?.let {
                navigateTo(MaterialInspectionPlanFragmentDirections.actionInspectionPlanFragmentToInspectionTableFragment(it))
            }
        }
        dialog.show()
    }

    override fun initPresenter(): MaterialInspectionPlanPresenter {
        return MaterialInspectionPlanPresenter()
    }
}