package com.module.backstage.fragment


import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.hboxs.serialport.plc.DialogClickListener
import com.module.backstage.R
import com.module.backstage.adapter.ErrorAdapter
import com.module.backstage.databinding.BackstageFragmentErrorstatBinding
import com.module.backstage.dialog.OKCancelDialog
import com.module.backstage.vm.ErrorStatFragmentVM
import com.module.database.room.entity.WarringBean
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import dagger.hilt.android.AndroidEntryPoint

/**
 * 报警记录Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@AndroidEntryPoint
class ErrorStatFragment : BaseFragment<BackstageFragmentErrorstatBinding, ErrorStatFragmentVM>() {

    override val mViewModel: ErrorStatFragmentVM by viewModels()
    private var errorAdapter: ErrorAdapter? = null
    override fun createVB() = BackstageFragmentErrorstatBinding.inflate(layoutInflater)
    var okCancelDialog: OKCancelDialog? = null


    override fun BackstageFragmentErrorstatBinding.initView() {
        rvAlarm.layoutManager = LinearLayoutManager(this@ErrorStatFragment.context)
        tvCleanAll.setOnClickListener {
            showOnOffDialog()
        }
    }

    override fun initObserve() {
        observeLiveData(mViewModel.allWarringData, ::getAllWarringData)
    }

    private fun getAllWarringData(warringBeans: MutableList<WarringBean>?) {
        mBinding.tvErrorTips.visibility = View.GONE
        errorAdapter = warringBeans?.let { ErrorAdapter(it) }
        errorAdapter?.animationEnable = true//开启动画
        errorAdapter?.isAnimationFirstOnly =true// 如果你只想在第一次加载数据时显示动画，可以设置isFirstOnly为true
        val headerView: View = LayoutInflater.from(context).inflate(R.layout.backstage_item_errorstat, null)
        errorAdapter?.addHeaderView(headerView)
        errorAdapter?.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn)
        mBinding.rvAlarm.adapter = errorAdapter
    }

    override fun initRequestData() {
        mViewModel.getAllWarringData()
    }

    private fun showOnOffDialog() {
        if (okCancelDialog == null) {
            okCancelDialog = this@ErrorStatFragment.context?.let { OKCancelDialog(it) }
        }
        okCancelDialog?.setListener(object: DialogClickListener {
            override fun onClickListener(type: Int, text: String?) {
                when (type) {
                    okCancelDialog?.type1-> {
                        mViewModel.removeAllWarringData()
                        mBinding.tvErrorTips.visibility = View.VISIBLE
                        errorAdapter?.productList?.clear()
                        errorAdapter?.notifyDataSetChanged()
                        okCancelDialog?.dismiss()

                    }
                    else -> {}
                }
            }

        })

        okCancelDialog?.show()
    }

    override fun onDestroyView() {
        errorAdapter = null

        super.onDestroyView()
    }
}