package com.gamedog.vididin.features.withdraw.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.core.view.isVisible
import com.ama.core.architecture.util.ResUtil
import com.gamedog.vididin.core.login.login.AccountManager
import com.viddin.videos.free.R
import com.viddin.videos.free.databinding.WithdrawItemBankViewBinding as ViewBinding


class WithDrawItemBankView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var mItemIndex: Int = 0
    private var mIsSelected = false
    private var mBinding: ViewBinding


    init {
        mBinding = ViewBinding.inflate(LayoutInflater.from(context), this, true)
    }

    fun updateBankAccountInfo() {
        with(mBinding) {
            if (AccountManager.isBankAccountExist()) {
                bankContainer.isVisible = true
                tvBankHint.isVisible = false
                AccountManager.getBankInfo()?.bankAccount?.let {
                    tvBankAccount.text = it
                }
            } else {
                bankContainer.isVisible = false
                tvBankHint.isVisible = true
            }
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        updateBankAccountInfo()
    }

    /**
     * For withdraw number in top area
     */
    fun setAction(itemIndex: Int, clickAction: (Int)->Unit) {
        mItemIndex = itemIndex
        mBinding.root.setOnClickListener {
            clickAction.invoke(mItemIndex)
        }
    }


    /**
     * For bank item in bottom area
     */
    fun setIconAndText(iconRes: Int, textRes: Int, clickAction: ()->Unit) {
        mBinding.tvWithdrawNum.text = ResUtil.getString(textRes)
        mBinding.ivItemIcon.setImageResource(iconRes)
        mBinding.root.setOnClickListener {
            clickAction.invoke()
        }
    }

    fun setSelectedState(isSelected: Boolean) {
        mIsSelected = isSelected
        mBinding.root.setBackgroundResource(if (mIsSelected) R.drawable.withdraw_item_bg_selected else R.drawable.withdraw_item_bg_unselected)
    }

}