package com.remax.visualnovel.ui.wallet.income

/**
 * Created by HJW on 2025/9/12
 */
import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.activityViewModels
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.BaseBindingFragment
import com.remax.visualnovel.databinding.ItemIncomeHeaderBinding
import com.remax.visualnovel.databinding.LayoutSimpleRecyclerviewRefreshBinding
import com.remax.visualnovel.entity.response.AccountBuffBill
import com.remax.visualnovel.extension.autoRefreshList
import com.remax.visualnovel.extension.formatPrice
import com.remax.visualnovel.extension.launchAndCollect
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.extension.showSingleBtnDialog
import com.remax.visualnovel.ui.wallet.WalletViewModel
import com.remax.visualnovel.ui.wallet.transaction.TransactionAdapter
import com.remax.visualnovel.ui.wallet.transaction.TransactionDetailActivity
import com.remax.visualnovel.utils.Routers
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.itemdecoration.SpaceItemDecoration
import com.pengxr.modular.eventbus.generated.events.EventDefineOfWalletEvents
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Routers.INCOME)
class IncomeFragment : BaseBindingFragment<LayoutSimpleRecyclerviewRefreshBinding>() {

    private val walletViewModel by activityViewModels<WalletViewModel>()

    private val transactionAdapter by lazy {
        TransactionAdapter(AccountBuffBill.INCOME)
    }

    private val incomeHeaderBinding by lazy {
        ItemIncomeHeaderBinding.inflate(layoutInflater)
    }

    @SuppressLint("SetTextI18n")
    override fun onCreated(bundle: Bundle?) {
        with(binding) {
            setOnClick(incomeHeaderBinding.transactionEnter, incomeHeaderBinding.pendingTips) {
                when (this) {
                    incomeHeaderBinding.transactionEnter -> {
                        TransactionDetailActivity.start()
                    }

                    incomeHeaderBinding.pendingTips -> {
                        activity?.showSingleBtnDialog(text = getString(R.string.transaction_tips))
                    }
                }
            }

            with(refreshLayout) {
                refreshLayout.autoRefreshList()
                onRefresh {
                    getData(true)
                }
            }
            with(recyclerView) {
                adapter = transactionAdapter
                layoutManager = LinearLayoutManager(recyclerView.context)
                addItemDecoration(SpaceItemDecoration(16.dp, 24.dp,0,24.dp))
                with(transactionAdapter) {
                    addHeaderView(incomeHeaderBinding.root)
                    incomeHeaderBinding.transactionEnter.text = "${getString(R.string.transaction_detail)}>"
                    headerWithEmptyEnable = true
                    loadMoreModule.setOnLoadMoreListener {
                        getData(false)
                    }
                }
            }
        }
    }

    override fun subscribeUi() {
        EventDefineOfWalletEvents.buffBalanceUpdateSucceeded().observe(this) {
            incomeHeaderBinding.income.setPrice(formatPrice(it?.income))
            incomeHeaderBinding.pending.setPrice(formatPrice(it?.awaitingIncome))
        }
    }

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

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

    companion object {
        fun newInstance(): IncomeFragment {
            return ARouter.getInstance().build(Routers.INCOME)
                .navigation() as IncomeFragment
        }
    }

}