package com.adealink.weparty.wallet

import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.onClick
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.module.wallet.Wallet
import com.adealink.weparty.wallet.comp.MyBalanceComp
import com.adealink.weparty.wallet.comp.MyIncomeComp
import com.adealink.weparty.wallet.comp.WalletActivityComp
import com.adealink.weparty.wallet.databinding.ActivityWalletBinding
import com.adealink.weparty.wallet.viewmodel.WalletViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModelFactory
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Wallet.Wallet.PATH],
    desc = "钱包首页"
)
class WalletActivity : BaseActivity() {

    private val binding by viewBinding(ActivityWalletBinding::inflate)
    private val viewModel by viewModels<WalletViewModel> { WalletViewModelFactory() }

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        val statusBarHeight = this@WalletActivity.statusBarHeight()
        binding.topBar.setPadding(0, statusBarHeight, 0, 0)
        binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height = statusBarHeight + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt()
        }

        binding.btnBack.onClick {
            finish()
        }
        binding.btnDetail.onClick {
            goDetail()
        }
    }

    override fun initComponents() {
        super.initComponents()
        MyBalanceComp(this, binding.vBalance).attach()
        MyIncomeComp(this, binding.vIncome).attach()
        WalletActivityComp(this, binding.vActivity).attach()
    }

    override fun onResume() {
        super.onResume()
        viewModel.refreshWalletData()
    }

    private fun goDetail() {
        Router.build(this, Wallet.Detail.PATH).start()
    }

}