package com.dlc.chemical.ui.waring

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dlc.chemical.R
import com.dlc.chemical.databinding.ActivityWaringRecordBinding
import com.dlc.chemical.ui.common.CommonListActivity
import com.dlc.chemical.ui.common.PageQuickAdapter
import com.dlc.chemical.ui.widget.TitleBar
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.vo.request.AlarmRecordReq
import com.rc.httpcore.vo.response.AioAlarmRecordVo
import io.reactivex.disposables.Disposable

/**
 * 报警台账
 *
 * @author ReiChin_
 */
class WaringRecordActivity : CommonListActivity<AioAlarmRecordVo, ActivityWaringRecordBinding>(),
    TitleBar.TitleListener {

    companion object {
        private const val REQ_HANDLE_WARING = 10
    }

    override val mAdapter by lazy { WaringRecordAdapter() }
    override val mSrlRefresh by lazy { viewBinding.swipeRefreshLayout }
    override val mRvContent by lazy { viewBinding.recyclerView }
    override val mTvPageInfo by lazy { viewBinding.pageInfo }
    override val mTvPagePrev by lazy { viewBinding.pagePrev }
    override val mTvPageNext by lazy { viewBinding.pageNext }

    override fun createViewBinding() = ActivityWaringRecordBinding.inflate(layoutInflater)

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)

        viewBinding.titleBar.setTitleListener(this)
        viewBinding.titleBar.setTitleInfoFromSp()
    }

    override fun queryData(): Disposable? {
        val param = AlarmRecordReq()
        param.pageNum = mCurrentPage
        param.pageSize = PAGE_SIZE

        return ApiRepository.queryAlarmRecordList(param)
            .subscribe({
                dismissLoading()
                dispatchLoadDataSuccess(it.rows, it.total)
            }, { throwable ->
                dismissLoading()
                dispatchLoadDataFailure(throwable)
            })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val intent = Intent(this, WaringDetailActivity::class.java)
        intent.putExtra("id", mAdapter.getItem(position).id)
        startActivityForResult(intent, REQ_HANDLE_WARING)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (REQ_HANDLE_WARING == requestCode) {
            if (RESULT_OK == resultCode) loadData(true)
        }
    }

    override fun createItemDecoration(): RecyclerView.ItemDecoration? = null

    override fun onHomeViewClicked(view: View) {
        finish()
    }

    override fun onBackViewClicked(view: View) {
        finish()
    }

}

class WaringRecordAdapter : PageQuickAdapter<AioAlarmRecordVo>(R.layout.item_waring_record) {

    override fun convert(holder: BaseViewHolder, item: AioAlarmRecordVo) {
        holder.setText(R.id.alarmTime, item.alarmTime)
            .setText(R.id.alarmContent, item.alarmContent)
            .setText(R.id.alarmMode, item.decodeAlarmMode())
            .setText(R.id.handlingStatus, item.decodeHandlingStatus())
            .setText(R.id.liableUserNames, item.liableUserNames)
    }

}