package com.dlc.exam.ui.mock

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import com.dlc.exam.ExamApp
import com.dlc.exam.R
import com.dlc.exam.databinding.ActivitySelectedMockTypeBinding
import com.dlc.exam.databinding.ItemCheckItemBinding
import com.dlc.exam.ui.common.BaseCountDownActivity
import com.dlc.exam.ui.me.PersonalCenterActivity
import com.dlc.exam.ui.widget.TitleBar
import com.dlc.exam.ui.widget.flowlayout.FlowLayout
import com.dlc.exam.ui.widget.flowlayout.TagAdapter
import com.rc.httpcore.HttpClient
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.vo.response.TopicClassifyVo
import java.lang.StringBuilder

/**
 * 模拟练习
 *
 * @author ReiChin_
 */
class SelectedMockTypeActivity : BaseCountDownActivity<ActivitySelectedMockTypeBinding>(),
    TitleBar.TitleListener {

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

    override fun initData() {
        super.initData()

        // 查询模拟考试类型
        queryMockExamType()
    }

    private var mDatas: List<TopicClassifyVo>? = null

    private fun queryMockExamType() {
        showLoading("加载中...")
        val disposable = ApiRepository.classifyQueryOption()
            .subscribe({ data ->
                mDatas = data
                dismissLoading()
                viewBinding.checkContentFlow.adapter = CheckItemAdapter(data)
            }, { throwable ->
                mDatas = null
                dismissLoading()
                showNetError(throwable)
                throwable.printStackTrace()
                viewBinding.checkContentFlow.adapter = CheckItemAdapter(null)
            })
        addDisposable(disposable)
    }

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

        viewBinding.confirm.setOnClickListener {
            // 开始模拟练习
            // [2, 3]
            val items = viewBinding.checkContentFlow.selectedList
            if (items.isNullOrEmpty()) {
                showToast("请选择题目类型")
                return@setOnClickListener
            }

            val selectedIds = StringBuilder()
            viewBinding.checkContentFlow.selectedList.forEach {
                selectedIds.append(mDatas?.get(it)?.id).append(",")
            }
            if (selectedIds.isNotEmpty()) {
                selectedIds.deleteCharAt(selectedIds.length - 1)
            }
            val intent = Intent(this, MockTestActivity::class.java)
            intent.putExtra("ids", selectedIds.toString())
            startActivity(intent)
            finish()
        }
    }

    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 CheckItemAdapter(datas: List<TopicClassifyVo>?) : TagAdapter<TopicClassifyVo>(datas) {

    override fun getView(parent: FlowLayout?, position: Int, text: TopicClassifyVo?): View {
        val binding = ItemCheckItemBinding.inflate(LayoutInflater.from(parent!!.context))
        binding.checkContent.text = text?.title
        return binding.root
    }

    override fun onSelected(position: Int, view: View?) {
        view?.findViewById<ImageView>(R.id.check_selected)?.visibility = View.VISIBLE
    }

    override fun unSelected(position: Int, view: View?) {
        view?.findViewById<ImageView>(R.id.check_selected)?.visibility = View.INVISIBLE
    }

}