package com.dlc.exam.ui.certificate

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dlc.exam.ExamApp
import com.dlc.exam.R
import com.dlc.exam.databinding.ActivityCertificateManagementBinding
import com.dlc.exam.ui.common.CommonListActivity
import com.dlc.exam.ui.common.PageQuickAdapter
import com.dlc.exam.ui.me.PersonalCenterActivity
import com.dlc.exam.ui.widget.ListPageView
import com.dlc.exam.ui.widget.TitleBar
import com.rc.httpcore.HttpClient
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.vo.response.CertVo
import io.reactivex.disposables.Disposable

/**
 * 证书管理
 *
 * @author ReiChin_
 */
class CertificateManagementActivity :
    CommonListActivity<CertVo, ActivityCertificateManagementBinding>(CertificateManagementAdapter()),
    TitleBar.TitleListener {

    init {
        mAdapter.addChildClickViewIds(R.id.detail)
    }

    override val mSrlRefresh by lazy { viewBinding.swipeRefreshLayout }
    override val mRvContent by lazy { viewBinding.content }
    override val mListPageView by lazy { viewBinding.pageInfo }

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

    override fun initListener() {
        super.initListener()
        viewBinding.titleBar.setTitleListener(this)
        viewBinding.titleBar.setUsername(ExamApp.sLearnLoginVo?.userName)
    }

    override fun queryData(): Disposable? {
        showLoading("查询中...")
        return ApiRepository.queryMyCert(mCurrentPage, ListPageView.PAGE_SIZE)
            .subscribe({
                dismissLoading()
                dispatchLoadDataSuccess(it.rows, it.total)
            }, { throwable ->
                dismissLoading()
                dispatchLoadDataFailure(throwable)
            })
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = mAdapter.getItem(position)
        CertificatePhotoDialog(this, item.certUrl).show()
    }

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

    override fun onLogoutViewClicked(view: View) {
        callLogoutApi {
            showToast("已退出登录")

            ExamApp.sLearnLoginVo = null
            HttpClient.token = null
            finish()
        }
    }

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

    override fun onUserViewClicked(view: View) {
        startActivity(Intent(this, PersonalCenterActivity::class.java))
    }

}

private class CertificateManagementAdapter :
    PageQuickAdapter<CertVo>(R.layout.item_certificate_management) {

    override fun convert(holder: BaseViewHolder, item: CertVo) {
        val pagePosition = pageOffsite + holder.adapterPosition + 1
        holder.setText(R.id.number, "$pagePosition")
            .setText(R.id.certTitle, item.certTitle)
            .setText(R.id.deptName, item.deptName)
            .setText(R.id.code, item.code)
            .setText(R.id.createTime, item.createTime)
            .setText(R.id.expirationTime, item.expirationTime)
    }

}