package com.dlc.exam.ui.learn.test

import android.content.Intent
import android.graphics.Color
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.ActivityClassTestRecordListBinding
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.request.AssessRecordReq
import com.rc.httpcore.vo.response.AccessRecordBean
import io.reactivex.disposables.Disposable

/**
 * info
 *
 * @author ReiChin_
 */
class ClassTestRecordListActivity :
    CommonListActivity<AccessRecordBean, ActivityClassTestRecordListBinding>(ClassTestRecordAdapter()),
    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() =
        ActivityClassTestRecordListBinding.inflate(LayoutInflater.from(this))

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

    override fun queryData(): Disposable? {
        showLoading("查询中...")
        val param = AssessRecordReq().apply {
            page = mCurrentPage
            pageSize = ListPageView.PAGE_SIZE
            chapterId = intent.getStringExtra("chapterId")
        }
        return ApiRepository.classTestRecord(param)
            .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)
        val intent = Intent(this, ClassTestRecordActivity::class.java)
        intent.putExtra("paperId", item.paperId)
        startActivity(intent)
    }

    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 ClassTestRecordAdapter :
    PageQuickAdapter<AccessRecordBean>(R.layout.item_class_test_record) {

    override fun convert(holder: BaseViewHolder, item: AccessRecordBean) {
        val pagePosition = pageOffsite + holder.adapterPosition + 1
        holder.setText(R.id.number, "$pagePosition")
            .setText(R.id.assessName, item.assessName)
            .setText(R.id.assessTime, item.assessTime)
            .setText(R.id.topicCount, item.allQu)
            .setText(R.id.errorCount, item.errors)
            .setTextColor(
                R.id.testResult,
                Color.parseColor(if ("100" == item.accuracy) "#2EAC2B" else "#FB3434")
            )
            .setText(R.id.testResult, if ("100" == item.accuracy) "通过" else "未通过")
            .setText(R.id.bonusPoints, item.points)
    }

}