package com.dlc.eboard.ui.rule

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dlc.eboard.R
import com.dlc.eboard.databinding.ActivitySafetyRegulationBinding
import com.dlc.eboard.ui.common.BaseCountDownRefreshActivity
import com.dlc.eboard.ui.widget.NavViewCompat
import com.dlc.eboard.ui.widget.TitleBar
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.vo.response.SafeBook
import io.reactivex.disposables.Disposable

/**
 * 规章制度
 *
 * @author ReiChin_
 */
open class SafetyRegulationActivity :
    BaseCountDownRefreshActivity<SafeBook, ActivitySafetyRegulationBinding>() {

    override val mSrlRefresh: SwipeRefreshLayout by lazy { viewBinding.swipeRefreshLayout }
    override val mRvContent: RecyclerView by lazy { viewBinding.recyclerView }
    override val mAdapter: BaseQuickAdapter<SafeBook, BaseViewHolder>
            by lazy { SafetyRegulationAdapter() }

    override val mTitleBar: TitleBar by lazy { viewBinding.titleBar }
    override val mNavView: NavViewCompat by lazy { viewBinding.navView }

    private var mSystemFlag = "1"

    override fun createViewBinding() =
        ActivitySafetyRegulationBinding.inflate(LayoutInflater.from(this))

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

        // 学校制度
        viewBinding.schoolSystem.setOnClickListener {
            mSystemFlag = "1"
            loadData(true)
        }

        // 学院制度
        viewBinding.collegeSystem.setOnClickListener {
            mSystemFlag = "2"
            loadData(true)
        }

        // 中心制度
        viewBinding.centerSystem.setOnClickListener {
            mSystemFlag = "3"
            loadData(true)
        }
    }

    override fun onLoadMore() {
        // 禁用上拉加载
        mAdapter.loadMoreModule.loadMoreEnd(false)
    }

    override fun queryData(): Disposable? {
        return ApiRepository.safeBookList(mSystemFlag)
            .subscribe({ data ->
                dispatchLoadDataSuccess(data)
            }, { throwable ->
                dispatchLoadDataFailure(throwable)
            })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = mAdapter.getItem(position)
        val intent = Intent(this, SafetyRegulationDetailActivity::class.java)
        intent.putExtra("data", item)
        intent.putExtra("onEboard", onEboard())
        startActivity(intent)
    }

    protected open fun onEboard() = true

    override fun enabledBackCountDown() = true

    override fun enabledLogoutCountDown() = true

}

private class SafetyRegulationAdapter :
    BaseQuickAdapter<SafeBook, BaseViewHolder>(R.layout.item_safety_regulation),
    LoadMoreModule {

    override fun convert(holder: BaseViewHolder, item: SafeBook) {
        holder.setText(R.id.text, item.name)
    }

}