package com.grkj.iscs_mc.view.fragment

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.navigation.fragment.navArgs
import com.bumptech.glide.Glide
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.FragmentMaterialInspectionTableBinding
import com.grkj.iscs_mc.extentions.navigateUp
import com.grkj.iscs_mc.extentions.setSelected
import com.grkj.iscs_mc.model.vo.inspection.InspectionCheckTableDataRespVO
import com.grkj.iscs_mc.view.base.BaseMvpFragment
import com.grkj.iscs_mc.view.dialog.InspectionDialog
import com.grkj.iscs_mc.view.iview.IMaterialInspectionTableView
import com.grkj.iscs_mc.view.presenter.MaterialInspectionTablePresenter
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 物资检查表页
 */
class MaterialInspectionTableFragment :
    BaseMvpFragment<IMaterialInspectionTableView, MaterialInspectionTablePresenter, FragmentMaterialInspectionTableBinding>() {

    private val mRecordList = mutableListOf<InspectionCheckTableDataRespVO.IsMaterialsCheckRecord>()
    private val mStandardList = mutableListOf<InspectionCheckTableDataRespVO.CheckStandardVO>()

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

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

        mBinding?.rvMaterial?.adapter = object : CommonAdapter<InspectionCheckTableDataRespVO.IsMaterialsCheckRecord>(
            requireContext(),
            R.layout.item_rv_inspection_table,
            mRecordList
        ) {
            override fun convert(holder: ViewHolder, vo: InspectionCheckTableDataRespVO.IsMaterialsCheckRecord, position: Int) {
                holder.setText(R.id.tv_number, String.format("%03d", position + 1))
                Glide.with(this@MaterialInspectionTableFragment).load(vo.materialsTypeIcon).into(holder.getView(R.id.iv_icon))
                holder.setText(R.id.tv_type_name, vo.materialsTypeName)
                holder.setText(R.id.tv_name, vo.materialsName)
                holder.setText(R.id.tv_rfid, vo.materialsRfid)

                holder.setSelected(R.id.rl_status_ok, vo.status == "0")
                holder.setSelected(R.id.rl_status_expired, vo.status == "1")
                holder.setSelected(R.id.rl_status_damaged, vo.status == "2")

                holder.setOnClickListener(R.id.rl_status_ok) {
                    vo.status = "0"
                    mBinding?.rvMaterial?.adapter?.notifyItemChanged(position)
                }
                holder.setOnClickListener(R.id.rl_status_expired) {
                    vo.status = "1"
                    mBinding?.rvMaterial?.adapter?.notifyItemChanged(position)
                }
                holder.setOnClickListener(R.id.rl_status_damaged) {
                    vo.status = "2"
                    mBinding?.rvMaterial?.adapter?.notifyItemChanged(position)
                }
//                holder.setText(R.id.tv_measure, vo.measure ?: "")
                val et = holder.getView<EditText>(R.id.et_measure)
                et.setText(vo.measure ?: "")
                val textWatcher = object : TextWatcher {
                    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

                    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                        if (et.hasFocus()) {
                            vo.measure = p0.toString()
                        }
                    }

                    override fun afterTextChanged(p0: Editable?) {}

                }
                et.setOnFocusChangeListener { view, b ->
                    if (b) {
                        et.addTextChangedListener(textWatcher)
                    } else {
                        et.removeTextChangedListener(textWatcher)
                        val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                        imm.hideSoftInputFromWindow(view.windowToken, 0)
                    }
                }
            }
        }

        mBinding?.rvStandard?.adapter = object : CommonAdapter<InspectionCheckTableDataRespVO.CheckStandardVO>(
            requireContext(),
            R.layout.item_rv_inspection_standard,
            mStandardList
        ) {
            override fun convert(holder: ViewHolder, vo: InspectionCheckTableDataRespVO.CheckStandardVO, position: Int) {
                Glide.with(this@MaterialInspectionTableFragment).load(vo.materialsTypeIcon).into(holder.getView(R.id.iv_icon))
                holder.setText(R.id.tv_name, vo.materialsTypeName ?: "")
                holder.setText(R.id.tv_standard, vo.checkStandard ?: "")
            }
        }

        mBinding?.cbAuto?.setOnClickListener {
            autoCheck()
        }

        mBinding?.cbSave?.setOnClickListener {
            presenter?.insertCheckRecord(mRecordList) {
                if (it) navigateUp()
            }
        }

        val args: MaterialInspectionTableFragmentArgs by navArgs()
        presenter?.getCheckTableData(args.planId) {
            it?.list?.let {
                mRecordList.clear()
                mRecordList.addAll(it)
                mBinding?.rvMaterial?.adapter?.notifyDataSetChanged()
            }

            it?.standards?.let {
                mStandardList.clear()
                mStandardList.addAll(it)
                mBinding?.rvStandard?.adapter?.notifyDataSetChanged()
            }

            mBinding?.tvStatus?.text = getString(
                R.string.inspection_status,
                it?.checkProgressVO?.checkNum,
                it?.checkProgressVO?.normalNum,
                it?.checkProgressVO?.timeOutNum,
                it?.checkProgressVO?.badNum
            )
        }
    }

    /**
     * 一键检查
     */
    private fun autoCheck() {
        val dialog = InspectionDialog(requireContext())
        dialog.setConfirmListener { status ->
            mRecordList.forEach {
                it.status = status.toString()
            }
            mBinding?.rvMaterial?.adapter?.notifyDataSetChanged()
        }
        dialog.show()
    }

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