package com.remax.visualnovel.ui.wallet.transaction

/**
 * Created by HJW on 2025/9/12
 */
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.remax.visualnovel.R
import com.remax.visualnovel.app.base.BaseBindingActivity
import com.remax.visualnovel.databinding.ActivityTransactionBinding
import com.remax.visualnovel.extension.addScrollerAlpha
import com.remax.visualnovel.extension.autoRefreshList
import com.remax.visualnovel.extension.launchAndCollect
import com.remax.visualnovel.ui.wallet.WalletViewModel
import com.remax.visualnovel.utils.Routers
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.itemdecoration.SpaceItemDecoration
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Routers.TRANSACTION)
class TransactionDetailActivity : BaseBindingActivity<ActivityTransactionBinding>() {

    private val walletViewModel by viewModels<WalletViewModel>()

    private val transactionAdapter by lazy {
        TransactionAdapter()
    }

    override fun initView() {
        setToolbar(R.string.transaction_detail)
        with(binding) {
            appBarLayout.addScrollerAlpha(tvTitle, isFromToolbar = true)
            refreshLayout.setOnRefreshListener {
                getData(true)
            }
            with(recyclerView) {
                adapter = transactionAdapter
                layoutManager = LinearLayoutManager(recyclerView.context)
                addItemDecoration(SpaceItemDecoration(16.dp, 24.dp,0,24.dp))
                with(transactionAdapter) {
                    setOnItemClickListener { _, _, position ->

                    }
                    loadMoreModule.setOnLoadMoreListener {
                        getData(false)
                    }
                }
            }
        }
    }

    override fun initData() {
        binding.refreshLayout.autoRefreshList()
    }

    private fun getData(isRefresh: Boolean) {
        launchAndCollect({ walletViewModel.getBillList(isRefresh, null) }) {
            onSuccess = {
                val data = it?.pageList?.datas ?: emptyList()
                transactionAdapter.setTransactionData(isRefresh, data, 120)
            }

            onComplete = {
                if (isRefresh) {
                    binding.refreshLayout.finishRefresh()
                }
            }
        }
    }

    companion object {
        fun start() {
            ARouter.getInstance()
                .build(Routers.TRANSACTION)
                .navigation()
        }
    }

}